Schema/AuditLog/Structure.pm

filter: Statementstmt 95.8% 69 / 72 Branchbran 61.5% 16 / 26 Conditioncond 62.5% 10 / 16 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 81.6% 111 / 136 CC 20 SCAR 21.0
CC
20
Cov
83%
get_changes
14.4
current_changeset
5.5
audit_log_create_changeset
3.0
CRAP 8.2
« AuditLog.pm ↑ Summary Schema/AuditLog.pm »
1 package DBIx::Class::Schema::AuditLog::Structure;
2 12 $DBIx::Class::Schema::AuditLog::Structure::VERSION = '0.6.4';
3 12 ▶use base qw/DBIx::Class::Schema/;
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 12 require DBIx::Class::Schema::AuditLog::Structure::Action;
9 12 require DBIx::Class::Schema::AuditLog::Structure::AuditedTable;
10 12 require DBIx::Class::Schema::AuditLog::Structure::Change;
11 12 require DBIx::Class::Schema::AuditLog::Structure::Changeset;
12 12 require DBIx::Class::Schema::AuditLog::Structure::Field;
13 12 require DBIx::Class::Schema::AuditLog::Structure::User;
14 12 require DBIx::Class::Schema::AuditLog::Structure::View;
15  
16 12 __PACKAGE__->mk_group_accessors( simple => '_current_changeset_container' );
17  
18 85 ▶sub _current_changeset {
Subroutine _current_changeset: called CC=2 SCAR=6.9
19 85 my $self = shift;
20 85 my $ref = $self->_current_changeset_container;
21  
22 85 ▶ return $ref && $ref->{changeset};
Condition100%3/3
$ref && $ref->{'changeset'}
!ll&&!rl&&r
114529
MC/DC100%2/2
$ref && $ref->{'changeset'}
$ref $ref->{'changeset'}
Truth table100%3/3
A && B
A= $refB= $ref->{'changeset'}
ABresult
0X0
100
111
23 }
24  
25  
26 56 ▶sub current_changeset {
Subroutine current_changeset: called CC=5 SCAR=17.0
Pod documented
27 56 my ( $self, @args ) = @_;
28  
29 56 ▶ $self->throw_exception('Cannot set changeset manually. Use txn_do.')
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 56 56
30 if @args;
31  
32 # we only want to create a changeset if the action (insert/update/delete)
33 # is being run from txn_do -- the txn_do method in
34 # DBIx::Class::Schema::AuditLog sets local
35 # _current_changeset_container->{changeset} &
36 # _current_changeset_container->{args} variables in the scope
37 # of each transaction
38 56 ▶ if ( defined $self->_current_changeset_container
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_current_changeset_container and defined $self->_current_changeset_container->{"changeset"}) 56 0 56
Condition33%1/3
defined $self->_current_changeset_container and defined $self->_current_changeset_container->{"changeset"}
!ll&&!rl&&r
0056
MC/DC0%0/2
defined $self->_current_changeset_container and defined $self->_current_changeset_container->{"changeset"}
defined $self->_current_changeset_container defined $self->_current_changeset_container->{"changeset"}
Truth table33%1/3
A and B
A= defined $self->_current_changeset_containerB= defined $self->_current_changeset_container->{"changeset"}
ABresult
0X0
100
111
39 && defined $self->_current_changeset_container->{changeset} )
40 {
41  
42 56 my $id = $self->_current_changeset;
43  
44 56 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 27 29 56
45 my $changeset = $self->audit_log_create_changeset(
46 27 $self->_current_changeset_container->{args} );
47 27 $self->_current_changeset_container->{changeset} = $changeset->id;
48 27 $id = $changeset->id;
49 }
50  
51 56 return $id;
52 }
53  
54 0 return;
55 }
56  
57  
58 27 ▶sub audit_log_create_changeset {
Subroutine audit_log_create_changeset: called CC=3 SCAR=11.0
Pod documented
59 27 my $self = shift;
60 27 my $changeset_data = shift;
61  
62 27 my ( $changeset, $user );
63  
64 27 ▶ my $user_id = $changeset_data->{user_id} || $changeset_data->{user};
Condition100%3/3
$changeset_data->{'user_id'} || $changeset_data->{'user'}
l!l&&r!l&&!r
8811
MC/DC100%2/2
$changeset_data->{'user_id'} || $changeset_data->{'user'}
$changeset_data->{'user_id'} $changeset_data->{'user'}
Truth table100%3/3
A || B
A= $changeset_data->{'user_id'}B= $changeset_data->{'user'}
ABresult
1X1
011
000
65  
66 27 ▶ if ( $user_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_id) { } 16 11 27
67 16 $user = $self->resultset('AuditLogUser')
68 ->find_or_create( { name => $user_id } );
69  
70 $changeset = $user->create_related( 'Changeset',
71 16 { description => $changeset_data->{description} } );
72 }
73 else {
74 $changeset = $self->resultset('AuditLogChangeset')
75 11 ->create( { description => $changeset_data->{description} } );
76 }
77  
78 27 return $changeset;
79 }
80  
81  
82 56 ▶sub audit_log_create_action {
Subroutine audit_log_create_action: called CC=2 SCAR=7.1
Pod documented
83 56 my $self = shift;
84 56 my $action_data = shift;
85  
86 56 my $changeset = $self->current_changeset;
87  
88 56 ▶ if ($changeset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changeset) 56 0 56
89 my $table = $self->resultset('AuditLogAuditedTable')
90 56 ->find_or_create( { name => $action_data->{table} } );
91  
92 return (
93 $self->resultset('AuditLogChangeset')->find($changeset)
94 ->create_related(
95 'Action',
96 { audited_row => $action_data->{row},
97 audited_table_id => $table->id,
98 action_type => $action_data->{action_type},
99 }
100 56 ),
101 $table
102 );
103 }
104  
105 0 return;
106 }
107  
108 12 ▶sub get_changes {
Subroutine get_changes: called CC=12 SCAR=26.7
Pod documented
109 12 my $self = shift;
110 12 my $options = shift;
111  
112 12 my $audited_row = $options->{id};
113 12 ▶ my $change_order = $options->{change_order} || 'desc';
Condition50%1/2
$options->{'change_order'} || 'desc'
l!l
012
MC/DC0%0/1
$options->{'change_order'} || 'desc'
$options->{'change_order'}
Truth table50%1/2
A
A= $options->{'change_order'}
Aresult
11
00
114 12 my $field_name = $options->{field};
115 12 my $table_name = $options->{table};
116 12 my $timestamp = $options->{created_on};
117 my $action_types = $options->{action_types}
118 12 ▶ || [ 'insert', 'update', 'delete' ];
Condition50%1/2
$options->{'action_types'} || ['insert', 'update', 'delete']
l!l
012
MC/DC0%0/1
$options->{'action_types'} || ['insert', 'update', 'delete']
$options->{'action_types'}
Truth table50%1/2
A
A= $options->{'action_types'}
Aresult
11
00
119  
120 # row and table are required for all changes
121 12 ▶ return if !$audited_row || !$table_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $audited_row and $table_name 0 12 12
Condition33%1/3
$audited_row and $table_name
!ll&&!rl&&r
0012
MC/DC0%0/2
$audited_row and $table_name
$audited_row $table_name
Truth table33%1/3
A and B
A= $audited_rowB= $table_name
ABresult
0X0
100
111
122  
123 12 ▶ my $table_criteria = { prefetch => 'Field' }
Branch: 1 branch
BranchTrueFalseTotal
if $field_name 12 0 12
124 if $field_name;
125 12 my $table = $self->resultset('AuditLogAuditedTable')
126 ->find( { name => $table_name }, $table_criteria );
127  
128 # cannot get changes if the specified table hasn't been logged
129 12 ▶ return unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 12 12
130  
131 12 ▶ my $field = $table->find_related( 'Field', { name => $field_name } )
Branch: 1 branch
BranchTrueFalseTotal
if $field_name 12 0 12
132 if $field_name;
133  
134 12 my $changeset_criteria = {};
135 12 ▶ $changeset_criteria->{'me.created_on'} = $timestamp if $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamp 0 12 12
136 12 my $changesets = $self->resultset('AuditLogChangeset')->search_rs(
137 $changeset_criteria
138 );
139  
140 12 my $actions = $changesets->search_related(
141 'Action',
142 { 'Action.audited_table_id' => $table->id,
143 'Action.audited_row' => $audited_row,
144 'Action.action_type' => $action_types,
145 }
146 );
147  
148 12 ▶ if ( $actions != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($actions != 0) 12 0 12
149 # if field is passed and the passed field wasn't found in the Field
150 # table set field id to -1 to ensure a $changes object with ->count =
151 # 0 is returned
152 12 my $criteria = {};
153 12 ▶ if ( $field_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_name) 12 0 12
154 12 ▶ $criteria->{field_id} = $field ? $field->id : -1;
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 10 2 12
155 }
156  
157 12 my $changes = $actions->search_related_rs(
158 'Change',
159 $criteria,
160 { order_by => { "-$change_order" => 'me.id' },
161 prefetch => [{ 'Action' => { 'Changeset' => 'User' } }, { 'Field' => 'AuditedTable' }],
162 }
163 );
164 12 return $changes;
165 }
166  
167 0 return;
168  
169 }
170  
171 12 1;
172  
173 __END__
« AuditLog.pm ↑ Summary Schema/AuditLog.pm »