GX/HTTP/Upload.pm

filter: Statementstmt 94.5% 52 / 55 Branchbran 80.0% 16 / 20 Conditioncond 69.2% 9 / 13 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 86.2% 94 / 109 CC 16 SCAR 13.9
CC
16
Cov
88%
move
6.3
DESTROY
5.1
open
4.0
CRAP 4.0
« GX/Meta/Accessor.pm ↑ Summary GX/Response.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/HTTP/Upload.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::HTTP::Upload;
7  
8 21 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # ----------------------------------------------------------------------------------------------------------------------
15 # Class setup
16 # ----------------------------------------------------------------------------------------------------------------------
17  
18 21 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 21 has 'cleanup' => (
21 isa => 'Bool',
22 default => 1,
23 initialize => 1
24 );
25  
26 21 has 'file' => (
27 isa => 'Scalar'
28 );
29  
30 21 has 'filename' => (
31 isa => 'Scalar'
32 );
33  
34 21 has 'headers' => (
35 isa => 'Scalar'
36 );
37  
38 21 has 'name' => (
39 isa => 'Scalar'
40 );
41  
42 21 build;
43  
44  
45 # ----------------------------------------------------------------------------------------------------------------------
46 # Constructor
47 # ----------------------------------------------------------------------------------------------------------------------
48  
49 31 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
50  
51 31 my $invocant = shift;
52  
53 31 ▶ return eval { $invocant->SUPER::new( @_ == 1 ? ( file => $_[0] ) : @_ ) } || complain $@;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 3 28 31
Condition33%1/3
eval { do { $invocant->SUPER::new(@_ == 1 ? ('file', $_[0]) : @_) } } || &complain($@)
l!l&&r!l&&!r
3100
MC/DC0%0/2
eval { do { $invocant->SUPER::new(@_ == 1 ? ('file', $_[0]) : @_) } } || &complain($@)
eval { do { $invocant->SUPER::new(@_ == 1 ? ('file', $_[0]) : @_) } } &complain($@)
Truth table33%1/3
A || B
A= eval { do { $invocant->SUPER::new(@_ == 1 ? ('file', $_[0]) : @_) } }B= &complain($@)
ABresult
1X1
011
000
54  
55 }
56  
57  
58 # ----------------------------------------------------------------------------------------------------------------------
59 # Public methods
60 # ----------------------------------------------------------------------------------------------------------------------
61  
62 3 ▶sub move {
Subroutine move: called CC=6 SCAR=18.3
Pod documented
63  
64 3 my $self = shift;
65 3 my $destination = shift;
66 3 my $overwrite = shift;
67  
68 3 ▶ if ( ! defined $destination ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $destination) 0 3 3
69 0 complain "Missing argument";
70 }
71  
72 3 my $file = $self->file;
73  
74 3 ▶ if ( ! defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 2 3
75 1 complain "Undefined file";
76 }
77  
78 2 ▶ if ( -e $destination && ! $overwrite ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $destination and not $overwrite) 1 1 2
Condition66%2/3
-e $destination and not $overwrite
!ll&&!rl&&r
101
MC/DC50%1/2
-e $destination and not $overwrite
-e $destination not $overwrite
Truth table66%2/3
A and not(B)
A= -e $destinationB= not $overwrite
ABresult
0X0
100
111
79 1 complain "Cannot move file (\"$destination\" already exists)";
80 }
81  
82 1 ▶ if ( ! File::Copy::move( $file, $destination ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (File::Copy::move($file, $destination)) 0 1 1
83 0 GX::Exception->complain(
84 message => "Cannot move upload file",
85 subexception => $!
86 );
87 }
88  
89 1 $self->file( $destination );
90  
91 1 $self->cleanup( 0 );
92  
93 1 return 1;
94  
95 }
96  
97 9 ▶sub open {
Subroutine open: called CC=4 SCAR=13.9
Pod documented
98  
99 9 my $self = shift;
100 9 my $mode = shift;
101  
102 9 my $file = $self->file;
103  
104 9 ▶ if ( ! defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 8 9
105 1 complain "Undefined file";
106 }
107  
108 8 ▶ my $handle = eval { IO::File->new( $file, ( $mode // '<' ) ) };
Condition100%2/2
$mode // "<"
l!l
17
MC/DC100%1/1
$mode // "<"
$mode
Truth table100%2/2
A
A= $mode
Aresult
11
00
109  
110 8 ▶ if ( ! $handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handle) 0 8 8
111 0 GX::Exception->complain(
112 message => "Cannot open upload file",
113 subexception => $@
114 );
115 }
116  
117 8 return $handle;
118  
119 }
120  
121 9 ▶sub size {
Subroutine size: called CC=2 SCAR=6.9
Pod documented
122  
123 9 my $self = shift;
124  
125 9 ▶ return -s ( $self->file // return 0 );
Condition100%2/2
$self->file // (return 0)
l!l
81
MC/DC100%1/1
$self->file // (return 0)
$self->file
Truth table100%2/2
A
A= $self->file
Aresult
11
00
126  
127 }
128  
129  
130 # ----------------------------------------------------------------------------------------------------------------------
131 # Internal methods
132 # ----------------------------------------------------------------------------------------------------------------------
133  
134 31 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=16.2
135  
136 31 my $self = shift;
137  
138 31 ▶ if ( $self->cleanup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cleanup) 29 2 31
139  
140 29 my $file = $self->file;
141  
142 29 ▶ if ( defined $file && -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and -f $file) 10 19 29
Condition66%2/3
defined $file and -f $file
!ll&&!rl&&r
19010
MC/DC50%1/2
defined $file and -f $file
defined $file -f $file
Truth table66%2/3
A and B
A= defined $fileB= -f $file
ABresult
0X0
100
111
143 10 ▶ unlink $file or warn "Cannot unlink file: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or warn "Cannot unlink file: $!" 10 0 10
144 }
145  
146 }
147  
148 31 return;
149  
150 }
151  
152  
153 21 1;
154  
155 __END__
« GX/Meta/Accessor.pm ↑ Summary GX/Response.pm »