blib/lib/Dancer2/Core/Role/SessionFactory/File.pm

filter: Statementstmt 83.5% 61 / 73 Branchbran 44.1% 15 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 1 totaltotal 69.5% 89 / 128 CC 19 SCAR 23.5
CC
19
Cov
69%
_sessions
42.0
_flush
6.6
_retrieve
5.2
CRAP 10.4
« blib/lib/Dancer2/CLI/Gen.pm ↑ Summary lib/Dancer2/Handler/AutoPage.pm »
1 package Dancer2::Core::Role::SessionFactory::File;
2 # ABSTRACT: Role for file-based session factories
3 6 $Dancer2::Core::Role::SessionFactory::File::VERSION = '2.3.0';
4 6 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 with 'Dancer2::Core::Role::SessionFactory';
6  
7 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Dancer2::FileUtils qw(escape_filename);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #--------------------------------------------------------------------------#
15 # Required by classes consuming this role
16 #--------------------------------------------------------------------------#
17  
18 6 requires '_suffix'; # '.yml', '.json', etc.
19 6 requires '_thaw_from_handle'; # given handle, return session 'data' field
20 6 requires '_freeze_to_handle'; # given handle and data, serialize it
21  
22  
23 #--------------------------------------------------------------------------#
24 # Attributes and methods
25 #--------------------------------------------------------------------------#
26  
27 has session_dir => (
28 is => 'ro',
29 isa => Str,
30 0 ▶ default => sub { Path::Tiny::path( '.', 'sessions' )->stringify },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 6 );
32  
33 6 has _session_dir_path => (
34 is => 'ro',
35 lazy => 1,
36 builder => '_build_session_dir_path',
37 init_arg => undef,
38 );
39  
40 4 ▶sub _build_session_dir_path {
Subroutine _build_session_dir_path: called CC=1 SCAR=0.0
41 4 my $self = shift;
42 4 return Path::Tiny::path( $self->session_dir );
43 }
44  
45 4 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.1
Pod undocumented
46 4 my $self = shift;
47  
48 4 ▶ if ( !$self->_session_dir_path->is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_session_dir_path->is_dir) 1 3 4
49 1 ▶ mkdir $self->session_dir
Branch: 1 branch
BranchTrueFalseTotal
mkdir $self->session_dir or croak('Unable to create session dir : ' . $self->session_dir . ' : ' . $!) 1 0 1
50 or croak "Unable to create session dir : "
51 . $self->session_dir . ' : '
52 . $!;
53 }
54 }
55  
56 0 ▶sub _sessions {
Subroutine _sessions: not called CC=6 SCAR=37.4
57 0 my ($self) = @_;
58 0 my $sessions = [];
59  
60 0 ▶ opendir( my $dh, $self->session_dir )
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->session_dir or croak('Unable to open directory ' . $self->session_dir . " : $!") 0 0 0
61 or croak "Unable to open directory " . $self->session_dir . " : $!";
62  
63 0 my $suffix = $self->_suffix;
64  
65 0 ▶ while ( my $file = readdir($dh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $dh)) 0 0 0
66 0 ▶ next if $file eq '.' || $file eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $file eq "." or $file eq ".." 0 0 0
Condition0%0/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table0%0/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
67 0 ▶ if ( $file =~ /(\w+)\Q$suffix\E/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /(\w+)\Q$suffix\E/) 0 0 0
68 0 push @{$sessions}, $1;
69 }
70 }
71 0 closedir($dh);
72  
73 0 return $sessions;
74 }
75  
76 17 ▶sub _retrieve {
Subroutine _retrieve: called CC=5 SCAR=16.4
77 17 my ( $self, $id ) = @_;
78 17 my $session_file = $self->_session_dir_path->child(
79 escape_filename($id) . $self->_suffix,
80 )->stringify;
81  
82 17 ▶ croak "Invalid session ID: $id" unless -f $session_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $session_file 1 16 17
83  
84 16 ▶ open my $fh, '+<', $session_file or die "Can't open '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<", $session_file or die "Can't open '${session_file}': $!\n" 16 0 16
85 16 ▶ flock $fh, LOCK_SH or die "Can't lock file '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 1 or die "Can't lock file '${session_file}': $!\n" 16 0 16
86 16 my $data = $self->_thaw_from_handle($fh);
87 16 ▶ close $fh or die "Can't close '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${session_file}': $!\n" 16 0 16
88  
89 16 return $data;
90 }
91  
92 1 ▶sub _change_id {
Subroutine _change_id: called CC=2 SCAR=7.0
93 1 my ($self, $old_id, $new_id) = @_;
94  
95 1 my $old_path = $self->_session_dir_path->child(
96 escape_filename($old_id) . $self->_suffix
97 )->stringify;
98  
99 1 ▶ return if !-f $old_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $old_path 0 1 1
100  
101 1 my $new_path = $self->_session_dir_path->child(
102 escape_filename($new_id) . $self->_suffix
103 )->stringify;
104  
105 1 File::Copy::move($old_path, $new_path);
106 }
107  
108 5 ▶sub _destroy {
Subroutine _destroy: called CC=2 SCAR=7.0
109 5 my ( $self, $id ) = @_;
110 5 my $session_file = $self->_session_dir_path->child(
111 escape_filename($id) . $self->_suffix
112 )->stringify;
113 5 ▶ return if !-f $session_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $session_file 0 5 5
114  
115 5 unlink $session_file;
116 }
117  
118 21 ▶sub _flush {
Subroutine _flush: called CC=6 SCAR=18.8
119 21 my ( $self, $id, $data ) = @_;
120 21 my $session_file = $self->_session_dir_path->child(
121 escape_filename($id) . $self->_suffix
122 )->stringify;
123  
124 21 ▶ open my $fh, '>', $session_file or die "Can't open '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $session_file or die "Can't open '${session_file}': $!\n" 21 0 21
125 21 ▶ flock $fh, LOCK_EX or die "Can't lock file '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or die "Can't lock file '${session_file}': $!\n" 21 0 21
126 21 ▶ seek $fh, 0, 0 or die "Can't seek in file '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or die "Can't seek in file '${session_file}': $!\n" 21 0 21
127 21 ▶ truncate $fh, 0 or die "Can't truncate file '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
truncate $fh, 0 or die "Can't truncate file '${session_file}': $!\n" 21 0 21
128 21 binmode $fh, ':encoding(UTF-8)';
129 21 $self->_freeze_to_handle( $fh, $data );
130 21 ▶ close $fh or die "Can't close '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${session_file}': $!\n" 21 0 21
131  
132 21 return $data;
133 }
134  
135 6 1;
136  
137 __END__
« blib/lib/Dancer2/CLI/Gen.pm ↑ Summary lib/Dancer2/Handler/AutoPage.pm »