FormValidator.pm

filter: Statementstmt 89.1% 74 / 83 Branchbran 66.6% 8 / 12 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 95.6% 22 / 23 Podpod 75.0% 3 / 4 totaltotal 88.0% 110 / 125 CC 8 SCAR 8.6
CC
8
Cov
87%
validate
6.0
__ANON__
6.0
__ANON__
2.0
CRAP 2.4
« FormValidator/Registry.pm ↑ Summary FormValidator/Validator/Same.pm »
1 package Dancer2::Plugin::FormValidator;
2  
3 10 ▶use 5.24.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Dancer2::Core::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Dancer2::Plugin::FormValidator::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Dancer2::Plugin::FormValidator::Factory::Extensions;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Dancer2::Plugin::FormValidator::Registry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Dancer2::Plugin::FormValidator::Input;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Dancer2::Plugin::FormValidator::Processor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Types::Standard qw(InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 our $VERSION = '1.03';
17  
18 10 plugin_keywords qw(validate validated errors);
19  
20 has validator_config => (
21 is => 'ro',
22 isa => InstanceOf['Dancer2::Plugin::FormValidator::Config'],
23 lazy => 1,
24 builder => sub {
25 6 ▶ return Dancer2::Plugin::FormValidator::Config->new(
Subroutine __ANON__: called CC=1 SCAR=0.0
26 config => $_[0]->config,
27 );
28 }
29 10 );
30  
31 has registry => (
32 is => 'ro',
33 isa => InstanceOf['Dancer2::Plugin::FormValidator::Registry'],
34 lazy => 1,
35 default => sub {
36 my $factory = Dancer2::Plugin::FormValidator::Factory::Extensions->new(
37 plugin => $_[0],
38 extensions => $_[0]->config->{extensions} // {},
39 10 ▶ );
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$_[0]->config->{"extensions"} // {}
l!l
19
MC/DC100%1/1
$_[0]->config->{"extensions"} // {}
$_[0]->config->{"extensions"}
Truth table100%2/2
A
A= $_[0]->config->{"extensions"}
Aresult
11
00
40  
41 10 return Dancer2::Plugin::FormValidator::Registry->new(
42 extensions => $factory->build,
43 );
44 }
45 10 );
46  
47 has plugin_deferred => (
48 is => 'ro',
49 isa => InstanceOf['Dancer2::Plugin::Deferred'],
50 lazy => 1,
51 builder => sub {
52 4 ▶ return $_[0]->app->with_plugin('Dancer2::Plugin::Deferred');
Subroutine _build_plugin_deferred: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
53 }
54 10 );
55  
56 # Var for saving last success validation valid input.
57 10 has valid => (
58 is => 'rwp',
59 clearer => 1,
60 );
61  
62 sub BUILD {
63 11 ▶ $_[0]->_register_hooks;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
64 11 return;
65 }
66  
67 sub validate {
68 11 ▶ my ($self, %args) = @_;
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
69  
70 # We need to unset value of this var (if there was something).
71 11 $self->clear_valid;
72  
73 # Arguments.
74 # Arguments.
75 11 my $profile = $args{profile};
76 11 my $input = $args{input};
77 11 my $lang = $args{lang};
78  
79 11 ▶ if (not defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input) 11 0 11
80 11 my $request = $self->app->request;
81  
82 11 ▶ if ($request->is_get) {
Branch: 2 branches
BranchTrueFalseTotal
if ($request->is_get) { } 1 10 11
elsif ($request->is_post) { } 10 0 10
83 1 $input = $request->query_parameters->as_hashref_mixed;
84 }
85 elsif($request->is_post) {
86 10 $input = $request->body_parameters->as_hashref_mixed;
87 }
88 }
89  
90 11 ▶ if (defined $lang) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lang) 1 10 11
91 1 $self->_validator_language($lang);
92 }
93  
94 11 my $processor = Dancer2::Plugin::FormValidator::Processor->new(
95 input => Dancer2::Plugin::FormValidator::Input->new(input => $input),
96 profile => $profile,
97 config => $self->validator_config,
98 registry => $self->registry,
99 );
100  
101 11 my $result = $processor->run;
102  
103 11 ▶ if ($result->success != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->success != 1) { } 8 3 11
104 8 $self->plugin_deferred->deferred(
105 $self->validator_config->session_namespace,
106 {
107 messages => $result->messages,
108 old => $input,
109 },
110 );
111 8 return undef;
112 }
113 else {
114 3 $self->_set_valid($result->valid);
115  
116 3 return $self->valid;
117 }
118 }
119  
120 sub validated {
121 2 ▶ return $_[0]->valid;
Subroutine validated: called CC=1 SCAR=0.0
Pod documented
122 }
123  
124 sub errors {
125 7 ▶ return $_[0]->_get_deferred->{messages};
Subroutine errors: called CC=1 SCAR=0.0
Pod documented
126 }
127  
128 # Register Dancer2 hook to add custom template tokens: errors, old.
129 sub _register_hooks {
130 11 ▶ my ($self) = @_;
Subroutine _register_hooks: called CC=1 SCAR=4.0
131  
132 $self->app->add_hook(
133 Dancer2::Core::Hook->new(
134 name => 'before_template_render',
135 code => sub {
136 0 ▶ my ($tokens) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
137  
138 0 my $errors = {};
139 0 my $old = {};
140  
141 0 ▶ if (my $deferred = $tokens->{deferred}->{$self->validator_config->session_namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $deferred = $tokens->{"deferred"}{$self->validator_config->session_namespace}) 0 0 0
142 0 $errors = delete $deferred->{messages};
143 0 $old = delete $deferred->{old};
144 }
145  
146 0 $tokens->{errors} = $errors;
147 0 $tokens->{old} = $old;
148  
149 0 return;
150 },
151 )
152 11 );
153  
154 11 return;
155 }
156  
157 # Set validator to language to $lang.
158 sub _validator_language {
159 1 ▶ my ($self, $lang) = @_;
Subroutine _validator_language: called CC=1 SCAR=0.0
160  
161 1 $self->validator_config->language($lang);
162 1 return;
163 }
164  
165 # Returned deferred message from session storage.
166 sub _get_deferred {
167 7 ▶ return $_[0]->plugin_deferred->deferred(
Subroutine _get_deferred: called CC=1 SCAR=0.0
168 $_[0]->validator_config->session_namespace
169 );
170 }
171  
172 10 1;
173  
174 __END__
« FormValidator/Registry.pm ↑ Summary FormValidator/Validator/Same.pm »