| 1 | 3 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Dancer2::Plugin::Deferred; | ||||||||||||||||||||||||||||
| 6 | 3 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||
| 7 | 3 | $Dancer2::Plugin::Deferred::VERSION = '0.008000'; | |||||||||||||||||||||||||||
| 8 | # ABSTRACT: Defer messages or data across redirections | ||||||||||||||||||||||||||||
| 9 | # VERSION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Dancer2::Core::Types qw/Str/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Dancer2::Plugin 0.200000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | has var_key => ( | ||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | isa => Str, | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | from_config => sub { 'dpdid' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | has var_keep_key => ( | ||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | isa => Str, | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | from_config => sub { 'dpd_keep' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 3 | ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | has params_key => ( | ||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | isa => Str, | ||||||||||||||||||||||||||||
| 32 | 2 | ▶ | from_config => sub { 'dpdid' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 3 | ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | has session_key_prefix => ( | ||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||
| 37 | isa => Str, | ||||||||||||||||||||||||||||
| 38 | 2 | ▶ | from_config => sub { 'dpd_' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 3 | ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | has template_key => ( | ||||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||
| 43 | isa => Str, | ||||||||||||||||||||||||||||
| 44 | 2 | ▶ | from_config => sub { 'deferred' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 3 | ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 3 | plugin_keywords 'deferred', 'all_deferred', 'deferred_param'; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | ▶ | sub deferred { | ||||||||||||||||||||||||||
|
Subroutine
deferred: called CC=5 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 3 | my ( $plugin, $key, $value ) = @_; | |||||||||||||||||||||||||||
| 51 | 3 | my $app = $plugin->app; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 3 | my $id = $plugin->_get_id; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # message data is flat "dpd_$id" to avoid race condition with | ||||||||||||||||||||||||||||
| 56 | # another session | ||||||||||||||||||||||||||||
| 57 | 3 | ▶ | my $data = $app->session->read( $plugin->session_key_prefix . $id ) || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$app->session->read($plugin->session_key_prefix . $id) || {}
MC/DC0%0/1
$app->session->read($plugin->session_key_prefix . $id) || {}
$app->session->read($plugin->session_key_prefix . $id)
Truth table50%1/2
A
A= $app->session->read($plugin->session_key_prefix . $id)
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # set value or destructively retrieve it | ||||||||||||||||||||||||||||
| 60 | 3 | ▶ | if ( defined $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 3 | $data->{$key} = $value; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||
| 64 | $value = | ||||||||||||||||||||||||||||
| 65 | $app->request->var( $plugin->var_keep_key ) | ||||||||||||||||||||||||||||
| 66 | ? $data->{$key} | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | : delete $data->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # store remaining data or clear it if no deferred messages are left | ||||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ( keys %$data ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 3 | $app->session->write( $plugin->session_key_prefix . $id => $data ); | |||||||||||||||||||||||||||
| 73 | 3 | $app->request->var( $plugin->var_key => $id ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 0 | $app->session->delete( $plugin->session_key_prefix . $id ); | |||||||||||||||||||||||||||
| 77 | 0 | $app->request->var( $plugin->var_key => undef ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 3 | return $value; | |||||||||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 9 | ▶ | sub all_deferred { | ||||||||||||||||||||||||||
|
Subroutine
all_deferred: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 9 | my $plugin = shift; | |||||||||||||||||||||||||||
| 84 | 9 | my $app = $plugin->app; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 9 | my $id = $plugin->_get_id; | |||||||||||||||||||||||||||
| 87 | 9 | ▶ | my $data = $plugin->app->session->read( $plugin->session_key_prefix . $id ) || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$plugin->app->session->read($plugin->session_key_prefix . $id) || {}
MC/DC100%1/1
$plugin->app->session->read($plugin->session_key_prefix . $id) || {}
$plugin->app->session->read($plugin->session_key_prefix . $id)
Truth table100%2/2
A
A= $plugin->app->session->read($plugin->session_key_prefix . $id)
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 9 | ▶ | unless ( $app->request->var( $plugin->var_keep_key ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 8 | $app->session->delete( $plugin->session_key_prefix . $id ); | |||||||||||||||||||||||||||
| 91 | 8 | $app->request->var( $plugin->var_key, undef ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 9 | return $data; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 3 | ▶ | sub deferred_param { | ||||||||||||||||||||||||||
|
Subroutine
deferred_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 3 | my $plugin = shift; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 3 | $plugin->app->request->var( $plugin->var_keep_key => 1 ); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 3 | return ( $plugin->params_key => $plugin->app->request->var( $plugin->var_key ) ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # not crypto strong, but will be stored in session, which should be | ||||||||||||||||||||||||||||
| 105 | 12 | ▶ | sub _get_id { | ||||||||||||||||||||||||||
|
Subroutine
_get_id: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 106 | 12 | my $plugin = shift; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 12 | ▶ | return $plugin->app->request->var( $plugin->var_key ) | ||||||||||||||||||||||||||
|
Condition66%2/3
$plugin->app->request->var($plugin->var_key) || sprintf('%08d', int rand 100000000)
MC/DC0%0/2
$plugin->app->request->var($plugin->var_key) || sprintf('%08d', int rand 100000000)
$plugin->app->request->var($plugin->var_key)
sprintf('%08d', int rand 100000000)
Truth table66%2/3
A || B
A= $plugin->app->request->var($plugin->var_key)B= sprintf('%08d', int rand 100000000)
| |||||||||||||||||||||||||||||
| 109 | || sprintf( "%08d", int( rand(100_000_000) ) ); | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 2 | my $plugin = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | $plugin->app->add_hook( | ||||||||||||||||||||||||||||
| 116 | Dancer2::Core::Hook->new( | ||||||||||||||||||||||||||||
| 117 | name => 'before_template', | ||||||||||||||||||||||||||||
| 118 | 9 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | 9 | my $data = shift; | |||||||||||||||||||||||||||
| 120 | 9 | $data->{$plugin->template_key} = $plugin->all_deferred; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | ) | ||||||||||||||||||||||||||||
| 123 | 2 | ); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | $plugin->app->add_hook( | ||||||||||||||||||||||||||||
| 126 | Dancer2::Core::Hook->new( | ||||||||||||||||||||||||||||
| 127 | name => 'before', | ||||||||||||||||||||||||||||
| 128 | 11 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 129 | 11 | my $id = $plugin->app->request->params->{ $plugin->params_key }; | |||||||||||||||||||||||||||
| 130 | 11 | ▶ | $plugin->app->request->var( $plugin->var_key => $id ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | if $id; | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | ) | ||||||||||||||||||||||||||||
| 134 | 2 | ); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | $plugin->app->add_hook( | ||||||||||||||||||||||||||||
| 137 | Dancer2::Core::Hook->new( | ||||||||||||||||||||||||||||
| 138 | name => 'after', | ||||||||||||||||||||||||||||
| 139 | 11 | ▶ | code => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 140 | 11 | my $response = shift; | |||||||||||||||||||||||||||
| 141 | 11 | ▶ | if ( $plugin->app->request->var( $plugin->var_key ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$plugin->app->request->var($plugin->var_key) and $response->status =~ /^3/
MC/DC100%2/2
$plugin->app->request->var($plugin->var_key) and $response->status =~ /^3/
$plugin->app->request->var($plugin->var_key)
$response->status =~ /^3/
Truth table100%3/3
A and B
A= $plugin->app->request->var($plugin->var_key)B= $response->status =~ /^3/
| |||||||||||||||||||||||||||||
| 142 | && $response->status =~ /^3/ ) | ||||||||||||||||||||||||||||
| 143 | { | ||||||||||||||||||||||||||||
| 144 | 2 | my $u = URI->new( $response->header("Location") ); | |||||||||||||||||||||||||||
| 145 | 2 | $u->query_param( $plugin->deferred_param ); | |||||||||||||||||||||||||||
| 146 | 2 | $response->header( "Location" => $u ); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | ) | ||||||||||||||||||||||||||||
| 150 | 2 | ); | |||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 3 | 1; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # vim: ts=4 sts=4 sw=4 et: | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||