Silki/Role/Schema/SystemLogger.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 68 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Silki::Role::Schema::SystemLogger;
2 {
3 $Silki::Role::Schema::SystemLogger::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8 use namespace::autoclean;
9  
10 use Scalar::Util qw( blessed );
11 use Silki::Schema::SystemLog;
12 use Silki::Types qw( ArrayRef Str );
13  
14 use MooseX::Role::Parameterized;
15  
16 parameter 'methods' => (
17 isa => ArrayRef [Str],
18 default => sub { [] },
19 );
20  
21 # When creating the very first user, we need to skip logging, because there is
22 # no user performing the action.
23 our $SkipLog;
24  
25 role {
26 my $params = shift;
27  
28 for my $meth ( @{ $params->methods() } ) {
29  
30 my $values_meth = '_system_log_values_for_' . $meth;
31 requires $values_meth;
32  
33 my $wrapper = sub {
34 my $orig = shift;
35 my $self = shift;
36 my %p = @_;
37  
38 my $user = delete $p{user};
39  
40 return $self->$orig(%p)
41 if $SkipLog;
42  
43 # XXX - it'd be better to use MX::Params::Validate, but that
44 # module doesn't yet allow you to ignore extra arguments, and we
45 # have no idea what additional arguments might get passed to
46 # $orig.
47 unless ( defined $user
48 && blessed $user
49 && $user->isa('Silki::Schema::User') ) {
50  
51 my $package = ref $self;
52 die "Cannot call $package\->$meth without a user parameter";
53 }
54  
55 my $trans = sub {
56 Silki::Schema::SystemLog->insert(
57 user_id => $user->user_id(),
58 $self->$values_meth(%p),
59 );
60  
61 $self->$orig(%p);
62 };
63  
64 Silki::Schema->RunInTransaction($trans);
65 };
66  
67 around $meth => $wrapper;
68 }
69 };
70  
71 1;
72  
73 # ABSTRACT: Logs specified actions in the SystemLog table
74  
75 __END__