blib/lib/Minima/Controller.pm

filter: Statementstmt 100% 124 / 124 Branchbran 100% 40 / 40 Conditioncond 100% 11 / 11 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 26 / 26 Podpod 100% 11 / 11 totaltotal 100% 218 / 218 CC 36 SCAR 17.8
CC
36
Cov
100%
trimmed_params
14.0
flash
8.0
_decode
6.0
CRAP 5.9
« blib/lib/Minima/Setup.pm ↑ Summary blib/lib/Minima/App.pm »
1 4 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use experimental 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 class Minima::Controller;
5  
6 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Minima::View::PlainText;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use constant k_FLASH => 'minima.flash';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 field $env :reader;
19 4 field $app :param :reader;
20 4 field $route :param :reader = {};
21  
22 4 field $request :reader;
23 4 field $response :reader;
24 4 field $params :reader;
25  
26 field $req_encoding;
27  
28 17 ▶ADJUST {
Subroutine __ANON__: called CC=2 SCAR=6.9
29 17 ▶ $env = $app->env // {};
Condition100%2/2
$app->env // {}
l!l
161
MC/DC100%1/1
$app->env // {}
$app->env
Truth table100%2/2
A
A= $app->env
Aresult
11
00
30  
31 17 $request = Plack::Request->new($env);
32 17 $response = Plack::Response->new(200);
33  
34 17 $params = $self->_get_request_parameters;
35 }
36  
37 6 ▶method before_action ($m) { }
Subroutine before_action: called CC=1 SCAR=0.0
Pod documented
38 6 ▶method after_action ($r) { }
Subroutine after_action: called CC=1 SCAR=0.0
Pod documented
39  
40 3 ▶method trimmed_params ($options = {})
Subroutine trimmed_params: called CC=14 SCAR=26.4
Pod documented
41 3 {
42 3 ▶ my $exclude = $options->{exclude} // [];
Condition100%2/2
$options->{"exclude"} // []
l!l
21
MC/DC100%1/1
$options->{"exclude"} // []
$options->{"exclude"}
Truth table100%2/2
A
A= $options->{"exclude"}
Aresult
11
00
43 3 my @f_params = $params->flatten;
44 3 my @params;
45  
46 3 for my ($k, $v) (@f_params) {
47 27 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 24 3 27
48 24 my $skip = 0;
49 24 for my $pat (@$exclude) {
50 23 ▶ if (ref $pat && reftype $pat eq 'REGEXP') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pat and builtin::reftype($pat) eq 'REGEXP') { } 7 16 23
Condition100%3/3
ref $pat and builtin::reftype($pat) eq "REGEXP"
!ll&&!rl&&r
887
MC/DC100%2/2
ref $pat and builtin::reftype($pat) eq "REGEXP"
ref $pat builtin::reftype($pat) eq "REGEXP"
Truth table100%3/3
A and B
A= ref $patB= builtin::reftype($pat) eq "REGEXP"
ABresult
0X0
100
111
51 7 ▶ if ($k =~ $pat) { $skip = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /$pat/u) 1 6 7
52 } else {
53 16 ▶ if ($k eq $pat) { $skip = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq $pat) 1 15 16
54 }
55 }
56 24 ▶ if (!$skip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip) 22 2 24
57 22 ▶ if (!ref $v) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $v) { } 13 9 22
elsif (ref $v eq ref []) { } 6 3 9
58 13 $v = trim $v;
59 } elsif (ref $v eq ref []) {
60 6 ▶ $v = [ map { defined $_ ? trim($_) : $_ } @$v ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 6 3 9
61 }
62 }
63 }
64 27 push @params, $k, $v;
65 }
66 3 return Hash::MultiValue->new(@params);
67 }
68  
69 method json_body
70 5 ▶{
Subroutine json_body: called CC=4 SCAR=13.9
Pod documented
71 5 ▶ my $c_type = $request->content_type // '';
Condition100%2/2
$request->content_type // ""
l!l
41
MC/DC100%1/1
$request->content_type // ""
$request->content_type
Truth table100%2/2
A
A= $request->content_type
Aresult
11
00
72 5 ▶ return undef unless $c_type =~ m|\Aapplication/json\b|i;
Branch: 1 branch
BranchTrueFalseTotal
unless $c_type =~ m[\Aapplication/json\b]iu 2 3 5
73  
74 3 my $body = $request->content;
75 3 ▶ return undef unless length $body;
Branch: 1 branch
BranchTrueFalseTotal
unless length $body 1 2 3
76  
77 2 my $data;
78  
79 2 try {
80 2 $data = decode_json($body);
81 } catch ($e) {
82 1 return undef;
83 }
84  
85 1 return $data;
86 }
87  
88 8 ▶method flash ($type = undef, $message = undef)
Subroutine flash: called CC=8 SCAR=20.8
Pod documented
89 8 {
90 8 my $session = $request->session;
91 8 my $options = $request->session_options;
92  
93 8 ▶ croak <<~M unless $session;
Branch: 1 branch
BranchTrueFalseTotal
unless $session 1 7 8
94 flash requires session middleware.
95 Please enable Plack::Middleware::Session.
96 M
97  
98 # pop
99 7 ▶ unless (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 3 4 7
100 3 ▶ return undef unless exists $session->{+k_FLASH};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $session->{"minima.flash"} 1 2 3
101 2 ▶ delete $options->{no_store} if $options;
Branch: 1 branch
BranchTrueFalseTotal
if $options 1 1 2
102 2 return delete $session->{+k_FLASH};
103 }
104  
105 # push
106 4 ▶ delete $options->{no_store} if $options;
Branch: 1 branch
BranchTrueFalseTotal
if $options 3 1 4
107 4 push $session->{+k_FLASH}->{$type}->@*, $message;
108 }
109  
110 method hello
111 5 ▶{
Subroutine hello: called CC=1 SCAR=0.0
Pod documented
112 5 $self->render(Minima::View::PlainText->new, "hello, world\n");
113 }
114  
115 method not_found
116 1 ▶{
Subroutine not_found: called CC=1 SCAR=0.0
Pod documented
117 1 $response->code(404);
118 1 $self->render(Minima::View::PlainText->new, "not found\n");
119 }
120  
121 1 ▶method redirect ($url, $code = 302)
Subroutine redirect: called CC=2 SCAR=6.9
Pod documented
122 {
123 1 $response->redirect($url, $code);
124 1 $response->finalize;
125 }
126  
127 9 ▶method render ($view, $data = {})
Subroutine render: called CC=2 SCAR=6.9
Pod documented
128 9 {
129 9 $response->body($view->render($data));
130 9 $view->prepare_response($response);
131  
132 9 $response->finalize;
133 }
134  
135 method print_env
136 2 ▶{
Subroutine print_env: called CC=4 SCAR=13.9
Pod documented
137 2 ▶ return $self->redirect('/') unless $app->development;
Branch: 1 branch
BranchTrueFalseTotal
unless $app->development 1 1 2
138  
139 1 my $max = 0;
140 1 for (map { length } keys %$env) {
141 5 ▶ $max = $_ if $_ > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $max 3 2 5
142 }
143  
144 $self->render(
145 Minima::View::PlainText->new,
146 join '', map {
147 1 sprintf "%*s => %s\n", -$max, $_, $env->{$_}
148 } sort keys %$env
149 );
150 }
151  
152 1 ▶method dd ($ref)
Subroutine dd: called CC=1 SCAR=0.0
Pod documented
153 {
154 1 my $dumper = Data::Dumper->new([ $ref ]);
155 1 $dumper->Terse(1);
156  
157 1 $self->render(
158 Minima::View::PlainText->new,
159 $dumper->Dump,
160 );
161 }
162  
163 method _get_request_parameters
164 17 ▶{
Subroutine _get_request_parameters: called CC=2 SCAR=6.9
165 17 ▶ $req_encoding = $app->config->{request_encoding} // 'UTF-8';
Condition100%2/2
$app->config->{"request_encoding"} // "UTF-8"
l!l
116
MC/DC100%1/1
$app->config->{"request_encoding"} // "UTF-8"
$app->config->{"request_encoding"}
Truth table100%2/2
A
A= $app->config->{"request_encoding"}
Aresult
11
00
166  
167 my @parameters = map {
168 17 $self->_decode($_)
169 } $request->parameters->flatten;
170  
171 16 $params = Hash::MultiValue->new(@parameters);
172 }
173  
174 28 ▶method _decode ($data)
Subroutine _decode: called CC=6 SCAR=17.9
175 {
176 28 ▶ if (ref $data eq ref {}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq ref {}) 1 27 28
177 1 my %encoded;
178 1 for my ($k, $v) (%$data) {
179 1 $encoded{ $self->_decode($k) } = $self->_decode($v);
180 }
181 1 return \%encoded;
182 }
183  
184 27 ▶ if (ref $data eq ref []) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq ref []) 2 25 27
185 2 my @encoded;
186 2 for my $v (@$data) {
187 3 push @encoded, $self->_decode($v);
188 }
189 2 return \@encoded;
190 }
191  
192 25 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 22 3 25
193 22 return decode($req_encoding, $data);
194 }
195  
196 3 undef;
197 }
198  
199 __END__
« blib/lib/Minima/Setup.pm ↑ Summary blib/lib/Minima/App.pm »