| 1 | package Catalyst::Plugin::Session::Store::DBIC::Delegate; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use base qw/Class::Accessor::Fast/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp qw/carp/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.15'; # VERSION | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | __PACKAGE__->mk_accessors(qw/model id_field data_field _session_row _flash_row/); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Catalyst::Plugin::Session::Store::DBIC::Delegate - Delegates between the session and flash rows | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | This class delegates between two rows in your sessions table for a | ||||||||||||||||||||||||||||
| 20 | given session (session and flash). This is done for compatibility | ||||||||||||||||||||||||||||
| 21 | with L<Catalyst::Plugin::Session::Store::DBI>. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head2 session | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Return the session row for this delegate. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub session { | ||||||||||||||||||||||||||
|
Subroutine
session: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | my $row = $self->_session_row; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | unless ($row) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | $row = $self->_load_row($key); | |||||||||||||||||||||||||||
| 38 | 0 | $self->_session_row($row); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | return $row; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head2 flash | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Return the flash row for this delegate. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub flash { | ||||||||||||||||||||||||||
|
Subroutine
flash: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my $row = $self->_flash_row; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | unless ($row) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | $row = $self->_load_row($key); | |||||||||||||||||||||||||||
| 57 | 0 | $self->_flash_row($row); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | return $row; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 _load_row | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | Load the specified session or flash row from the database. This is a | ||||||||||||||||||||||||||||
| 66 | wrapper around L<DBIx::Class::ResultSet/find_or_create> to add support | ||||||||||||||||||||||||||||
| 67 | for transactions. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub _load_row { | ||||||||||||||||||||||||||
|
Subroutine
_load_row: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 72 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $load_sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | 0 | return $self->model->find_or_create({ $self->id_field => $key }) | |||||||||||||||||||||||||||
| 76 | 0 | }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | my $row; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (blessed $self->model and $self->model->can('result_source')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($self->model) and $self->model->can("result_source")
MC/DC0%0/2
builtin::blessed($self->model) and $self->model->can("result_source")
builtin::blessed($self->model)
$self->model->can("result_source")
Truth table0%0/3
A and B
A= builtin::blessed($self->model)B= $self->model->can("result_source")
| |||||||||||||||||||||||||||||
| 80 | 0 | $row = $self->model->result_source->schema->txn_do($load_sub); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||
| 83 | # Fallback for DBIx::Class::DB | ||||||||||||||||||||||||||||
| 84 | 0 | $row = $load_sub->(); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | return $row; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head2 expires | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Return the expires row for this delegate. As with | ||||||||||||||||||||||||||||
| 93 | L<Catalyst::Plugin::Session::Store::DBI>, this maps to the L</session> | ||||||||||||||||||||||||||||
| 94 | row. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub expires { | ||||||||||||||||||||||||||
|
Subroutine
expires: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | $key =~ s/^expires/session/; | |||||||||||||||||||||||||||
| 102 | 0 | $self->session($key); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head2 flush | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Update the session and flash data in the backend store. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub flush { | ||||||||||||||||||||||||||
|
Subroutine
flush: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | for (qw/_session_row _flash_row/) { | |||||||||||||||||||||||||||
| 115 | 0 | my $row = $self->$_; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | next unless $row; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Check the size if available to avoid silent truncation on e.g. MySQL | ||||||||||||||||||||||||||||
| 119 | 0 | my $data_field = $self->data_field; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if (my $size = $row->result_source->column_info($data_field)->{size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | my $total_size = length($row->$data_field); | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | carp "This session requires $total_size bytes of storage, but your database column '$data_field' can only store $size bytes. Storing this session may not be reliable; increase the size of your data field" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | if $total_size > $size; | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | $row->update if $row->in_storage; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | $self->_clear_instance_data; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 _clear_instance_data | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Remove any references held by the delegate. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub _clear_instance_data { | ||||||||||||||||||||||||||
|
Subroutine
_clear_instance_data: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 139 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | $self->id_field(undef); | |||||||||||||||||||||||||||
| 142 | 0 | $self->model(undef); | |||||||||||||||||||||||||||
| 143 | 0 | $self->_session_row(undef); | |||||||||||||||||||||||||||
| 144 | 0 | $self->_flash_row(undef); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 clear_session | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Deletes the session row for this delegate, forcing a re-fetch on the next access. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub clear_session { | ||||||||||||||||||||||||||
|
Subroutine
clear_session: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | $self->_session_row(undef); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 clear_flash | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Deletes the flash row for this delegate, forcing a re-fetch on the next access. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub clear_flash { | ||||||||||||||||||||||||||
|
Subroutine
clear_flash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | $self->_flash_row(undef); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Daniel Westermann-Clark E<lt>danieltwc@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Andrew Rodland E<lt>andrew@cleverdomain.orgE<gt> | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Copyright 2006-2008 the L</AUTHORS> as listed above. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 182 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 1 | 1; | |||||||||||||||||||||||||||