Schema/ResultSet/Session.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 100% 64 / 64 CC 8 SCAR 16.1
CC
8
Cov
100%
expire
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.0
« Schema/Populate/StateLocale.pm ↑ Summary Schema/Result/User.pm »
1 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Interchange6::Schema::ResultSet::Session;
3  
4 =head1 NAME
5  
6 Interchange6::Schema::ResultSet::Session
7  
8 =cut
9  
10 =head1 SYNOPSIS
11  
12 Check for expired sessions and carts.
13  
14 =cut
15  
16 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Time::Duration::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use parent 'Interchange6::Schema::ResultSet';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head2 expire( $expires )
25  
26 Checks for expired sessions where I<$expires> is either in seconds or
27 human readable format. Example '2 hours'. If the cart user is
28 registered the session will be deleted but and the cart's session_id
29 will be made NULL. Anonymous sessions/carts will be deleted.
30  
31 =cut
32  
33 sub expire {
34 4 ▶ my ($self, $expires) = @_;
Subroutine expire: called CC=8 SCAR=20.8
Pod documented
35 4 my $schema = $self->result_source->schema;
36 4 my $dtf = $schema->storage->datetime_parser;
37 4 my $duration;
38  
39 # converting to seconds
40 4 ▶ if (defined $expires) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expires) 3 1 4
41 3 ▶ if ($expires =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires =~ /^\d+$/) { } 1 2 3
42 # already got seconds
43 1 $duration = $expires;
44 }
45 else {
46 2 $duration = parse_duration($expires);
47 }
48 }
49  
50 3 ▶ unless ($duration) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($duration) 1 2 3
51 1 die "$0: Session expiration not set.\n";
52 }
53  
54 2 my $max = DateTime->now->subtract( seconds => $duration );
55  
56 #find expired sessions
57 2 my $session = $self->search(
58 {
59 last_modified => { '<=' => $dtf->format_datetime($max) },
60 }
61 );
62  
63 2 ▶ while (my $session_rs = $session->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session_rs = $session->next) 3 2 5
64 # check for payment orders with this session
65 3 my $po_rs = $session_rs->search_related('payment_orders');
66 3 ▶ while (my $po = $po_rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $po = $po_rs->next) 2 3 5
67 2 $po->update({ sessions_id => undef });
68 }
69 # check for carts with this session
70 3 my $cart_rs = $session_rs->search_related('carts');
71 3 ▶ while (my $cart = $cart_rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cart = $cart_rs->next) 3 3 6
72 3 my $user_id = $cart->users_id;
73 # check for user
74 3 ▶ if ($user_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_id) { } 2 1 3
75 2 $cart->update({ sessions_id => undef });
76 } else {
77 1 $cart->delete;
78 }
79 }
80 3 $session_rs->delete;
81 }
82 }
83  
84  
85 2 1;
« Schema/Populate/StateLocale.pm ↑ Summary Schema/Result/User.pm »