blib/lib/Dancer/Session/JSON.pm

filter: Statementstmt 100% 77 / 77 Branchbran 66.6% 16 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 93.5% 116 / 124 CC 13 SCAR 10.6
CC
13
Cov
92%
retrieve
5.1
init
5.0
flush
4.0
CRAP 2.9
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Dancer::Session::JSON;
4 # ABSTRACT: JSON session backend for Dancer
5 2 $Dancer::Session::JSON::VERSION = '0.002';
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base 'Dancer::Session::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Dancer::ModuleLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Dancer::FileUtils qw(path set_file_mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # static
17  
18 2 my %session_dir_initialized;
19 2 my $json = JSON->new;
20  
21 3 ▶sub init {
Subroutine init: called CC=5 SCAR=16.2
Pod documented
22 3 my $self = shift;
23  
24 3 $self->SUPER::init(@_);
25  
26 # default value for session_dir
27 3 ▶ defined setting('session_dir')
Branch: 1 branch
BranchTrueFalseTotal
defined setting("session_dir") or setting("session_dir", path(setting("appdir"), "sessions")) 2 1 3
28 or setting( session_dir => path( setting('appdir'), 'sessions' ) );
29  
30 3 my $session_dir = setting('session_dir');
31 3 ▶ if ( ! exists $session_dir_initialized{$session_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $session_dir_initialized{$session_dir}) 2 1 3
32 2 $session_dir_initialized{$session_dir} = 1;
33  
34 # make sure session_dir exists
35 2 ▶ if ( ! -d $session_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $session_dir) 2 0 2
36 2 ▶ mkdir $session_dir
Branch: 1 branch
BranchTrueFalseTotal
mkdir $session_dir or croak("session_dir $session_dir cannot be created") 2 0 2
37 or croak "session_dir $session_dir cannot be created";
38 }
39  
40 2 Dancer::Logger::core("session_dir: $session_dir");
41 }
42 }
43  
44 # create a new session and return the newborn object
45 # representing that session
46 3 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
47 3 my ($class) = @_;
48  
49 3 my $self = Dancer::Session::JSON->new;
50 3 $self->flush;
51  
52 2 return $self;
53 }
54  
55 # deletes the dir cache
56 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
57 1 my ($class) = @_;
58 1 %session_dir_initialized = ();
59 }
60  
61 # Return the session object corresponding to the given id
62 6 ▶sub retrieve {
Subroutine retrieve: called CC=5 SCAR=16.3
Pod documented
63 6 my $class = shift;
64 6 my $id = shift;
65 6 my $session_file = _json_file($id);
66  
67 6 ▶ return unless -f $session_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $session_file 2 4 6
68  
69 4 ▶ 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" 4 0 4
70 4 ▶ 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" 4 0 4
71 4 my $json_data = do { local $/ = undef; <$fh>; };
72 4 my $content = $json->decode($json_data);
73 4 ▶ close $fh or die "Can't close '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${session_file}': $!\n" 4 0 4
74  
75 4 return bless $content => 'Dancer::Session::JSON';
76 }
77  
78 # instance
79 12 ▶sub _json_file {
Subroutine _json_file: called CC=1 SCAR=0.0
80 12 my $id = shift;
81 12 return path( setting('session_dir'), "$id.json" );
82 }
83  
84 1 ▶sub destroy {
Subroutine destroy: called CC=2 SCAR=7.0
Pod documented
85 1 my ($self) = @_;
86  
87 1 my $file = _json_file( $self->id );
88 1 Dancer::Logger::core("trying to remove session file: $file");
89  
90 1 ▶ -f $file and unlink $file;
Branch: 1 branch
BranchTrueFalseTotal
-f $file and unlink $file 1 0 1
91 }
92  
93 4 ▶sub flush {
Subroutine flush: called CC=4 SCAR=13.9
Pod documented
94 4 my $self = shift;
95 4 my $session_file = _json_file( $self->id );
96  
97 4 ▶ 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" 3 1 4
98 3 ▶ 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" 3 0 3
99 3 set_file_mode($fh);
100 3 print {$fh} $json->allow_blessed->convert_blessed->encode(
101 3 +{ %{$self} }
102 );
103  
104 3 ▶ close $fh or die "Can't close '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${session_file}': $!\n" 3 0 3
105  
106 3 return $self;
107 }
108  
109 2 1;
110  
111 __END__
↑ Summary