blib/lib/Dancer/Session/YAML.pm

filter: Statementstmt 97.2% 71 / 73 Branchbran 66.6% 16 / 24 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 17 / 17 Podpod 85.7% 6 / 7 totaltotal 89.3% 117 / 131 CC 15 SCAR 14.0
CC
15
Cov
89%
retrieve
7.8
init
7.3
destroy
2.0
CRAP 4.1
« lib/Dancer/Cookies.pm ↑ Summary lib/Dancer/Template/Abstract.pm »
1 package Dancer::Session::YAML;
2 4 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: YAML-file-based session backend for Dancer
4 4 $Dancer::Session::YAML::VERSION = '1.3522';
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use base 'Dancer::Session::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Dancer::ModuleLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Dancer::FileUtils qw(path atomic_write);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # static
17  
18 4 my %session_dir_initialized;
19  
20 3 ▶sub init {
Subroutine init: called CC=7 SCAR=19.8
Pod documented
21 3 my $self = shift;
22 3 $self->SUPER::init(@_);
23  
24 3 ▶ if (!keys %session_dir_initialized) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %session_dir_initialized) 2 1 3
25 2 ▶ raise core_session => "YAML is needed and is not installed"
Branch: 1 branch
BranchTrueFalseTotal
unless "Dancer::ModuleLoader"->load("YAML") 0 2 2
26 unless Dancer::ModuleLoader->load('YAML');
27 }
28  
29 # default value for session_dir
30 3 ▶ setting('session_dir' => path(setting('appdir'), 'sessions'))
Branch: 1 branch
BranchTrueFalseTotal
unless defined setting("session_dir") 0 3 3
31 if not defined setting('session_dir');
32  
33 3 my $session_dir = setting('session_dir');
34 3 ▶ if (! exists $session_dir_initialized{$session_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $session_dir_initialized{$session_dir}) 2 1 3
35 2 $session_dir_initialized{$session_dir} = 1;
36 # make sure session_dir exists
37 2 ▶ if (!-d $session_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $session_dir) 2 0 2
38 2 ▶ mkdir $session_dir
Branch: 1 branch
BranchTrueFalseTotal
mkdir $session_dir or &raise("core_session", "session_dir $session_dir cannot be created") 2 0 2
39 or raise core_session => "session_dir $session_dir cannot be created";
40 }
41 2 Dancer::Logger::core("session_dir : $session_dir");
42 }
43 }
44  
45 # create a new session and return the newborn object
46 # representing that session
47 3 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
48 3 my ($class) = @_;
49  
50 3 my $self = Dancer::Session::YAML->new;
51 3 $self->flush;
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=7 SCAR=20.5
Pod documented
63 6 my ($class, $id) = @_;
64  
65 6 ▶ unless( $id =~ /^[\da-z]+$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id =~ /^[\da-z]+$/i) 0 6 6
66 0 warn "session id '$id' contains illegal characters\n";
67 0 return;
68 }
69  
70 6 my $session_file = yaml_file($id);
71  
72 6 ▶ return unless defined $session_file && -f $session_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session_file and -f $session_file 2 4 6
Condition100%3/3
defined $session_file and -f $session_file
!ll&&!rl&&r
114
MC/DC100%2/2
defined $session_file and -f $session_file
defined $session_file -f $session_file
Truth table100%3/3
A and B
A= defined $session_fileB= -f $session_file
ABresult
0X0
100
111
73  
74 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
75 4 my $content = YAML::LoadFile($fh);
76 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
77  
78 4 ▶ return bless $content => ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
130
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
79 }
80  
81 # instance
82  
83 19 ▶sub yaml_file {
Subroutine yaml_file: called CC=2 SCAR=6.9
Pod undocumented
84 19 my $id = shift;
85  
86 # Untaint Session ID before using it in file actions
87 # required when running under Perl Taint mode
88 19 $id =~ m/^([\d]*)$/;
89 19 ▶ return unless $1;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 1 18 19
90 18 my $yaml_file = "$1.yml";
91  
92 18 return path(setting('session_dir'), $yaml_file);
93 }
94  
95 1 ▶sub destroy {
Subroutine destroy: called CC=2 SCAR=7.0
Pod documented
96 1 my ($self) = @_;
97 4 ▶ use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 Dancer::Logger::core(
99 "trying to remove session file: " . yaml_file($self->id));
100 1 ▶ unlink yaml_file($self->id) if -f yaml_file($self->id);
Branch: 1 branch
BranchTrueFalseTotal
if -f yaml_file($self->id) 1 0 1
101 }
102  
103 4 ▶sub flush {
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
104 4 my $self = shift;
105 4 my $session_file = yaml_file( $self->id );
106  
107 4 atomic_write( setting('session_dir'), yaml_file($self->id), YAML::Dump($self) );
108  
109 3 return $self;
110 }
111  
112 4 1;
113  
114 __END__
« lib/Dancer/Cookies.pm ↑ Summary lib/Dancer/Template/Abstract.pm »