blib/lib/DBIx/TransactionManager/Extended.pm

filter: Statementstmt 20.8% 19 / 91 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 6 / 18 Podpod 77.7% 7 / 9 totaltotal 21.6% 32 / 148 SCAR 54.6
CC
19
Cov
16%
txn_commit
110.0
_remove_hook
30.0
context_data
6.0
CRAP 235.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.02";
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->{_in_commit_after_hook} = 0;
19 0 $self->{_context_data} = {};
20 0 $self->{_hooks_before_commit} = [];
21 0 $self->{_hooks_after_commit} = [];
22 0 $self;
23 }
24  
25 sub context_data {
26 0 ▶ my $self = shift;
Subroutine context_data: not called CC=2 SCAR=17.9
Pod documented
27 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
28 0 return $self->{_context_data};
29 }
30  
31 # override
32 0 ▶sub txn_scope { DBIx::TransactionManager::Extended::Txn->new(@_) }
Subroutine txn_scope: not called CC=1 SCAR=6.9
Pod documented
33  
34 # override
35 sub txn_commit {
36 0 ▶ my $self = shift;
Subroutine txn_commit: not called CC=10 SCAR=47.0
Pod undocumented
37 0 ▶ return $self->SUPER::txn_commit() if @{ $self->active_transactions } != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->active_transactions;} != 1 0 0 0
38  
39 0 my $context_data = $self->{_context_data};
40 0 my $hooks_before_commit = $self->{_hooks_before_commit};
41 0 my $hooks_after_commit = $self->{_hooks_after_commit};
42  
43 0 ▶ if (@$hooks_before_commit) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$hooks_before_commit) 0 0 0
44 0 eval { $_->($context_data) for @$hooks_before_commit };
45 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
46 0 $self->txn_rollback();
47 0 croak $@;
48 }
49 0 @$hooks_before_commit = ();
50 }
51  
52 0 my $ret = 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 (@$hooks_after_commit) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$hooks_after_commit) 0 0 0
61 0 local $self->{_in_commit_after_hook} = $self->{_in_commit_after_hook} + 1;
62 0 ▶ if ($self->{_in_commit_after_hook} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_in_commit_after_hook"} == 1) 0 0 0
63 0 eval { $_->($context_data) for @$hooks_after_commit };
64 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
65 0 $self->_reset_all();
66 0 croak $@;
67 }
68 0 @$hooks_after_commit = ();
69 }
70 }
71 0 %$context_data = ();
72  
73 0 return $ret;
74 }
75  
76 # override
77 sub txn_rollback {
78 0 ▶ my $self = shift;
Subroutine txn_rollback: not called CC=1 SCAR=6.9
Pod undocumented
79 0 $self->_reset_all();
80 0 $self->SUPER::txn_rollback();
81 }
82  
83 sub _reset_all {
84 0 %{$_[0]->{_context_data}}
85 0 = @{$_[0]->{_hooks_before_commit}}
86 0 ▶ = @{$_[0]->{_hooks_after_commit}}
Subroutine _reset_all: not called CC=1 SCAR=6.9
87 = ();
88 }
89  
90 sub add_hook_before_commit {
91 0 ▶ my ($self, $hook) = @_;
Subroutine add_hook_before_commit: not called CC=2 SCAR=17.9
Pod documented
92 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
93 0 push @{ $self->{_hooks_before_commit} } => $hook;
94 0 return $hook;
95 }
96  
97 sub add_hook_after_commit {
98 0 ▶ my ($self, $hook) = @_;
Subroutine add_hook_after_commit: not called CC=2 SCAR=17.9
Pod documented
99 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
100 0 push @{ $self->{_hooks_after_commit} } => $hook;
101 0 return $hook;
102 }
103  
104 sub remove_hook_before_commit {
105 0 ▶ my ($self, $hook) = @_;
Subroutine remove_hook_before_commit: not called CC=2 SCAR=17.9
Pod documented
106 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
107 0 _remove_hook($self->{_hooks_before_commit}, $hook);
108 }
109  
110 sub remove_hook_after_commit {
111 0 ▶ my ($self, $hook) = @_;
Subroutine remove_hook_after_commit: not called CC=2 SCAR=17.9
Pod documented
112 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
113 0 _remove_hook($self->{_hooks_after_commit}, $hook);
114 }
115  
116 sub _remove_hook {
117 0 ▶ my ($hooks, $hook) = @_;
Subroutine _remove_hook: not called CC=5 SCAR=34.0
118  
119 0 my $found;
120 0 for my $i (0..$#{$hooks}) {
121 0 ▶ if ($hook == $hooks->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hook == $hooks->[$i]) 0 0 0
122 0 $found = $i;
123 0 last;
124 }
125 }
126 0 ▶ splice @$hooks, $found, 1 if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
127 0 ▶ return $found ? $hook : undef;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 0 0 0
128 }
129  
130 1 1;
131 __END__
↑ Summary blib/lib/DBIx/TransactionManager/Extended/Txn.pm »