PSGI/StateHandler.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 59 CC 4 SCAR 14.3
CC
4
Cov
0%
save
6.0
move
6.0
retrieve
6.0
CRAP 4.2
1 package Net::AS2::PSGI::StateHandler;
2  
3 use strict;
4 use warnings;
5 use autodie;
6 our $VERSION = '1.0'; # VERSION
7  
8 =head1 NAME
9  
10 Net::AS2::PSGI::FileHandler - Provides methods to handle files being sent and received
11  
12 =cut
13  
14 use parent 'Net::AS2::PSGI::File';
15  
16 =head1 METHODS
17  
18 =over 4
19  
20 =item $self->file( $dir, $ext )
21  
22 Return the state file in the given directory, <$dir>.
23  
24 An optional additional extension may be given, C<$ext>.
25  
26 =cut
27  
28 sub file {
29 my ($self, $dir, $ext) = @_;
30  
31 $ext //= '';
32  
33 return $self->SUPER::file($dir, '.state' . $ext);
34  
35 }
36  
37 =item $file = $self->save( $content, $dir, $ext )
38  
39 Write C<$content> to state file with extension C<$ext> in directory, C<$dir>.
40  
41 Returns the state file path.
42  
43 =cut
44  
45 sub save {
46 my ($self, $content, $dir, $ext) = @_;
47  
48 my $file = $self->file($dir, $ext);
49  
50 $self->logger(debug => "Overwriting state file $file") if -f $file;
51  
52 $self->write($file, $content);
53  
54 $self->logger(debug => "Wrote state file $file");
55  
56 return $file;
57 }
58  
59 =item $file = $self->move( $file, $dir, $ext, $text )
60  
61 Move C<$file> to directory, C<$dir>, adding an optional extension C<$ext>.
62  
63 Debuging level C<$text> is output to the logger, if defined.
64  
65 Returns the path the file was moved to.
66  
67 =cut
68  
69 sub move {
70 my ($self, $file, $dir, $ext, $text) = @_;
71  
72 $ext //= '';
73  
74 my $to = $self->file($dir, $ext);
75  
76 $self->logger(warn => "File already exists: $to $text") if -f $to;
77  
78 rename $file, $to;
79  
80 $self->logger(debug => "Moved state file to $to $text");
81  
82 return $to;
83 }
84  
85  
86 =item $content = $self->retrieve( $file, $text )
87  
88 Read state file, C<$file>, returning its content.
89  
90 Debuging level C<$text> is output to the logger, if defined.
91  
92 =cut
93  
94 sub retrieve {
95 my ($self, $file, $text) = @_;
96  
97 $self->logger(debug => "About to read " . (-r $file ? 'readable' : 'not readable') . " file $file");
98  
99 local $/ = undef;
100  
101 open my $fh, '<', $file;
102 my $content = scalar(<$fh>);
103 close $fh;
104  
105 $self->logger(debug => $text);
106  
107 return $content;
108 }
109  
110 =back
111  
112 =cut
113  
114 1;