| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||||||||||
| 3 | # File: GX/HTTP/Cookies.pm | ||||||||||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package GX::HTTP::Cookies; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use GX::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 14 | # Class setup | ||||||||||||||||||||||||||||
| 15 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 19 | ▶ | use GX::Class::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 19 | has 'cookies' => ( | |||||||||||||||||||||||||||
| 20 | isa => 'Array', | ||||||||||||||||||||||||||||
| 21 | accessors => { | ||||||||||||||||||||||||||||
| 22 | '_cookies' => { type => 'get_reference' } | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 19 | build; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 30 | # Overloading | ||||||||||||||||||||||||||||
| 31 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | use overload | ||||||||||||||||||||||||||||
| 34 | 2 | ▶ | '@{}' => sub { $_[0]->_cookies }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 19 | 'fallback' => 1; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 39 | # Public methods | ||||||||||||||||||||||||||||
| 40 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 26 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 26 | for ( @_ ) { | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 79 | ▶ | if ( ! blessed $_ || ! $_->isa( $self->_cookie_class ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($_) and $_->isa($self->_cookie_class)
MC/DC0%0/2
builtin::blessed($_) and $_->isa($self->_cookie_class)
builtin::blessed($_)
$_->isa($self->_cookie_class)
Truth table33%1/3
A and B
A= builtin::blessed($_)B= $_->isa($self->_cookie_class)
| |||||||||||||||||||||||||||||
| 49 | 0 | complain "Invalid argument"; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 26 | push @{$self->_cookies}, @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 26 | return; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 30 | ▶ | sub all { | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 30 | return @{$_[0]->_cookies}; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | return scalar @{$_[0]->_cookies}; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 43 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 43 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 43 | ▶ | return if ! @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 43 | my $name = shift; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 43 | ▶ | if ( wantarray ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 24 | ▶ | if ( defined $name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 20 | ▶ | return grep { defined $_->name && $_->name eq $name } @{$self->_cookies}; | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $_->name && $_->name eq $name
MC/DC50%1/2
defined $_->name && $_->name eq $name
defined $_->name
$_->name eq $name
Truth table66%2/3
A && B
A= defined $_->nameB= $_->name eq $name
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | 4 | return grep { ! defined $_->name } @{$self->_cookies}; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 19 | ▶ | if ( defined $name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 15 | ▶ | defined $_->name && $_->name eq $name && return $_ for @{$self->_cookies}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_->name and $_->name eq $name
MC/DC50%1/2
defined $_->name and $_->name eq $name
defined $_->name
$_->name eq $name
Truth table66%2/3
A and B
A= defined $_->nameB= $_->name eq $name
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||
| 96 | 4 | ▶ | defined $_->name or return $_ for @{$self->_cookies}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 6 | return undef; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 6 | ▶ | sub names { | ||||||||||||||||||||||||||
|
Subroutine
names: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 6 | my @names; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | my %seen; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | for my $cookie ( @{$self->_cookies} ) { | |||||||||||||||||||||||||||
| 114 | 14 | my $name = $cookie->name; | |||||||||||||||||||||||||||
| 115 | 14 | ▶ | next if ! defined $name || $seen{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $name or $seen{$name}
MC/DC100%2/2
not defined $name or $seen{$name}
not defined $name
$seen{$name}
Truth table100%3/3
not(A) or B
A= not defined $nameB= $seen{$name}
| |||||||||||||||||||||||||||||
| 116 | 7 | push @names, $name; | |||||||||||||||||||||||||||
| 117 | 7 | $seen{$name} = 1; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | return @names; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 14 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 14 | my $cookies = $self->_cookies; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 14 | my $count = @$cookies; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 14 | for my $name ( @_ ) { | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 19 | ▶ | if ( defined $name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 17 | ▶ | @$cookies = grep { ! defined $_->name || $_->name ne $name } @$cookies; | ||||||||||||||||||||||||||
|
Condition66%2/3
!defined($_->name) || $_->name ne $name
MC/DC50%1/2
!defined($_->name) || $_->name ne $name
!defined($_->name)
$_->name ne $name
Truth table66%2/3
not(A) || B
A= !defined($_->name)B= $_->name ne $name
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||
| 138 | 2 | @$cookies = grep { defined $_->name } @$cookies; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 14 | return $count - @$cookies; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 7 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 7 | for ( @_ ) { | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 10 | ▶ | if ( ! blessed $_ || ! $_->isa( $self->_cookie_class ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($_) and $_->isa($self->_cookie_class)
MC/DC0%0/2
builtin::blessed($_) and $_->isa($self->_cookie_class)
builtin::blessed($_)
$_->isa($self->_cookie_class)
Truth table33%1/3
A and B
A= builtin::blessed($_)B= $_->isa($self->_cookie_class)
| |||||||||||||||||||||||||||||
| 154 | 0 | complain "Invalid argument"; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 7 | $self->remove( map { $_->name } @_ ); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 7 | push @{$self->_cookies}, @_; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 7 | return; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 169 | # Private methods | ||||||||||||||||||||||||||||
| 170 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub _cookie_class { | ||||||||||||||||||||||||||
|
Subroutine
_cookie_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | return 'GX::HTTP::Cookie'; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 19 | 1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||