blib/lib/Catalyst/Plugin/MessageStack.pm

filter: Statementstmt 75.0% 63 / 84 Branchbran 52.6% 20 / 38 Conditioncond 51.2% 20 / 39 MC/DCmcdc 30.4% 7 / 23 Subroutinesub 100% 9 / 9 Podpod 50.0% 2 / 4 totaltotal 61.4% 121 / 197 CC 36 SCAR 31.0
CC
36
Cov
64%
message
33.4
dispatch
26.2
reset_messages
10.8
CRAP 22.2
↑ Summary
1 package Catalyst::Plugin::MessageStack;
2 {
3 3 $Catalyst::Plugin::MessageStack::VERSION = '0.03';
4 }
5  
6 # ABSTRACT: A Catalyst plugin for gracefully handling messaging (and more) that follows the Post/Redirect/Get pattern.
7  
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Message::Stack;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 15 ▶sub message {
Subroutine message: called CC=14 SCAR=35.1
Pod documented
17 15 my ( $c, $message ) = @_;
18 15 my $config = $c->config->{'Plugin::MessageStack'};
19 15 ▶ my $default = $config->{default_type} || 'success';
Condition50%1/2
$config->{'default_type'} || 'success'
l!l
015
MC/DC0%0/1
$config->{'default_type'} || 'success'
$config->{'default_type'}
Truth table50%1/2
A
A= $config->{'default_type'}
Aresult
11
00
20 15 ▶ my $stash_key = $config->{stash_key} || 'messages';
Condition100%2/2
$config->{'stash_key'} || 'messages'
l!l
105
MC/DC100%1/1
$config->{'stash_key'} || 'messages'
$config->{'stash_key'}
Truth table100%2/2
A
A= $config->{'stash_key'}
Aresult
11
00
21  
22 15 ▶ if ( not defined $c->stash->{$stash_key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $c->stash->{$stash_key}) { } 7 8 15
elsif (not builtin::blessed($c->stash->{$stash_key}) || $c->stash->{$stash_key}->isa('Message::Stack')) { } 0 8 8
23 7 ▶ if ( $config->{model} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'model'}) { } 0 7 7
24 0 $c->stash->{$stash_key} = $c->model($config->{model})->messages;
25 } else {
26 7 $c->stash->{$stash_key} = Message::Stack->new;
27 }
28 }
29 ▶ elsif ( not blessed $c->stash->{$stash_key} and
Condition33%1/3
builtin::blessed($c->stash->{$stash_key}) || $c->stash->{$stash_key}->isa('Message::Stack')
l!l&&r!l&&!r
800
MC/DC0%0/2
builtin::blessed($c->stash->{$stash_key}) || $c->stash->{$stash_key}->isa('Message::Stack')
builtin::blessed($c->stash->{$stash_key}) $c->stash->{$stash_key}->isa('Message::Stack')
Truth table33%1/3
A || B
A= builtin::blessed($c->stash->{$stash_key})B= $c->stash->{$stash_key}->isa('Message::Stack')
ABresult
1X1
011
000
30 not $c->stash->{$stash_key}->isa('Message::Stack') )
31 {
32 0 $c->log->error("Unable to add messages into the stash, the stash has data at $stash_key already, and it isn't a Message::Stack");
33 0 return;
34 }
35 15 my $stash = $c->stash->{$stash_key};
36  
37 15 ▶ return $stash unless $message;
Branch: 1 branch
BranchTrueFalseTotal
unless $message 7 8 15
38  
39 8 ▶ if ( blessed $message ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($message)) { } 0 8 8
40 0 $stash->add($message);
41 } else {
42 8 my $s = { scope => 'global' };
43 8 ▶ if ( ref $message ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message) { } 0 8 8
44 0 ▶ $s->{level} = $message->{type} || $default;
Condition0%0/3
$message->{'type'} || $default
l!l&&r!l&&!r
000
MC/DC0%0/2
$message->{'type'} || $default
$message->{'type'} $default
Truth table0%0/3
A || B
A= $message->{'type'}B= $default
ABresult
1X1
011
000
45 0 $s->{id} = $message->{message};
46 0 ▶ $s->{scope} = $message->{scope} || 'global';
Condition0%0/2
$message->{'scope'} || 'global'
l!l
00
MC/DC0%0/1
$message->{'scope'} || 'global'
$message->{'scope'}
Truth table0%0/2
A
A= $message->{'scope'}
Aresult
11
00
47 0 ▶ $s->{subject} = $message->{subject} if($message->{subject});
Branch: 1 branch
BranchTrueFalseTotal
if $message->{"subject"} 0 0 0
48 0 ▶ $s->{params} = $message->{params} if($message->{params});
Branch: 1 branch
BranchTrueFalseTotal
if $message->{"params"} 0 0 0
49 } else {
50 8 $s->{level} = $default;
51 8 $s->{id} = $message;
52 }
53 8 $stash->add($s);
54 }
55  
56 8 $c->stash->{$stash_key} = $stash;
57  
58 8 return $stash;
59 }
60  
61  
62 8 ▶sub has_messages {
Subroutine has_messages: called CC=4 SCAR=14.0
Pod documented
63 8 my ( $c, $scope ) = @_;
64  
65 8 ▶ my $stash_key = $c->config->{'Plugin::MessageStack'}->{stash_key} || 'messages';
Condition100%2/2
$c->config->{'Plugin::MessageStack'}{'stash_key'} || 'messages'
l!l
53
MC/DC100%1/1
$c->config->{'Plugin::MessageStack'}{'stash_key'} || 'messages'
$c->config->{'Plugin::MessageStack'}{'stash_key'}
Truth table100%2/2
A
A= $c->config->{'Plugin::MessageStack'}{'stash_key'}
Aresult
11
00
66 8 my $stack = $c->stash->{$stash_key};
67 8 ▶ return 0 unless defined $stack;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stack 1 7 8
68  
69 7 ▶ if ( $scope ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope) 0 7 7
70 0 return $stack->for_scope($scope)->has_messages;
71 }
72 7 return $stack->has_messages;
73 }
74  
75 1 ▶sub reset_messages {
Subroutine reset_messages: called CC=4 SCAR=23.7
Pod undocumented
76 1 my ( $c, $scope ) = @_;
77  
78 1 ▶ my $stash_key = $c->config->{'Plugin::MessageStack'}->{stash_key} || 'messages';
Condition50%1/2
$c->config->{'Plugin::MessageStack'}{'stash_key'} || 'messages'
l!l
01
MC/DC0%0/1
$c->config->{'Plugin::MessageStack'}{'stash_key'} || 'messages'
$c->config->{'Plugin::MessageStack'}{'stash_key'}
Truth table50%1/2
A
A= $c->config->{'Plugin::MessageStack'}{'stash_key'}
Aresult
11
00
79 1 my $stack = $c->stash->{$stash_key};
80 0 ▶ return 0 unless defined $stack;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stack 0 0 0
81  
82 0 my $count = $stack->count;
83 0 ▶ if ( $scope ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope) { } 0 0 0
84 0 $count = $stack->for_scope($scope)->count;
85 0 $stack->reset_scope($scope);
86 } else {
87 0 $stack->reset;
88 }
89 0 return $count;
90  
91 }
92  
93 22 ▶sub dispatch {
Subroutine dispatch: called CC=17 SCAR=32.6
Pod undocumented
94 22 my $c = shift;
95  
96 22 my $config = $c->config->{'Plugin::MessageStack'};
97 22 ▶ my $stash_key = $config->{stash_key} || 'messages';
Condition100%2/2
$config->{'stash_key'} || 'messages'
l!l
148
MC/DC100%1/1
$config->{'stash_key'} || 'messages'
$config->{'stash_key'}
Truth table100%2/2
A
A= $config->{'stash_key'}
Aresult
11
00
98 22 ▶ my $flash_key = $config->{flash_key} || '_messages';
Condition100%2/2
$config->{'flash_key'} || '_messages'
l!l
1111
MC/DC100%1/1
$config->{'flash_key'} || '_messages'
$config->{'flash_key'}
Truth table100%2/2
A
A= $config->{'flash_key'}
Aresult
11
00
99 22 ▶ my $rflash_key = $config->{results_flash_key} || '_results';
Condition100%2/2
$config->{'results_flash_key'} || '_results'
l!l
814
MC/DC100%1/1
$config->{'results_flash_key'} || '_results'
$config->{'results_flash_key'}
Truth table100%2/2
A
A= $config->{'results_flash_key'}
Aresult
11
00
100 22 ▶ my $rstash_key = $config->{results_stash_key} || 'results';
Condition100%2/2
$config->{'results_stash_key'} || 'results'
l!l
517
MC/DC100%1/1
$config->{'results_stash_key'} || 'results'
$config->{'results_stash_key'}
Truth table100%2/2
A
A= $config->{'results_stash_key'}
Aresult
11
00
101  
102 # Copy to the stash
103 22 ▶ if ( $c->can('flash') and $c->flash->{$flash_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("flash") and $c->flash->{$flash_key}) 9 13 22
Condition66%2/3
$c->can("flash") and $c->flash->{$flash_key}
!ll&&!rl&&r
0139
MC/DC50%1/2
$c->can("flash") and $c->flash->{$flash_key}
$c->can("flash") $c->flash->{$flash_key}
Truth table66%2/3
A and B
A= $c->can("flash")B= $c->flash->{$flash_key}
ABresult
0X0
100
111
104 9 $c->stash->{$stash_key} = delete $c->flash->{$flash_key};
105 }
106  
107 22 ▶ if ( $c->can('flash') and $c->flash->{$rflash_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->can("flash") and $c->flash->{$rflash_key}) 0 22 22
Condition33%1/3
$c->can("flash") and $c->flash->{$rflash_key}
!ll&&!rl&&r
0220
MC/DC0%0/2
$c->can("flash") and $c->flash->{$rflash_key}
$c->can("flash") $c->flash->{$rflash_key}
Truth table33%1/3
A and B
A= $c->can("flash")B= $c->flash->{$rflash_key}
ABresult
0X0
100
111
108 0 $c->stash->{$rstash_key} = delete $c->flash->{$rflash_key};
109 }
110  
111  
112 22 my $ret = $c->next::method(@_);
113  
114 22 ▶ return $ret unless defined $c->res->location;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c->res->location 13 9 22
115  
116 # Redirect?
117
118 9 my $messages = $c->stash->{$stash_key};
119  
120 # No messages in stash, but check if we have something in the Data::Manager
121 # model.
122 9 ▶ if ( not $messages and $config->{model} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $messages and $config->{"model"}) 0 9 9
Condition33%1/3
not $messages and $config->{"model"}
!ll&&!rl&&r
900
MC/DC0%0/2
not $messages and $config->{"model"}
not $messages $config->{"model"}
Truth table33%1/3
not(A) and B
A= not $messagesB= $config->{"model"}
ABresult
0X0
100
111
123 0 $messages = $c->model($config->{model})->messages;
124 }
125  
126 9 ▶ return $ret unless defined $messages;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $messages 0 9 9
127  
128 9 ▶ if ( $messages->has_messages and $c->response->location) {
Branch: 1 branch
BranchTrueFalseTotal
if ($messages->has_messages and $c->response->location) 9 0 9
Condition33%1/3
$messages->has_messages and $c->response->location
!ll&&!rl&&r
009
MC/DC0%0/2
$messages->has_messages and $c->response->location
$messages->has_messages $c->response->location
Truth table33%1/3
A and B
A= $messages->has_messagesB= $c->response->location
ABresult
0X0
100
111
129 9 $c->flash->{$flash_key} = $messages;
130 9 $c->keep_flash($flash_key);
131 9 ▶ if ( $config->{model} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"model"}) 0 9 9
132 0 ▶ $c->flash->{$rflash_key} ||= $c->model($config->{model})->results;
Condition0%0/3
$c->flash->{$rflash_key} ||= $c->model($config->{"model"})->results
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->flash->{$rflash_key} ||= $c->model($config->{"model"})->results
$c->flash->{$rflash_key} $c->model($config->{"model"})->results
Truth table0%0/3
A ||= B
A= $c->flash->{$rflash_key}B= $c->model($config->{"model"})->results
ABresult
1X1
011
000
133 0 $c->keep_flash($rflash_key);
134 }
135 }
136 9 return $ret;
137 }
138  
139 3 1;
140  
141 __END__
↑ Summary