Preferences/RolesFor/Result/PrfOwner.pm

filter: Statementstmt 10.7% 11 / 102 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 21.4% 3 / 14 Podpod 100% 9 / 9 totaltotal 13.8% 23 / 166 CC 20 SCAR 43.2
CC
20
Cov
8%
prf_set
156.0
prf_get
72.0
_clear_out_inactive_unique_values
6.0
CRAP 75.1
↑ Summary Preferences/RolesFor/ResultSet/PrfOwner.pm »
1  
2 package OpusVL::Preferences::RolesFor::Result::PrfOwner;
3  
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 0 ▶sub prf_id_column {'id'}
Subroutine prf_id_column: not called CC=1 SCAR=6.9
Pod documented
11  
12 sub prf_owner_init
13 {
14 0 ▶ my $class = shift;
Subroutine prf_owner_init: not called CC=1 SCAR=6.9
Pod documented
15  
16 0 $class->add_columns
17 (
18 prf_owner_type_id =>
19 {
20 data_type => 'integer',
21 is_nullable => 1,
22 is_foreign_key => 1
23 }
24 );
25  
26 0 $class->belongs_to
27 (
28 prf_owner => 'OpusVL::Preferences::Schema::Result::PrfOwner',
29 {
30 'foreign.prf_owner_id' => 'self.' . $class->prf_id_column,
31 'foreign.prf_owner_type_id' => 'self.prf_owner_type_id'
32 }
33 );
34  
35 0 $class->belongs_to
36 (
37 prf_owner_type => 'OpusVL::Preferences::Schema::Result::PrfOwnerType',
38 {
39 'foreign.prf_owner_type_id' => 'self.prf_owner_type_id'
40 }
41 );
42 }
43  
44 after insert => sub
45 {
46 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 my $schema = $self->result_source->schema;
48 0 my $type = $schema->resultset ('PrfOwnerType')->setup_from_source ($self->result_source);
49  
50 # Ensure that any auto-generated values have been populated (in case the
51 # prf_id column is not a primary key)
52 0 $self->discard_changes;
53 0 my $prf_id_column = $self->prf_id_column;
54 0 $schema->resultset('PrfOwner')->create
55 ({
56 prf_owner_id => $self->$prf_id_column,
57 prf_owner_type_id => $type->prf_owner_type_id
58 });
59
60 0 $self->update ({ prf_owner_type_id => $type->prf_owner_type_id });
61 1 };
62  
63 sub prf_defaults
64 {
65 0 ▶ my $self = shift;
Subroutine prf_defaults: not called CC=1 SCAR=6.9
Pod documented
66  
67 0 return $self->prf_owner_type->prf_defaults;
68 }
69  
70 sub prf_preferences
71 {
72 # this could maybe be achieved with a proper DBIx::Class relationship, but
73 # this will do for now
74  
75 0 ▶ my $self = shift;
Subroutine prf_preferences: not called CC=1 SCAR=6.9
Pod documented
76  
77 0 return $self->prf_owner->prf_preferences;
78 }
79  
80 sub preferences_to_array
81 {
82 0 ▶ my $self = shift;
Subroutine preferences_to_array: not called CC=1 SCAR=6.9
Pod documented
83  
84 0 my $preferences = $self->prf_preferences;
85 my @d = sort {
86 $a->{param}->display_order <=> $b->{param}->display_order
87 0 } map { {
88 0 name => $_->name,
89 value => $_->value,
90 param => $self->prf_defaults->find({ name => $_->name }),
91 } } $preferences->all;
92 0 return \@d;
93 }
94  
95 sub safe_preferences_in_array
96 {
97 0 ▶ my $self = shift;
Subroutine safe_preferences_in_array: not called CC=1 SCAR=6.9
Pod documented
98 0 my $extra_params = $self->preferences_to_array;
99 0 my @cleaned_up = map { {
100 name => $_->{name},
101 value => $_->{value},
102 label => $_->{param}->comment,
103 0 } } @$extra_params;
104 0 return \@cleaned_up;
105 }
106  
107 sub prf_get
108 {
109 0 ▶ my $self = shift;
Subroutine prf_get: not called CC=8 SCAR=42.8
Pod documented
110 0 my $name = shift;
111  
112 0 my $default = $self->prf_defaults->search ({ name => $name })->first;
113 0 ▶ die "Field $name not setup" unless $default;
Branch: 1 branch
BranchTrueFalseTotal
unless $default 0 0 0
114  
115 0 my $pref = $self->prf_preferences->search ({ name => $name })->first;
116 0 my $value;
117 0 ▶ $value = $pref->value if $pref;
Branch: 1 branch
BranchTrueFalseTotal
if $pref 0 0 0
118 0 ▶ if($default->encrypted)
Branch: 1 branch
BranchTrueFalseTotal
if ($default->encrypted) 0 0 0
119 {
120 0 ▶ if($pref)
Branch: 1 branch
BranchTrueFalseTotal
if ($pref) 0 0 0
121 {
122 0 my $schema = $self->result_source->schema;
123 0 my $crypto = $schema->encryption_client;
124 0 ▶ if($crypto)
Branch: 1 branch
BranchTrueFalseTotal
if ($crypto) 0 0 0
125 {
126 0 $value = $crypto->decrypt($value);
127 }
128 }
129 }
130 0 ▶ return $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
131  
132 # FIXME: should probably look at encrypting defaults,
133 # although, then again, do we need to?
134 0 ▶ return $default->default_value
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 0 0 0
135 if defined $default;
136  
137 0 return;
138 }
139  
140 sub _clear_out_inactive_unique_values
141 {
142 0 ▶ my $self = shift;
Subroutine _clear_out_inactive_unique_values: not called CC=2 SCAR=17.9
143 0 my $prefname = shift;
144 0 my $field = shift;
145  
146 0 my $schema = $self->result_source->schema;
147 0 my $obj_rs = $schema->resultset($self->prf_owner_type->owner_resultset);
148 0 ▶ if($obj_rs->can('inactive_for_unique_params'))
Branch: 1 branch
BranchTrueFalseTotal
if ($obj_rs->can("inactive_for_unique_params")) 0 0 0
149 {
150 0 my $rs = $obj_rs->inactive_for_unique_params;
151 0 $rs->search_related('prf_owner')->search_related('prf_preferences',
152 {
153 "prf_preferences.name" => $prefname,
154 "prf_preferences.prf_owner_type_id" => $field->prf_owner_type_id,
155 }
156 )->search_related('unique_value')->delete;
157 }
158 }
159  
160 sub prf_set
161 {
162 0 ▶ my $self = shift;
Subroutine prf_set: not called CC=12 SCAR=50.5
Pod documented
163 0 my $prefname = shift;
164 0 my $value = shift;
165  
166 0 my $allprefs = $self->prf_preferences;
167
168 0 my $pref = $allprefs->search ({ name => $prefname })->first;
169 0 my $field = $self->prf_defaults->search ({ name => $prefname })->first;
170 0 ▶ unless($field)
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 0 0 0
171 {
172 0 die "Field $prefname not setup.";
173 }
174  
175 0 ▶ if($field->encrypted)
Branch: 1 branch
BranchTrueFalseTotal
if ($field->encrypted) 0 0 0
176 {
177 0 my $schema = $self->result_source->schema;
178 0 my $crypto = $schema->encryption_client;
179  
180 # if we need to search or ensure unique values,
181 # then we have to use deterministic encryption
182 # which is less secure, but still encrypted.
183  
184 0 ▶ if($crypto)
Branch: 1 branch
BranchTrueFalseTotal
if ($crypto) 0 0 0
185 {
186 0 ▶ if($field->unique_field || $field->searchable)
Branch: 1 branch
BranchTrueFalseTotal
if ($field->unique_field or $field->searchable) { } 0 0 0
Condition0%0/3
$field->unique_field or $field->searchable
l!l&&r!l&&!r
000
MC/DC0%0/2
$field->unique_field or $field->searchable
$field->unique_field $field->searchable
Truth table0%0/3
A or B
A= $field->unique_fieldB= $field->searchable
ABresult
1X1
011
000
187 {
188 0 $value = $crypto->encrypt_deterministic($value);
189 }
190 else
191 {
192 0 $value = $crypto->encrypt($value);
193 }
194 }
195 }
196 0 ▶ if ($pref)
Branch: 1 branch
BranchTrueFalseTotal
if ($pref) { } 0 0 0
197 {
198 0 $pref->update ({ value => $value });
199  
200 0 ▶ if($field->unique_field)
Branch: 1 branch
BranchTrueFalseTotal
if ($field->unique_field) 0 0 0
201 {
202 0 $self->_clear_out_inactive_unique_values($prefname, $field);
203 0 my $unique_val = $pref->unique_value;
204 0 ▶ if($unique_val)
Branch: 1 branch
BranchTrueFalseTotal
if ($unique_val) { } 0 0 0
205 {
206 0 my $place_holder = $value;
207 0 ▶ if($field->data_type eq 'email')
Branch: 1 branch
BranchTrueFalseTotal
if ($field->data_type eq "email") 0 0 0
208 {
209 0 $place_holder = lc $value;
210 }
211 0 $unique_val->value($place_holder);
212 0 $unique_val->update;
213 }
214 else
215 {
216 0 $pref->create_related('unique_value', { value => $value });
217 }
218 }
219 }
220 else
221 {
222 0 my $data = {
223 name => $prefname,
224 value => $value
225 };
226 0 ▶ if($field->unique_field)
Branch: 1 branch
BranchTrueFalseTotal
if ($field->unique_field) 0 0 0
227 {
228 0 $self->_clear_out_inactive_unique_values($prefname, $field);
229 0 my $place_holder = $value;
230 0 ▶ if($field->data_type eq 'email')
Branch: 1 branch
BranchTrueFalseTotal
if ($field->data_type eq "email") 0 0 0
231 {
232 0 $place_holder = lc $value;
233 }
234 0 $data->{unique_value} = { value => $place_holder };
235 }
236 0 $allprefs->create($data);
237 }
238 }
239  
240 sub prf_reset
241 {
242 0 ▶ my $self = shift;
Subroutine prf_reset: not called CC=1 SCAR=6.9
Pod documented
243 0 my $name = shift;
244  
245 0 my $val = $self->prf_preferences->search ({ 'me.name' => $name });
246 0 $val->search_related('unique_value')->delete;
247 0 $val->delete;
248 }
249  
250 1 return 1;
251  
252 __END__
253  
254 =pod
255  
256 =encoding UTF-8
257  
258 =head1 NAME
259  
260 OpusVL::Preferences::RolesFor::Result::PrfOwner
261  
262 =head1 VERSION
263  
264 version 0.24
265  
266 =head1 SYNOPSIS
267  
268 =head1 DESCRIPTION
269  
270 If you are using DBIx::Class::Schema::Loader add the necessary link fields manually, otherwise
271 add the following line to add the fields to your result class.
272  
273 __PACKAGE__->prf_owner_init;
274  
275 =head1 METHODS
276  
277 =head2 prf_owner_init
278  
279 Tries to add the columns and relationships for your result class. Call it like this,
280  
281 __PACKAGE__->prf_owner_init;
282  
283 Your mileage may vary.
284  
285 =head2 prf_defaults
286  
287 ResultSet for the defaults.
288  
289 =head2 prf_preferences
290  
291 ResultSet of the preference values.
292  
293 =head2 prf_get
294  
295 Gets the setting. If the object doesn't have the setting specified but there is a
296 default, the default will be returned.
297  
298 =head2 prf_set
299  
300 Sets the setting for the object.
301  
302 =head2 prf_reset
303  
304 Resets the settings against the object. prf_get may still return a value if there is a default
305 for the setting.
306  
307 =head2 preferences_to_array
308  
309 Returns an array of the current results preferences.
310  
311 $object->preferences_to_array();
312 # [{
313 # name => $_->name,
314 # value => $_->value,
315 # param => # assocaited PrfDefault parameter definition.
316 # } ];
317  
318 =head2 safe_preferences_in_array
319  
320 Returns the same as preferences_to_array but instead of the param object it returns the
321 field label. The safe refers to the fact that all the items in the hash are base types
322 and therefore are trivially serializable.
323  
324 =head1 COPYRIGHT and LICENSE
325  
326 Copyright (C) 2011 OpusVL
327  
328 This software is licensed according to the "IP Assignment Schedule" provided with the development project.
329  
330 =head2 prf_id_column
331  
332 Provides the default column that contains the preferences identifier.
333  
334 If your Result doesn't have a standard integer primary key called 'id', override
335 this with the name of another column that I<is> an identifying integer
336  
337 =head1 AUTHOR
338  
339 OpusVL - www.opusvl.com
340  
341 =head1 COPYRIGHT AND LICENSE
342  
343 This software is copyright (c) 2011 by OpusVL - www.opusvl.com.
344  
345 This is free software; you can redistribute it and/or modify it under
346 the same terms as the Perl 5 programming language system itself.
347  
348 =cut
↑ Summary Preferences/RolesFor/ResultSet/PrfOwner.pm »