GX/HTTP/Body/Scalar.pm

filter: Statementstmt 96.0% 73 / 76 Branchbran 85.7% 36 / 42 Conditioncond 80.0% 12 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 88.8% 143 / 161 CC 28 SCAR 20.3
CC
28
Cov
91%
add
13.1
__initialize
8.2
open
7.1
CRAP 7.6
« GX/Callback/Hook/Queue.pm ↑ Summary GX/HTTP/Parser/Body/MultiPart.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/HTTP/Body/Scalar.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::HTTP::Body::Scalar;
7  
8 32 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 32 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # ----------------------------------------------------------------------------------------------------------------------
15 # Constants
16 # ----------------------------------------------------------------------------------------------------------------------
17  
18 32 ▶use constant BUFFER_SIZE => 8192;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 # ----------------------------------------------------------------------------------------------------------------------
22 # Class setup
23 # ----------------------------------------------------------------------------------------------------------------------
24  
25 32 ▶use GX::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 32 extends 'GX::HTTP::Body';
28  
29 has 'content' => (
30 isa => 'Scalar',
31 52 ▶ initializer => sub { \( my $string = '' ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 32 accessor => { type => 'get' }
33 );
34  
35 32 has 'readonly' => (
36 isa => 'Bool'
37 );
38  
39 32 build;
40  
41  
42 # ----------------------------------------------------------------------------------------------------------------------
43 # Constructor
44 # ----------------------------------------------------------------------------------------------------------------------
45  
46 72 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
47  
48 72 my $invocant = shift;
49  
50 72 ▶ return eval { $invocant->SUPER::new( ( @_ == 1 ) ? ( content => $_[0] ) : @_ ) } || complain $@;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 8 64 72
Condition66%2/3
eval { do { $invocant->SUPER::new(@_ == 1 ? ('content', $_[0]) : @_) } } || &complain($@)
l!l&&r!l&&!r
6804
MC/DC0%0/2
eval { do { $invocant->SUPER::new(@_ == 1 ? ('content', $_[0]) : @_) } } || &complain($@)
eval { do { $invocant->SUPER::new(@_ == 1 ? ('content', $_[0]) : @_) } } &complain($@)
Truth table33%1/3
A || B
A= eval { do { $invocant->SUPER::new(@_ == 1 ? ('content', $_[0]) : @_) } }B= &complain($@)
ABresult
1X1
011
000
51  
52 }
53  
54  
55 # ----------------------------------------------------------------------------------------------------------------------
56 # Public methods
57 # ----------------------------------------------------------------------------------------------------------------------
58  
59 44 ▶sub add {
Subroutine add: called CC=13 SCAR=25.8
Pod documented
60  
61 44 my $self = shift;
62  
63 44 ▶ if ( $self->readonly ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->readonly) 1 43 44
64 1 complain "Cannot add content (message body is readonly)";
65 }
66  
67 43 my $content = $self->content;
68  
69 43 for my $data ( @_ ) {
70  
71 43 ▶ next unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 42 43
72  
73 42 ▶ if ( ref $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) { } 8 34 42
74  
75 8 ▶ if ( blessed $data ) {
Branch: 4 branches
BranchTrueFalseTotal
if (builtin::blessed($data)) { } 2 6 8
elsif (ref $data eq 'GLOB') { } 2 4 6
elsif (ref $data eq 'SCALAR') { } 2 2 4
elsif (ref $data eq 'CODE') { } 2 0 2
76  
77 2 ▶ if ( $data->isa( 'IO::Handle' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa('IO::Handle')) { } 2 0 2
78  
79 2 ▶ while ( $data->read( my $buffer, BUFFER_SIZE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->read(my $buffer, 8192)) 2 2 4
80 2 $$content .= $buffer;
81 }
82  
83 }
84 else {
85 0 $$content .= "$data";
86 }
87  
88 }
89 elsif ( ref $data eq 'GLOB' ) {
90  
91 2 ▶ while ( $data->read( my $buffer, BUFFER_SIZE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->read(my $buffer, 8192)) 2 2 4
92 2 $$content .= $buffer;
93 }
94  
95 }
96 elsif ( ref $data eq 'SCALAR' ) {
97 2 $$content .= $$data;
98 }
99 elsif ( ref $data eq 'CODE' ) {
100 2 $$content .= $data->();
101 }
102 else {
103 0 $$content .= "$data";
104 }
105  
106 }
107 else {
108 34 $$content .= $data;
109 }
110  
111 }
112  
113 43 ▶ if ( ! utf8::downgrade( $$content, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::downgrade($$content, 1)) 2 41 43
114 2 complain "Invalid content";
115 }
116  
117 41 return;
118  
119 }
120  
121 25 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
122  
123 25 return ${$_[0]->content};
124  
125 }
126  
127 22 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
128  
129 22 return length ${$_[0]->content};
130  
131 }
132  
133 354 ▶sub open {
Subroutine open: called CC=7 SCAR=19.6
Pod documented
134  
135 354 my $self = shift;
136 354 my $mode = shift;
137  
138 354 ▶ if ( defined $mode ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) { } 7 347 354
139  
140 7 ▶ if ( $mode ne '>>' && $mode ne '>' && $mode ne '<' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode ne ">>" and $mode ne ">" and $mode ne "<") 0 7 7
Condition83%5/6
$mode ne ">>" and $mode ne ">" and $mode ne "<"
!ll&&!rl&&r
430
$mode ne ">>" and $mode ne ">"
!ll&&!rl&&r
133
MC/DC0%0/3
$mode ne ">>" and $mode ne ">" and $mode ne "<"
$mode ne ">>" $mode ne ">" $mode ne "<"
Truth table75%3/4
A and B and C
A= $mode ne ">>"B= $mode ne ">"C= $mode ne "<"
ABCresult
0XX0
10X0
1100
1111
141 0 complain "Invalid open mode";
142 }
143  
144 7 ▶ if ( $self->readonly && $mode ne '<' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->readonly and $mode ne "<") 2 5 7
Condition100%3/3
$self->readonly and $mode ne "<"
!ll&&!rl&&r
412
MC/DC100%2/2
$self->readonly and $mode ne "<"
$self->readonly $mode ne "<"
Truth table100%3/3
A and B
A= $self->readonlyB= $mode ne "<"
ABresult
0X0
100
111
145 2 complain "Cannot open in-memory file in \"$mode\" mode";
146 }
147  
148 }
149 else {
150 347 $mode = '<';
151 }
152  
153 352 return IO::File->new( $self->content, $mode );
154  
155 }
156  
157 21 ▶sub print_to {
Subroutine print_to: called CC=1 SCAR=0.0
Pod documented
158  
159 21 my $self = shift;
160 21 my $handle = shift;
161  
162 21 return $handle->print( ${$self->content} );
163  
164 }
165  
166  
167 # ----------------------------------------------------------------------------------------------------------------------
168 # Aliases
169 # ----------------------------------------------------------------------------------------------------------------------
170  
171 32 *print = \&add;
172  
173  
174 # ----------------------------------------------------------------------------------------------------------------------
175 # Internal methods
176 # ----------------------------------------------------------------------------------------------------------------------
177  
178 72 ▶sub __initialize {
Subroutine __initialize: called CC=8 SCAR=21.0
179  
180 72 my $self = shift;
181 72 my $args = shift;
182  
183 72 ▶ if ( exists $args->{'content'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"content"}) 16 56 72
184  
185 16 ▶ if ( defined $args->{'content'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"content"}) 16 0 16
186  
187 16 ▶ if ( ref $args->{'content'} eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args->{'content'} eq 'SCALAR') { } 12 4 16
elsif (not ref $args->{'content'}) { } 4 0 4
188 12 ▶ $self->{'content'} = $args->{'content'} if defined ${$args->{'content'}};
Branch: 1 branch
BranchTrueFalseTotal
if defined ${$$args{"content"};} 12 0 12
189 }
190 elsif ( ! ref $args->{'content'} ) {
191 4 ${$self->{'content'}} = $args->{'content'};
192 }
193  
194 }
195  
196 16 ▶ if ( ! $self->{'content'} || ! utf8::downgrade( ${$self->{'content'}}, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"content"} and utf8::downgrade(${$$self{"content"};}, 1)) 4 12 16
Condition66%2/3
$self->{"content"} and utf8::downgrade(${$$self{"content"};}, 1)
!ll&&!rl&&r
0412
MC/DC50%1/2
$self->{"content"} and utf8::downgrade(${$$self{"content"};}, 1)
$self->{"content"} utf8::downgrade(${$$self{"content"};}, 1)
Truth table66%2/3
A and B
A= $self->{"content"}B= utf8::downgrade(${$$self{"content"};}, 1)
ABresult
0X0
100
111
197 4 throw "Invalid argument (\"content\" must be a byte string)";
198 }
199  
200 }
201  
202 68 return;
203  
204 }
205  
206  
207 32 1;
208  
209 __END__
« GX/Callback/Hook/Queue.pm ↑ Summary GX/HTTP/Parser/Body/MultiPart.pm »