blib/lib/DBIx/Class/AuditLog.pm

filter: Statementstmt 98.9% 98 / 99 Branchbran 86.0% 43 / 50 Conditioncond 83.3% 15 / 18 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 92.7% 178 / 192 CC 35 SCAR 22.1
CC
35
Cov
93%
update
18.1
_modify_audit_value
5.1
_store_changes
5.0
CRAP 9.1
↑ Summary blib/lib/DBIx/Class/Schema/AuditLog/Structure.pm »
1 package DBIx::Class::AuditLog;
2 12 $DBIx::Class::AuditLog::VERSION = '0.6.2';
3 12 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # local $DBIx::Class::AuditLog::enabled = 0;
9 # can be set to temporarily disable audit logging
10 12 our $enabled = 1;
11  
12 sub insert {
13 28 ▶ my $self = shift;
Subroutine insert: called CC=4 SCAR=14.0
Pod documented
14  
15 28 ▶ return $self->next::method(@_) if !$enabled || $self->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
if not $DBIx::Class::AuditLog::enabled or $self->in_storage 2 26 28
Condition66%2/3
not $DBIx::Class::AuditLog::enabled or $self->in_storage
l!l&&r!l&&!r
2026
MC/DC50%1/2
not $DBIx::Class::AuditLog::enabled or $self->in_storage
not $DBIx::Class::AuditLog::enabled $self->in_storage
Truth table66%2/3
not(A) or B
A= not $DBIx::Class::AuditLog::enabledB= $self->in_storage
ABresult
1X1
011
000
16  
17 26 my $result = $self->next::method(@_);
18  
19 26 my ( $action, $table ) = $self->_action_setup( $result, 'insert' );
20  
21 26 ▶ if ($action) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action) 26 0 26
22 26 my %column_data = $result->get_columns;
23 26 $self->_store_changes( $action, $table, {}, \%column_data );
24 }
25  
26 26 return $result;
27 }
28  
29 sub update {
30 17 ▶ my $self = shift;
Subroutine update: called CC=18 SCAR=29.0
Pod documented
31  
32 17 ▶ return $self->next::method(@_) if !$enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $DBIx::Class::AuditLog::enabled 2 15 17
33  
34 15 my $stored_row = $self->get_from_storage;
35 15 my %new_data = $self->get_columns;
36 15 ▶ my @changed_columns = keys %{ $_[0] || {} };
Branch: 1 branch
BranchTrueFalseTotal
$_[0] or {} 8 7 15
37  
38 15 my $result = $self->next::method(@_);
39  
40 15 ▶ return unless $stored_row; # update on deleted row - nothing to log
Branch: 1 branch
BranchTrueFalseTotal
unless $stored_row 1 14 15
41  
42 14 my %old_data = $stored_row->get_columns;
43  
44 14 ▶ if (@changed_columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@changed_columns) 7 7 14
45 7 @new_data{@changed_columns} = map $self->get_column($_),
46 @changed_columns;
47 }
48  
49 14 foreach my $col ( $self->columns ) {
50 46 ▶ if ( $self->_force_audit($col) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_force_audit($col)) 2 44 46
51 $old_data{$col} = $stored_row->get_column($col)
52 2 ▶ unless defined $old_data{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_data{$col} 1 1 2
53 $new_data{$col} = $self->get_column($col)
54 2 ▶ unless defined $new_data{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_data{$col} 1 1 2
55 }
56 }
57  
58 # remove unwanted columns
59 14 foreach my $key ( keys %new_data ) {
60 46 ▶ next if $self->_force_audit($key); # skip forced cols
Branch: 1 branch
BranchTrueFalseTotal
if $self->_force_audit($key) 2 44 46
61 44 ▶ if ( defined $old_data{$key}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_data{$key} and defined $new_data{$key} and $old_data{$key} eq $new_data{$key} or not defined $old_data{$key} || defined $new_data{$key}) 25 19 44
Condition83%10/12
defined $old_data{$key} and defined $new_data{$key} and $old_data{$key} eq $new_data{$key} or not defined $old_data{$key} || defined $new_data{$key}
l!l&&r!l&&!r
24119
defined $old_data{$key} and defined $new_data{$key} and $old_data{$key} eq $new_data{$key}
!ll&&!rl&&r
11924
defined $old_data{$key} and defined $new_data{$key}
!ll&&!rl&&r
1043
defined $old_data{$key} || defined $new_data{$key}
l!l&&r!l&&!r
1901
MC/DC60%3/5
defined $old_data{$key} and defined $new_data{$key} and $old_data{$key} eq $new_data{$key} or not defined $old_data{$key} || defined $new_data{$key}
defined $old_data{$key} defined $new_data{$key} $old_data{$key} eq $new_data{$key} defined $old_data{$key} defined $new_data{$key}
Truth table30%3/10
A and B and C or not(D || E)
A= defined $old_data{$key}B= defined $new_data{$key}C= $old_data{$key} eq $new_data{$key}D= defined $old_data{$key}E= defined $new_data{$key}
ABCDEresult
111XX1
0XX001
10X001
110001
0XX1X0
0XX010
10X1X0
10X010
1101X0
110010
62 && defined $new_data{$key}
63 && $old_data{$key} eq $new_data{$key}
64 || !defined $old_data{$key} && !defined $new_data{$key} )
65 {
66 25 delete $new_data{$key}; # remove unchanged cols
67 }
68 }
69  
70 14 ▶ if ( keys %new_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %new_data) 14 0 14
71 14 my ( $action, $table )
72 = $self->_action_setup( $stored_row, 'update' );
73  
74 14 ▶ if ($action) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action) 14 0 14
75 14 $self->_store_changes( $action, $table, \%old_data, \%new_data );
76 }
77 }
78  
79 14 return $result;
80 }
81  
82 sub delete {
83 16 ▶ my $self = shift;
Subroutine delete: called CC=3 SCAR=11.1
Pod documented
84  
85 16 ▶ return $self->next::method(@_) if !$enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $DBIx::Class::AuditLog::enabled 0 16 16
86  
87 16 my $stored_row = $self->get_from_storage;
88  
89 16 my $result = $self->next::method(@_);
90  
91 16 my ( $action, $table ) = $self->_action_setup( $stored_row, 'delete' );
92  
93 16 ▶ if ($action) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action) 16 0 16
94 16 my %old_data = $stored_row->get_columns;
95 16 $self->_store_changes( $action, $table, \%old_data, {} );
96 }
97  
98 16 return $result;
99 }
100  
101  
102 sub _audit_log_schema {
103 56 ▶ my $self = shift;
Subroutine _audit_log_schema: called CC=1 SCAR=0.0
104 56 return $self->result_source->schema->audit_log_schema;
105 }
106  
107 sub _action_setup {
108 56 ▶ my $self = shift;
Subroutine _action_setup: called CC=1 SCAR=0.0
109 56 my $row = shift;
110 56 my $type = shift;
111  
112 56 return $self->_audit_log_schema->audit_log_create_action(
113 { row => join( '-', $row->id ),
114 table => $row->result_source_instance->name,
115 action_type => $type,
116 }
117 );
118 }
119  
120 sub _store_changes {
121 56 ▶ my $self = shift;
Subroutine _store_changes: called CC=5 SCAR=16.1
122 56 my $action = shift;
123 56 my $table = shift;
124 56 my $old_values = shift;
125 56 my $new_values = shift;
126  
127 56 ▶ foreach my $column (
Branch: 1 branch
BranchTrueFalseTotal
keys %{$new_values;} ? : 40 16 56
128 56 keys %{$new_values} ? keys %{$new_values} : keys %{$old_values} )
129 {
130 143 ▶ if ( $self->_do_audit($column) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_do_audit($column)) 125 18 143
131 125 my $field = $table->find_or_create_related( 'Field',
132 { name => $column } );
133  
134 125 my $create_params = { field_id => $field->id, };
135  
136 125 ▶ if ( $self->_do_modify_audit_value($column) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_do_modify_audit_value($column)) { } 3 122 125
137 $create_params->{new_value}
138 = $self->_modify_audit_value( $column,
139 3 $new_values->{$column} );
140 $create_params->{old_value}
141 = $self->_modify_audit_value( $column,
142 3 $old_values->{$column} );
143 }
144 else {
145 122 $create_params->{new_value} = $new_values->{$column};
146 122 $create_params->{old_value} = $old_values->{$column};
147 }
148  
149 125 $action->create_related( 'Change', $create_params, );
150 }
151 }
152 }
153  
154 sub _force_audit {
155 235 ▶ my ( $self, $column ) = @_;
Subroutine _force_audit: called CC=1 SCAR=0.0
156  
157 235 my $info = $self->column_info($column);
158 235 return defined $info->{force_audit_log_column};
159  
160 }
161  
162 sub _do_audit {
163 143 ▶ my $self = shift;
Subroutine _do_audit: called CC=4 SCAR=13.9
164 143 my $column = shift;
165  
166 143 ▶ return 1 if $self->_force_audit($column);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_force_audit($column) 2 141 143
167  
168 141 my $info = $self->column_info($column);
169 return defined $info->{audit_log_column}
170 141 ▶ && $info->{audit_log_column} == 0 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $info->{'audit_log_column'} && $info->{'audit_log_column'} == 0 ? : 18 123 141
Condition100%3/3
defined $info->{"audit_log_column"} and $info->{"audit_log_column"} == 0
!ll&&!rl&&r
122118
MC/DC100%2/2
defined $info->{"audit_log_column"} and $info->{"audit_log_column"} == 0
defined $info->{"audit_log_column"} $info->{"audit_log_column"} == 0
Truth table100%3/3
A and B
A= defined $info->{"audit_log_column"}B= $info->{"audit_log_column"} == 0
ABresult
0X0
100
111
171 }
172  
173 sub _do_modify_audit_value {
174 125 ▶ my $self = shift;
Subroutine _do_modify_audit_value: called CC=2 SCAR=6.9
175 125 my $column = shift;
176  
177 125 my $info = $self->column_info($column);
178  
179 125 ▶ return $info->{modify_audit_value} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$info->{'modify_audit_value'} ? : 3 122 125
180 }
181  
182 sub _modify_audit_value {
183 6 ▶ my $self = shift;
Subroutine _modify_audit_value: called CC=5 SCAR=16.3
184 6 my $column = shift;
185 6 my $value = shift;
186  
187 6 my $info = $self->column_info($column);
188 6 my $meth = $info->{modify_audit_value};
189 6 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meth 0 6 6
190 unless defined $meth;
191  
192 6 ▶ return &$meth( $self, $value )
Branch: 1 branch
BranchTrueFalseTotal
if ref $meth eq "CODE" 2 4 6
193 if ref($meth) eq 'CODE';
194  
195 4 ▶ $meth = "modify_audit_$column"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($meth) 2 2 4
196 unless $self->can($meth);
197  
198 4 ▶ return $self->$meth($value)
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($meth) 4 0 4
199 if $self->can($meth);
200  
201 0 die "unable to find modify_audit_method ($meth) for $column in $self";
202  
203 }
204  
205 # ABSTRACT: Simple activity audit logging for DBIx::Class
206  
207 12 1;
208  
209 __END__
210  
211 =pod
212  
213 =encoding UTF-8
214  
215 =head1 NAME
216  
217 DBIx::Class::AuditLog - Simple activity audit logging for DBIx::Class
218  
219 =head1 VERSION
220  
221 version 0.6.2
222  
223 =head1 NAME
224  
225 DBIx::Class::AuditLog - Simple activity audit logging for DBIx::Class
226  
227 =head1 VERSION
228  
229 version 0.2.6
230  
231 =head1 DBIx::Class OVERRIDDEN METHODS
232  
233 =head2 insert
234  
235 =head2 update
236  
237 =head2 delete
238  
239 =head1 HELPER METHODS
240  
241 =head2 _audit_log_schema
242  
243 Returns the AuditLog schema from storage.
244  
245 my $al_schema = $schmea->audit_log_schema;
246  
247 =head2 _action_setup
248  
249 Creates a new AuditLog Action for a specific type.
250  
251 Requires:
252 row: primary key of the table that is being audited
253 action_type: action type, 1 of insert/update/delete
254  
255 =head2 _store_changes
256  
257 Store the column data that has changed
258  
259 Requires:
260 action: the action object that has associated changes
261 old_values: the old values are being replaced
262 new_values: the new values that are replacing the old
263 table: dbic object of the audit_log_table object
264  
265 =head2 _do_audit
266  
267 Returns 1 or 0 if the column should be audited or not.
268  
269 Requires:
270 column: the name of the column/field to check
271  
272 =head2 _force_audit
273  
274 Returns 1 or 0 if the column should be audited even if its value did not change.
275  
276 Requires:
277 column: the name of the column/field to check
278  
279 =head2 _do_modify_audit_value
280  
281 Returns 1 or 0 if the columns value should be modified before audit.
282  
283 Requires:
284 column: the name of the column/field to check
285  
286 =head2 _modify_audit_value
287  
288 Modifies the colums audit-value. Dies if no modify-method could be found.
289  
290 Returns:
291 the modified value
292  
293 Requires:
294 column: the name of the column/field to check
295 value: the original value
296  
297 =head1 AUTHOR
298  
299 Mark Jubenville <ioncache@gmail.com>
300  
301 =head1 CONTRIBUTORS
302  
303 Lukas Thiemeier <lukast@cpan.org>
304  
305 Dimitar Petrov <dcpetrov@cpan.org>
306  
307 =head1 COPYRIGHT AND LICENSE
308  
309 This software is copyright (c) 2012 by Mark Jubenville.
310  
311 This is free software; you can redistribute it and/or modify it under
312 the same terms as the Perl 5 programming language system itself.
313  
314 =head1 AUTHOR
315  
316 Mark Jubenville <ioncache@cpan.org>
317  
318 =head1 COPYRIGHT AND LICENSE
319  
320 This software is copyright (c) 2012 by Mark Jubenville.
321  
322 This is free software; you can redistribute it and/or modify it under
323 the same terms as the Perl 5 programming language system itself.
324  
325 =cut
↑ Summary blib/lib/DBIx/Class/Schema/AuditLog/Structure.pm »