| 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 | 2 | ▶ | use mro 'c3'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | use DateTime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use Time::Duration::Parse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 2 | ▶ | use parent 'Interchange6::Schema::ResultSet'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | =head2 expire( $expires ) | ||||||||||
| 26 | |||||||||||
| 27 | Checks for expired sessions where I<$expires> is either in seconds or | ||||||||||
| 28 | human readable format. Example '2 hours'. If the cart user is | ||||||||||
| 29 | registered the session will be deleted but and the cart's session_id | ||||||||||
| 30 | will be made NULL. Anonymous sessions/carts will be deleted. | ||||||||||
| 31 | |||||||||||
| 32 | =cut | ||||||||||
| 33 | |||||||||||
| 34 | sub expire { | ||||||||||
| 35 | 4 | ▶ | my ($self, $expires) = @_; | ||||||||
|
Subroutine
expire: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||
| 36 | 4 | my $schema = $self->result_source->schema; | |||||||||
| 37 | 4 | my $dtf = $schema->storage->datetime_parser; | |||||||||
| 38 | 4 | my $duration; | |||||||||
| 39 | |||||||||||
| 40 | # converting to seconds | ||||||||||
| 41 | 4 | ▶ | if (defined $expires) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 3 | ▶ | if ($expires =~ /^\d+$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | # already got seconds | ||||||||||
| 44 | 1 | $duration = $expires; | |||||||||
| 45 | } | ||||||||||
| 46 | else { | ||||||||||
| 47 | 2 | $duration = parse_duration($expires); | |||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 3 | ▶ | unless ($duration) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 1 | die "$0: Session expiration not set.\n"; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 2 | my $max = DateTime->now->subtract( seconds => $duration ); | |||||||||
| 56 | |||||||||||
| 57 | #find expired sessions | ||||||||||
| 58 | 2 | my $session = $self->search( | |||||||||
| 59 | { | ||||||||||
| 60 | last_modified => { '<=' => $dtf->format_datetime($max) }, | ||||||||||
| 61 | } | ||||||||||
| 62 | ); | ||||||||||
| 63 | |||||||||||
| 64 | 2 | ▶ | while (my $session_rs = $session->next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | # check for payment orders with this session | ||||||||||
| 66 | 3 | my $po_rs = $session_rs->search_related('payment_orders'); | |||||||||
| 67 | 3 | ▶ | while (my $po = $po_rs->next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 2 | $po->update({ sessions_id => undef }); | |||||||||
| 69 | } | ||||||||||
| 70 | # check for carts with this session | ||||||||||
| 71 | 3 | my $cart_rs = $session_rs->search_related('carts'); | |||||||||
| 72 | 3 | ▶ | while (my $cart = $cart_rs->next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 3 | my $user_id = $cart->users_id; | |||||||||
| 74 | # check for user | ||||||||||
| 75 | 3 | ▶ | if ($user_id) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 2 | $cart->update({ sessions_id => undef }); | |||||||||
| 77 | } else { | ||||||||||
| 78 | 1 | $cart->delete; | |||||||||
| 79 | } | ||||||||||
| 80 | } | ||||||||||
| 81 | 3 | $session_rs->delete; | |||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | |||||||||||
| 86 | 2 | 1; | |||||||||