AuditLog.pm

filter: Statementstmt 99.0% 99 / 100 Branchbran 84.0% 42 / 50 Conditioncond 82.6% 19 / 23 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 91.5% 184 / 201 CC 37 SCAR 21.9
CC
37
Cov
92%
update
18.1
_modify_audit_value
5.1
_store_changes
5.0
CRAP 8.9
↑ Summary Schema/AuditLog/Structure.pm »
1 package DBIx::Class::AuditLog;
2 12 $DBIx::Class::AuditLog::VERSION = '0.6.4';
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 28 ▶sub insert {
Subroutine insert: called CC=4 SCAR=14.0
Pod documented
13 28 my $self = shift;
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 17 ▶sub update {
Subroutine update: called CC=18 SCAR=29.0
Pod documented
30 17 my $self = shift;
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] || {} };
Condition100%2/2
$_[0] || {}
l!l
78
MC/DC100%1/1
$_[0] || {}
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
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 16 ▶sub delete {
Subroutine delete: called CC=3 SCAR=11.1
Pod documented
83 16 my $self = shift;
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 56 ▶sub _audit_log_schema {
Subroutine _audit_log_schema: called CC=1 SCAR=0.0
103 56 my $self = shift;
104 56 return $self->result_source->schema->audit_log_schema;
105 }
106  
107 56 ▶sub _action_setup {
Subroutine _action_setup: called CC=1 SCAR=0.0
108 56 my $self = shift;
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 56 ▶sub _store_changes {
Subroutine _store_changes: called CC=5 SCAR=16.1
121 56 my $self = shift;
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 235 ▶sub _force_audit {
Subroutine _force_audit: called CC=3 SCAR=11.3
155 235 my ( $self, $column ) = @_;
156  
157 ## make sure that this is an actual column, and is not
158 ## a correlated column
159 235 ▶ return unless $self->has_column($column);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_column($column) 0 235 235
160  
161 235 my $info = $self->column_info($column);
162  
163 return defined $info->{force_audit_log_column}
164 235 ▶ && $info->{force_audit_log_column};
Condition66%2/3
defined $info->{'force_audit_log_column'} && $info->{'force_audit_log_column'}
!ll&&!rl&&r
22906
MC/DC50%1/2
defined $info->{'force_audit_log_column'} && $info->{'force_audit_log_column'}
defined $info->{'force_audit_log_column'} $info->{'force_audit_log_column'}
Truth table66%2/3
A && B
A= defined $info->{'force_audit_log_column'}B= $info->{'force_audit_log_column'}
ABresult
0X0
100
111
165 }
166  
167 143 ▶sub _do_audit {
Subroutine _do_audit: called CC=4 SCAR=13.9
168 143 my $self = shift;
169 143 my $column = shift;
170  
171 143 ▶ return 1 if $self->_force_audit($column);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_force_audit($column) 2 141 143
172  
173 141 my $info = $self->column_info($column);
174 return defined $info->{audit_log_column}
175 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
176 }
177  
178 125 ▶sub _do_modify_audit_value {
Subroutine _do_modify_audit_value: called CC=2 SCAR=6.9
179 125 my $self = shift;
180 125 my $column = shift;
181  
182 125 my $info = $self->column_info($column);
183  
184 125 ▶ return $info->{modify_audit_value} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$info->{'modify_audit_value'} ? : 3 122 125
185 }
186  
187 6 ▶sub _modify_audit_value {
Subroutine _modify_audit_value: called CC=5 SCAR=16.3
188 6 my $self = shift;
189 6 my $column = shift;
190 6 my $value = shift;
191  
192 6 my $info = $self->column_info($column);
193 6 my $meth = $info->{modify_audit_value};
194 6 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meth 0 6 6
195 unless defined $meth;
196  
197 6 ▶ return &$meth( $self, $value )
Branch: 1 branch
BranchTrueFalseTotal
if ref $meth eq "CODE" 2 4 6
198 if ref($meth) eq 'CODE';
199  
200 4 ▶ $meth = "modify_audit_$column"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($meth) 2 2 4
201 unless $self->can($meth);
202  
203 4 ▶ return $self->$meth($value)
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($meth) 4 0 4
204 if $self->can($meth);
205  
206 0 die "unable to find modify_audit_method ($meth) for $column in $self";
207  
208 }
209  
210 # ABSTRACT: Simple activity audit logging for DBIx::Class
211  
212 12 1;
213  
214 __END__
↑ Summary Schema/AuditLog/Structure.pm »