blib/lib/Tags/HTML/Login/Register.pm

filter: Statementstmt 98.5% 68 / 69 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 97.8% 93 / 95 CC 7 SCAR 8.2
CC
7
Cov
98%
new
5.0
_process
2.0
_text
2.0
CRAP 2.3
↑ Summary
1 package Tags::HTML::Login::Register;
2  
3 5 ▶use base qw(Tags::HTML);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Class::Utils qw(set_params split_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use List::Util qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Tags::HTML::Messages;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 Readonly::Array our @FORM_METHODS => qw(post get);
14  
15 5 our $VERSION = 0.10;
16  
17 # Constructor.
18 22 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
19 22 my ($class, @params) = @_;
20  
21 # Create object.
22 22 my ($object_params_ar, $other_params_ar) = split_params(
23 ['css_register', 'form_method', 'lang', 'logo_image_url',
24 'text', 'width'], @params);
25 22 my $self = $class->SUPER::new(@{$other_params_ar});
26  
27 # CSS style for register box.
28 19 $self->{'css_register'} = 'form-register';
29  
30 # Form method.
31 19 $self->{'form_method'} = 'post';
32  
33 # Language.
34 19 $self->{'lang'} = 'eng';
35  
36 # Logo.
37 19 $self->{'logo_image_url'} = undef;
38  
39 # Language texts.
40 19 $self->{'text'} = {
41 'eng' => {
42 'password1_label' => 'Password #1',
43 'password2_label' => 'Password #2',
44 'register' => 'Register',
45 'submit' => 'Register',
46 'username_label' => 'User name',
47 },
48 };
49  
50 # Login box width.
51 19 $self->{'width'} = '300px';
52  
53 # Process params.
54 19 set_params($self, @{$object_params_ar});
55  
56 # Check form method.
57 19 ▶ if (none { $self->{'form_method'} eq $_ } @FORM_METHODS) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $self->{"form_method"} eq $_; } , @Tags::HTML::Login::Register::FORM_METHODS)) 1 18 19
58 1 err "Parameter 'form_method' has bad value.";
59 }
60  
61 # TODO Check lang.
62  
63 # Check text for lang
64 18 ▶ if (! defined $self->{'text'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"text"}) 1 17 18
65 1 err "Parameter 'text' is required.";
66 }
67 17 ▶ if (ref $self->{'text'} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"text"} ne "HASH") 1 16 17
68 1 err "Parameter 'text' must be a hash with language texts.";
69 }
70 16 ▶ if (! exists $self->{'text'}->{$self->{'lang'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"text"}{$self->{"lang"}}) 1 15 16
71 1 err "Texts for language '$self->{'lang'}' doesn't exist.";
72 }
73  
74 $self->{'_tags_messages'} = Tags::HTML::Messages->new(
75 'css' => $self->{'css'},
76 'flag_no_messages' => 0,
77 15 'tags' => $self->{'tags'},
78 );
79  
80 # Object.
81 15 return $self;
82 }
83  
84 # Process 'Tags'.
85 7 ▶sub _process {
Subroutine _process: called CC=2 SCAR=7.0
86 7 my ($self, $messages_ar) = @_;
87  
88 7 my $username_id = 'username';
89 7 my $password1_id = 'password1';
90 7 my $password2_id = 'password2';
91  
92 # Main content.
93 $self->{'tags'}->put(
94 ['b', 'form'],
95 ['a', 'class', $self->{'css_register'}],
96 7 ['a', 'method', $self->{'form_method'}],
97  
98 ['b', 'fieldset'],
99 ['b', 'legend'],
100 ['d', $self->_text('register')],
101 ['e', 'legend'],
102 );
103  
104 6 ▶ if (defined $self->{'logo_image_url'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"logo_image_url"}) 0 6 6
105 $self->{'tags'}->put(
106 ['b', 'div'],
107 ['a', 'class', 'logo'],
108 ['b', 'img'],
109 0 ['a', 'src', $self->{'logo_image_url'}],
110 ['a', 'alt', 'logo'],
111 ['e', 'img'],
112 ['e', 'div'],
113 );
114 }
115  
116 6 $self->{'tags'}->put(
117 ['b', 'p'],
118 ['b', 'label'],
119 ['a', 'for', $username_id],
120 ['d', $self->_text('username_label')],
121 ['e', 'label'],
122 ['b', 'input'],
123 ['a', 'type', 'text'],
124 ['a', 'name', $username_id],
125 ['a', 'id', $username_id],
126 ['a', 'autofocus', 'autofocus'],
127 ['e', 'input'],
128 ['e', 'p'],
129  
130 ['b', 'p'],
131 ['b', 'label'],
132 ['a', 'for', $password1_id],
133 ['d', $self->_text('password1_label')],
134 ['e', 'label'],
135 ['b', 'input'],
136 ['a', 'type', 'password'],
137 ['a', 'name', $password1_id],
138 ['a', 'id', $password1_id],
139 ['e', 'input'],
140 ['e', 'p'],
141  
142 ['b', 'p'],
143 ['b', 'label'],
144 ['a', 'for', $password2_id],
145 ['d', $self->_text('password2_label')],
146 ['e', 'label'],
147 ['b', 'input'],
148 ['a', 'type', 'password'],
149 ['a', 'name', $password2_id],
150 ['a', 'id', $password2_id],
151 ['e', 'input'],
152 ['e', 'p'],
153  
154 ['b', 'p'],
155 ['b', 'button'],
156 ['a', 'type', 'submit'],
157 ['a', 'name', 'register'],
158 ['a', 'value', 'register'],
159 ['d', $self->_text('submit')],
160 ['e', 'button'],
161 ['e', 'p'],
162  
163 ['e', 'fieldset'],
164 );
165  
166 6 $self->{'_tags_messages'}->process($messages_ar);
167  
168 3 $self->{'tags'}->put(
169 ['e', 'form'],
170 );
171  
172 3 return;
173 }
174  
175 # Process 'CSS::Struct'.
176 3 ▶sub _process_css {
Subroutine _process_css: called CC=1 SCAR=0.0
177 3 my ($self, $message_types_hr) = @_;
178  
179 $self->{'css'}->put(
180 ['s', '.'.$self->{'css_register'}],
181 ['d', 'width', $self->{'width'}],
182 ['d', 'background-color', '#f2f2f2'],
183 ['d', 'padding', '20px'],
184 ['d', 'border-radius', '5px'],
185 ['d', 'box-shadow', '0 0 10px rgba(0, 0, 0, 0.2)'],
186 ['e'],
187  
188 ['s', '.'.$self->{'css_register'}.' fieldset'],
189 ['d', 'border', 'none'],
190 ['d', 'padding', 0],
191 ['d', 'margin-bottom', '20px'],
192 ['e'],
193  
194 ['s', '.'.$self->{'css_register'}.' .logo'],
195 ['d', 'height', '5em'],
196 ['d', 'width', '100%'],
197 ['e'],
198  
199 ['s', '.'.$self->{'css_register'}.' img'],
200 ['d', 'margin', 'auto'],
201 ['d', 'display', 'block'],
202 ['d', 'max-width', '100%'],
203 ['d', 'max-height', '5em'],
204 ['e'],
205  
206 ['s', '.'.$self->{'css_register'}.' legend'],
207 ['d', 'font-weight', 'bold'],
208 ['d', 'margin-bottom', '10px'],
209 ['e'],
210  
211 ['s', '.'.$self->{'css_register'}.' p'],
212 ['d', 'margin', 0],
213 ['d', 'padding', '10px 0'],
214 ['e'],
215  
216 ['s', '.'.$self->{'css_register'}.' label'],
217 ['d', 'display', 'block'],
218 ['d', 'font-weight', 'bold'],
219 ['d', 'margin-bottom', '5px'],
220 ['e'],
221  
222 ['s', '.'.$self->{'css_register'}.' input[type="text"]'],
223 ['s', '.'.$self->{'css_register'}.' input[type="password"]'],
224 ['d', 'width', '100%'],
225 ['d', 'padding', '8px'],
226 ['d', 'border', '1px solid #ccc'],
227 ['d', 'border-radius', '3px'],
228 ['e'],
229  
230 ['s', '.'.$self->{'css_register'}.' button[type="submit"]'],
231 ['d', 'width', '100%'],
232 ['d', 'padding', '10px'],
233 ['d', 'background-color', '#4CAF50'],
234 ['d', 'color', '#fff'],
235 ['d', 'border', 'none'],
236 ['d', 'border-radius', '3px'],
237 ['d', 'cursor', 'pointer'],
238 ['e'],
239  
240 ['s', '.'.$self->{'css_register'}.' button[type="submit"]:hover'],
241 ['d', 'background-color', '#45a049'],
242 ['e'],
243  
244 3 ['s', '.'.$self->{'css_register'}.' .messages'],
245 ['d', 'text-align', 'center'],
246 ['e'],
247 );
248  
249 3 $self->{'_tags_messages'}->process_css($message_types_hr);
250  
251 2 return;
252 }
253  
254 31 ▶sub _text {
Subroutine _text: called CC=2 SCAR=6.9
255 31 my ($self, $key) = @_;
256  
257 31 ▶ if (! exists $self->{'text'}->{$self->{'lang'}}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"text"}{$self->{"lang"}}{$key}) 1 30 31
258 1 err "Text for lang '$self->{'lang'}' and key '$key' doesn't exist.";
259 }
260  
261 30 return $self->{'text'}->{$self->{'lang'}}->{$key};
262 }
263  
264  
265 5 1;
266  
267 __END__
↑ Summary