blib/lib/DBIx/TransactionManager/Extended.pm

filter: Statementstmt 21.3% 19 / 89 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 6 / 18 Podpod 77.7% 7 / 9 totaltotal 21.9% 32 / 146 SCAR 54.5
CC
19
Cov
16%
txn_commit
110.0
_remove_hook
30.0
context_data
6.0
CRAP 233.2
↑ Summary blib/lib/DBIx/TransactionManager/Extended/Txn.pm »
1 package DBIx::TransactionManager::Extended;
2 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = "0.01";
7  
8 1 ▶use parent qw/DBIx::TransactionManager/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DBIx::TransactionManager::Extended::Txn;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # override
14 0 ▶sub new { shift->SUPER::new(@_)->_initialize }
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
15  
16 sub _initialize {
17 0 ▶ my $self = shift;
Subroutine _initialize: not called CC=1 SCAR=6.9
18 0 $self->{_context_data} = {};
19 0 $self->{_hooks_before_commit} = [];
20 0 $self->{_hooks_after_commit} = [];
21 0 $self;
22 }
23  
24 sub context_data {
25 0 ▶ my $self = shift;
Subroutine context_data: not called CC=2 SCAR=17.9
Pod documented
26 0 ▶ croak 'CANNOT call context_data out of the transaction' unless $self->in_transaction;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_transaction 0 0 0
27 0 return $self->{_context_data};
28 }
29  
30 # override
31 0 ▶sub txn_scope { DBIx::TransactionManager::Extended::Txn->new(@_) }
Subroutine txn_scope: not called CC=1 SCAR=6.9
Pod documented
32  
33 # override
34 sub txn_commit {
35 0 ▶ my $self = shift;
Subroutine txn_commit: not called CC=10 SCAR=47.0
Pod undocumented
36  
37 0 my $context_data = $self->{_context_data};
38 0 my $last = @{ $self->active_transactions } == 1;
39  
40 0 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) 0 0 0
41 0 my $hooks = $self->{_hooks_before_commit};
42 0 ▶ if (@$hooks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$hooks) 0 0 0
43 0 eval { $_->($context_data) for @$hooks };
44 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
45 0 $self->txn_rollback();
46 0 croak $@;
47 }
48 0 @$hooks = ();
49 }
50 }
51  
52 0 eval {
53 0 $self->SUPER::txn_commit();
54 };
55 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
56 0 $self->_reset_all();
57 0 croak $@;
58 }
59  
60 0 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) 0 0 0
61 0 my $hooks = $self->{_hooks_after_commit};
62 0 ▶ if (@$hooks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$hooks) 0 0 0
63 0 eval { $_->($context_data) for @$hooks };
64 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
65 0 $self->_reset_all();
66 0 croak $@;
67 }
68 0 @$hooks = ();
69 }
70 0 %$context_data = ();
71 }
72 }
73  
74 # override
75 sub txn_rollback {
76 0 ▶ my $self = shift;
Subroutine txn_rollback: not called CC=1 SCAR=6.9
Pod undocumented
77 0 $self->_reset_all();
78 0 $self->SUPER::txn_rollback();
79 }
80  
81 sub _reset_all {
82 0 %{$_[0]->{_context_data}}
83 0 = @{$_[0]->{_hooks_before_commit}}
84 0 ▶ = @{$_[0]->{_hooks_after_commit}}
Subroutine _reset_all: not called CC=1 SCAR=6.9
85 = ();
86 }
87  
88 sub add_hook_before_commit {
89 0 ▶ my ($self, $hook) = @_;
Subroutine add_hook_before_commit: not called CC=2 SCAR=17.9
Pod documented
90 0 ▶ croak 'CANNOT call add_hook_before_commit out of the transaction' unless $self->in_transaction;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_transaction 0 0 0
91 0 push @{ $self->{_hooks_before_commit} } => $hook;
92 0 return $hook;
93 }
94  
95 sub add_hook_after_commit {
96 0 ▶ my ($self, $hook) = @_;
Subroutine add_hook_after_commit: not called CC=2 SCAR=17.9
Pod documented
97 0 ▶ croak 'CANNOT call add_hook_after_commit out of the transaction' unless $self->in_transaction;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_transaction 0 0 0
98 0 push @{ $self->{_hooks_after_commit} } => $hook;
99 0 return $hook;
100 }
101  
102 sub remove_hook_before_commit {
103 0 ▶ my ($self, $hook) = @_;
Subroutine remove_hook_before_commit: not called CC=2 SCAR=17.9
Pod documented
104 0 ▶ croak 'CANNOT call remove_hook_before_commit out of the transaction' unless $self->in_transaction;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_transaction 0 0 0
105 0 _remove_hook($self->{_hooks_before_commit}, $hook);
106 }
107  
108 sub remove_hook_after_commit {
109 0 ▶ my ($self, $hook) = @_;
Subroutine remove_hook_after_commit: not called CC=2 SCAR=17.9
Pod documented
110 0 ▶ croak 'CANNOT call remove_hook_after_commit out of the transaction' unless $self->in_transaction;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_transaction 0 0 0
111 0 _remove_hook($self->{_hooks_after_commit}, $hook);
112 }
113  
114 sub _remove_hook {
115 0 ▶ my ($hooks, $hook) = @_;
Subroutine _remove_hook: not called CC=5 SCAR=34.0
116  
117 0 my $found;
118 0 for my $i (0..$#{$hooks}) {
119 0 ▶ if ($hook == $hooks->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook == $hooks->[$i]) 0 0 0
120 0 $found = $i;
121 0 last;
122 }
123 }
124 0 ▶ splice @$hooks, $found, 1 if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
125 0 ▶ return $found ? $hook : undef;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 0 0 0
126 }
127  
128 1 1;
129 __END__
↑ Summary blib/lib/DBIx/TransactionManager/Extended/Txn.pm »