Workflow/Factory.pm

filter: Statementstmt 86.7% 399 / 460 Branchbran 78.9% 101 / 128 Conditioncond 65.9% 29 / 44 MC/DCmcdc 37.0% 10 / 27 Subroutinesub 85.7% 48 / 56 Podpod 100% 17 / 17 totaltotal 82.5% 604 / 732 CC 98 SCAR 16.5
CC
98
Cov
84%
_add_action_config
10.1
fetch_workflow
9.5
create_workflow
8.3
CRAP 5.2
« Workflow/State.pm ↑ Summary Workflow/Persister/DBI/AutoGeneratedId.pm »
1 package Workflow::Factory;
2  
3 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use parent qw( Workflow::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Workflow::Exception qw( configuration_error workflow_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use Module::Runtime qw( require_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 28 $Workflow::Factory::VERSION = '2.12';
16  
17 # Extra action attribute validation is off by default for compatibility.
18 28 our $VALIDATE_ACTION_CONFIG = 0;
19  
20 28 my (%INSTANCES);
21  
22 127 ▶sub import {
Subroutine import: called CC=5 SCAR=16.2
23 127 my $class = shift;
24  
25 127 ▶ $class = ref $class || $class; # just in case
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
01270
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
26 127 my $package = caller;
27 127 ▶ if ( defined $_[0] && $_[0] eq 'FACTORY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] eq "FACTORY") 124 3 127
Condition66%2/3
defined $_[0] and $_[0] eq "FACTORY"
!ll&&!rl&&r
30124
MC/DC50%1/2
defined $_[0] and $_[0] eq "FACTORY"
defined $_[0] $_[0] eq "FACTORY"
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq "FACTORY"
ABresult
0X0
100
111
28 124 shift;
29 124 my $instance;
30  
31 124 my $import_target = $package . '::FACTORY';
32 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 124 ▶ unless ( defined &{$import_target} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{$import_target;}) 124 0 124
34 124 ▶ *{$import_target} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
35 28 ▶ return $instance if $instance;
Branch: 1 branch
BranchTrueFalseTotal
if $instance 11 17 28
36 17 $instance = _initialize_instance($class);
37 17 return $instance;
38 124 };
39 }
40 }
41 127 $class->SUPER::import(@_);
42 }
43  
44 28 require Workflow;
45 28 require Workflow::Action;
46 28 require Workflow::Condition;
47 28 require Workflow::Condition::Negated;
48 28 require Workflow::Config;
49 28 require Workflow::Context;
50 28 require Workflow::Persister;
51 28 require Workflow::State;
52 28 require Workflow::Validator;
53  
54 28 my $DEFAULT_INITIAL_STATE = 'INITIAL';
55  
56 28 my @FIELDS = qw(config_callback);
57  
58 28 __PACKAGE__->mk_accessors(@FIELDS);
59  
60 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
61 2 my $proto = shift;
62 2 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
63  
64 2 workflow_error "Please call 'instance()' or import the 'FACTORY' object ",
65 "to get the '$class' object rather than instantiating a ",
66 "new one directly.";
67 }
68  
69 36 ▶sub instance {
Subroutine instance: called CC=2 SCAR=7.6
Pod documented
70 36 my $proto = shift;
71 36 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0360
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
72  
73 36 return _initialize_instance($class);
74 }
75  
76 53 ▶sub _initialize_instance {
Subroutine _initialize_instance: called CC=2 SCAR=6.9
77 53 my ($class) = @_;
78  
79 53 ▶ unless ( $INSTANCES{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INSTANCES{$class}) 21 32 53
80 21 $log->debug( "Creating empty instance of '$class' factory for ",
81 "singleton use" );
82 21 $INSTANCES{$class} = bless {} => $class;
83 }
84 53 return $INSTANCES{$class};
85 }
86  
87 0 ▶sub _delete_instance {
Subroutine _delete_instance: not called CC=2 SCAR=17.9
88 0 my ($class) = @_;
89  
90 0 ▶ if ( $INSTANCES{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INSTANCES{$class}) { } 0 0 0
91 0 $log->debug( "Deleting instance of '$class' factory." );
92 0 delete $INSTANCES{$class};
93 } else {
94 0 $log->debug( "No instance of '$class' factory found." );
95 }
96  
97 0 return;
98 }
99  
100 28 my %CONFIG = ( 'Workflow::Config' => 1 );
101  
102 126 ▶sub _add_config_from_files {
Subroutine _add_config_from_files: called CC=2 SCAR=6.9
103 126 my ($self, $method, $type, $config) = @_;
104  
105 126 foreach my $item ( @{ $config } ) {
106 174 $self->$method(
107 Workflow::Config->parse_all_files( $type, $item )
108 );
109 }
110  
111 126 return;
112 }
113  
114 126 ▶sub _add_config_from_file {
Subroutine _add_config_from_file: called CC=2 SCAR=6.9
115 126 my ($self, $method, $type, $config) = @_;
116  
117 126 ▶ if ( ref $config eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config eq 'ARRAY') { } 37 89 126
118 37 $self->_add_config_from_files( $method, $type, $config );
119 }
120 else {
121 89 $self->_add_config_from_files( $method, $type, [ $config ] );
122 }
123  
124 126 return;
125 }
126  
127 21 ▶sub add_config_from_file {
Subroutine add_config_from_file: called CC=3 SCAR=11.0
Pod documented
128 21 my ( $self, %params ) = @_;
129 21 ▶ return unless ( scalar keys %params );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %params 0 21 21
130  
131 21 _check_config_keys(%params);
132  
133 21 foreach my $type ( sort keys %params ) {
134 $self->log->debug(
135 0 ▶ sub { "Using '$type' configuration file(s): " .
Subroutine __ANON__: not called CC=1 SCAR=1.2
136 0 join( ', ', _flatten( $params{$type} ) ) } );
137 }
138  
139 21 $self->log->debug( "Adding condition configurations..." );
140 $self->_add_config_from_file( \&_add_condition_config,
141 21 'condition', $params{condition});
142  
143 21 $self->log->debug( "Adding validator configurations..." );
144 $self->_add_config_from_file( \&_add_validator_config,
145 21 'validator', $params{validator});
146  
147 21 $self->log->debug( "Adding persister configurations..." );
148 $self->_add_config_from_file( \&_add_persister_config,
149 21 'persister', $params{persister});
150  
151 21 $self->log->debug( "Adding action configurations..." );
152 $self->_add_config_from_file( \&_add_action_config,
153 21 'action', $params{action});
154  
155 21 $self->log->debug( "Adding workflow configurations..." );
156 $self->_add_config_from_file( \&_add_workflow_config,
157 21 'workflow', $params{workflow});
158  
159 21 $self->log->debug( "Adding independent observer configurations..." );
160 $self->_add_config_from_file( \&_add_observer_config,
161 21 'observer', $params{observer});
162  
163 21 return;
164 }
165  
166 29 ▶sub add_config {
Subroutine add_config: called CC=2 SCAR=6.9
Pod documented
167 29 my ( $self, %params ) = @_;
168 29 ▶ return unless ( scalar keys %params );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %params 0 29 29
169 29 _check_config_keys(%params);
170 29 $self->_add_condition_config( _flatten( $params{condition} ) );
171 29 $self->_add_validator_config( _flatten( $params{validator} ) );
172 29 $self->_add_persister_config( _flatten( $params{persister} ) );
173 29 $self->_add_action_config( _flatten( $params{action} ) );
174 28 $self->_add_workflow_config( _flatten( $params{workflow} ) );
175 27 $self->_add_observer_config( _flatten( $params{observer} ) );
176 27 return;
177 }
178  
179 50 ▶sub _check_config_keys {
Subroutine _check_config_keys: called CC=2 SCAR=7.4
180 50 my (%params) = @_;
181 my @bad_keys
182 50 = grep { !Workflow::Config->is_valid_config_type($_) } keys %params;
183 50 ▶ if ( scalar @bad_keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @bad_keys) 0 50 50
184 0 workflow_error "You tried to add configuration information to the ",
185 "workflow factory with one or more bad keys: ",
186 join( ', ', @bad_keys ), ". The following are the ",
187 "keys you have to choose from: ",
188 join( ', ', Workflow::Config->get_valid_config_types ), '.';
189 }
190 }
191  
192 171 ▶sub _flatten {
Subroutine _flatten: called CC=2 SCAR=6.9
193 171 my ($item) = @_;
194 171 ▶ return ( ref $item eq 'ARRAY' ) ? @{$item} : ($item);
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'ARRAY' ? : 24 147 171
195 }
196  
197 ########################################
198 # WORKFLOW
199  
200 71 ▶sub _add_workflow_config {
Subroutine _add_workflow_config: called CC=7 SCAR=19.5
201 71 my ( $self, @all_workflow_config ) = @_;
202 71 ▶ return unless ( scalar @all_workflow_config );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @all_workflow_config 0 71 71
203  
204 71 foreach my $workflow_config (@all_workflow_config) {
205 71 ▶ next unless ( ref $workflow_config eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $workflow_config eq "HASH" 19 52 71
206 52 my $wf_type = $workflow_config->{type};
207 52 $self->{_workflow_config}{$wf_type} = $workflow_config;
208  
209 # Create Workflow::State objects for each configured state.
210 # When we instantiate a new workflow we pass these objects
211  
212 52 foreach my $state_conf ( @{ $workflow_config->{state} } ) {
213  
214 # Add the workflow type to the state conf.
215 162 $state_conf->{type} = $wf_type;
216 162 my $wf_state = Workflow::State->new( $state_conf, $self );
217  
218 162 push @{ $self->{_workflow_state}{$wf_type} }, $wf_state;
219 }
220  
221 52 my $wf_class = $workflow_config->{class};
222 52 ▶ if ( $wf_class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wf_class) 1 51 52
223 1 $self->_load_class( $wf_class,
224 q{Cannot require workflow class '%s': %s} );
225 }
226  
227 52 ▶ $workflow_config->{history_class} ||= 'Workflow::History';
Condition100%2/2
$workflow_config->{"history_class"} ||= "Workflow::History"
l!l
1735
MC/DC100%1/1
$workflow_config->{"history_class"} ||= "Workflow::History"
$workflow_config->{"history_class"}
Truth table100%2/2
A
A= $workflow_config->{"history_class"}
Aresult
11
00
228 $self->_load_class( $workflow_config->{history_class},
229 52 q{Cannot require workflow history class '%s': %s} );
230  
231 $self->_load_observers($workflow_config->{type},
232 52 $workflow_config->{observer} );
233  
234 51 $self->log->info( "Added all workflow states..." );
235 }
236  
237 70 return;
238 }
239  
240 # Load all the observers so they're available when we instantiate the
241 # workflow
242  
243 55 ▶sub _load_observers {
Subroutine _load_observers: called CC=8 SCAR=20.8
244 55 my ( $self, $wf_type, $observer_specs ) = @_;
245 55 my @observers = ();
246 55 ▶ foreach my $observer_info ( @{$observer_specs || []} ) {
Condition100%2/2
$observer_specs || []
l!l
550
MC/DC100%1/1
$observer_specs || []
$observer_specs
Truth table100%2/2
A
A= $observer_specs
Aresult
11
00
247 6 ▶ if ( my $observer_class = $observer_info->{class} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $observer_class = $observer_info->{'class'}) { } 2 4 6
elsif (my $observer_sub = $observer_info->{'sub'}) { } 4 0 4
248 2 $self->_load_class( $observer_class,
249 "Cannot require observer '%s' to watch observer "
250 . "of type '$wf_type': %s" );
251 2 ▶ push @observers, sub { $observer_class->update(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
252 } elsif ( my $observer_sub = $observer_info->{sub} ) {
253 4 my ( $observer_class, $observer_sub )
254 = $observer_sub =~ /^(.*)::(.*)$/;
255 4 $self->_load_class( $observer_class,
256 "Cannot require observer '%s' with sub '$observer_sub' to "
257 . "watch observer of type '$wf_type': %s" );
258 4 my $o_sub_name = $observer_class . '::' . $observer_sub;
259 4 ▶ if (exists &$o_sub_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists &$o_sub_name) { } 3 1 4
260 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
261 3 push @observers, \&{ $o_sub_name };
262 } else {
263 1 my $error = 'subroutine not found';
264 1 $self->log->error( "Error loading subroutine '$observer_sub' in ",
265 "class '$observer_class': $error" );
266 1 workflow_error $error;
267 }
268 } else {
269 0 workflow_error "Cannot add observer to '$wf_type': you must ",
270 "have either 'class' or 'sub' defined. (See ",
271 "Workflow::Factory docs for details.)";
272 }
273 }
274  
275 54 my $observers_num = scalar @observers;
276  
277 54 ▶ if (@observers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@observers) { } 4 50 54
278 4 ▶ $self->{_workflow_observers}{$wf_type} ||= [];
Condition100%2/2
$self->{"_workflow_observers"}{$wf_type} ||= []
l!l
13
MC/DC100%1/1
$self->{"_workflow_observers"}{$wf_type} ||= []
$self->{"_workflow_observers"}{$wf_type}
Truth table100%2/2
A
A= $self->{"_workflow_observers"}{$wf_type}
Aresult
11
00
279 4 push @{ $self->{_workflow_observers}{$wf_type} }, @observers;
280  
281 $self->log->info(
282 0 ▶ sub { "Added $observers_num to '$wf_type': " .
Subroutine __ANON__: not called CC=1 SCAR=6.9
283 4 join( ', ', @observers ) } );
284  
285 } else {
286 50 $self->{_workflow_observers}{$wf_type} = undef;
287  
288 50 $self->log->info( "No observers added to '$wf_type'" );
289 }
290  
291 54 return $observers_num;
292 }
293  
294 59 ▶sub _load_class {
Subroutine _load_class: called CC=1 SCAR=1.2
295 59 my ( $self, $class_to_load, $msg ) = @_;
296  
297 try {
298 59 require_module( $class_to_load );
299 }
300 59 catch ($error) {
301 0 my $full_msg = sprintf $msg, $class_to_load, $error;
302 0 $self->log->error($full_msg);
303 0 workflow_error $full_msg;
304 }
305 }
306  
307 30 ▶sub create_workflow {
Subroutine create_workflow: called CC=8 SCAR=21.1
Pod documented
308 30 my ( $self, $wf_type, $context, $wf_class ) = @_;
309 30 my $wf_config = $self->_get_workflow_config($wf_type);
310  
311 30 ▶ unless ($wf_config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wf_config) 2 28 30
312 2 workflow_error "No workflow of type '$wf_type' available";
313 }
314  
315 28 ▶ $wf_class = $wf_config->{class} || 'Workflow' unless ($wf_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $wf_class 28 0 28
Condition100%2/2
$wf_config->{'class'} || 'Workflow'
l!l
226
MC/DC100%1/1
$wf_config->{'class'} || 'Workflow'
$wf_config->{'class'}
Truth table100%2/2
A
A= $wf_config->{'class'}
Aresult
11
00
316 my $wf
317 = $wf_class->new( undef,
318 $wf_config->{initial_state} || $DEFAULT_INITIAL_STATE,
319 28 ▶ $wf_config, $self->{_workflow_state}{$wf_type}, $self );
Condition33%1/3
$wf_config->{'initial_state'} || $DEFAULT_INITIAL_STATE
l!l&&r!l&&!r
0280
MC/DC0%0/2
$wf_config->{'initial_state'} || $DEFAULT_INITIAL_STATE
$wf_config->{'initial_state'} $DEFAULT_INITIAL_STATE
Truth table33%1/3
A || B
A= $wf_config->{'initial_state'}B= $DEFAULT_INITIAL_STATE
ABresult
1X1
011
000
320  
321 28 ▶ if ($context and not blessed $context) {
Branch: 2 branches
BranchTrueFalseTotal
if ($context and not builtin::blessed($context)) { } 0 28 28
elsif (not $context) { } 26 2 28
Condition66%2/3
$context and not builtin::blessed($context)
!ll&&!rl&&r
2620
MC/DC0%0/2
$context and not builtin::blessed($context)
$context not builtin::blessed($context)
Truth table66%2/3
A and not(B)
A= $contextB= not builtin::blessed($context)
ABresult
0X0
100
111
322 0 $context = Workflow::Context->new( %{ $context } );
323 }
324 elsif (not $context) {
325 26 $context = Workflow::Context->new;
326 }
327 28 $wf->context( $context );
328  
329 28 $wf->last_update( DateTime->now( time_zone => $wf->time_zone() ) );
330 28 $self->log->info( "Instantiated workflow object properly, persisting..." );
331 28 my $persister = $self->get_persister( $wf_config->{persister} );
332 28 my $id = $persister->create_workflow($wf);
333 28 $wf->id($id);
334 28 $self->log->info("Persisted workflow with ID '$id'; creating history...");
335 28 $wf->add_history(
336 {
337 $wf->get_initial_history_data(), # returns a *list*
338 workflow_id => $id,
339 state => $wf->state,
340 date => DateTime->now( time_zone => $wf->time_zone() ),
341 time_zone => $wf->time_zone(),
342 });
343 28 $persister->create_history( $wf, $wf->get_unsaved_history() );
344 28 $self->log->info( "Created history object ok" );
345 28 $persister->commit_transaction;
346  
347 28 $self->associate_observers_with_workflow($wf);
348 28 $self->_associate_transaction_observer_with_workflow($wf, $persister);
349 28 $wf->notify_observers('create');
350  
351 28 my $state = $wf->_get_workflow_state();
352 28 $wf->_maybe_autorun_state( $state );
353  
354 28 return $wf;
355 }
356  
357 9 ▶sub fetch_workflow {
Subroutine fetch_workflow: called CC=9 SCAR=22.5
Pod documented
358 9 my ( $self, $wf_type, $wf_id, $context, $wf_class ) = @_;
359 9 my $wf_config = $self->_get_workflow_config($wf_type);
360  
361 9 ▶ unless ($wf_config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wf_config) 0 9 9
362 0 workflow_error "No workflow of type '$wf_type' available";
363 }
364 9 my $persister = $self->get_persister( $wf_config->{persister} );
365 9 my $wf_info = $persister->fetch_workflow($wf_id);
366 9 ▶ $wf_class = $wf_config->{class} || 'Workflow' unless ($wf_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $wf_class 9 0 9
Condition100%2/2
$wf_config->{'class'} || 'Workflow'
l!l
36
MC/DC100%1/1
$wf_config->{'class'} || 'Workflow'
$wf_config->{'class'}
Truth table100%2/2
A
A= $wf_config->{'class'}
Aresult
11
00
367  
368 9 ▶ return unless ($wf_info);
Branch: 1 branch
BranchTrueFalseTotal
unless $wf_info 1 8 9
369  
370 8 ▶ $wf_info->{last_update} ||= '';
Condition50%1/2
$wf_info->{"last_update"} ||= ""
l!l
80
MC/DC0%0/1
$wf_info->{"last_update"} ||= ""
$wf_info->{"last_update"}
Truth table50%1/2
A
A= $wf_info->{"last_update"}
Aresult
11
00
371 8 $self->log->debug(
372 "Fetched data for workflow '$wf_id' ok: ",
373 "[State: $wf_info->{state}] ",
374 "[Last update: $wf_info->{last_update}]"
375 );
376 my $wf = $wf_class->new( $wf_id, $wf_info->{state}, $wf_config,
377 8 $self->{_workflow_state}{$wf_type}, $self );
378  
379 8 ▶ if ($wf_info->{context} && blessed( $wf_info->{context} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wf_info->{'context'} and builtin::blessed($wf_info->{'context'})) { } 0 8 8
Condition66%2/3
$wf_info->{"context"} and builtin::blessed($wf_info->{"context"})
!ll&&!rl&&r
620
MC/DC0%0/2
$wf_info->{"context"} and builtin::blessed($wf_info->{"context"})
$wf_info->{"context"} builtin::blessed($wf_info->{"context"})
Truth table66%2/3
A and B
A= $wf_info->{"context"}B= builtin::blessed($wf_info->{"context"})
ABresult
0X0
100
111
380 0 $context = $wf_info->{context};
381 } else {
382 $context = Workflow::Context->new(
383 8 %{ $context },
384 8 ▶ %{ $wf_info->{context} // {} }
Condition100%2/2
$wf_info->{"context"} // {}
l!l
26
MC/DC100%1/1
$wf_info->{"context"} // {}
$wf_info->{"context"}
Truth table100%2/2
A
A= $wf_info->{"context"}
Aresult
11
00
385 );
386 }
387 8 $wf->context( $context );
388  
389 8 $wf->last_update( $wf_info->{last_update} );
390  
391 8 $self->associate_observers_with_workflow($wf);
392 8 $self->_associate_transaction_observer_with_workflow($wf, $persister);
393 8 $wf->notify_observers('fetch');
394  
395 8 return $wf;
396 }
397  
398 36 ▶sub associate_observers_with_workflow {
Subroutine associate_observers_with_workflow: called CC=3 SCAR=11.0
Pod documented
399 36 my ( $self, $wf ) = @_;
400 36 my $observers = $self->{_workflow_observers}{ $wf->type };
401 36 ▶ return unless ( ref $observers eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $observers eq "ARRAY" 26 10 36
402 10 $wf->add_observer($_) for ( @{$observers} );
403 }
404  
405 36 ▶sub _associate_transaction_observer_with_workflow {
Subroutine _associate_transaction_observer_with_workflow: called CC=1 SCAR=0.1
406 36 my ( $self, $wf, $persister ) = @_;
407 $wf->add_observer(
408 253 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
409 253 my ($unused, $action) = @_; # first argument repeats $wf
410 253 ▶ if ( $action eq 'save' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'save') { } 36 217 253
elsif ($action eq 'rollback') { } 0 217 217
411 36 $persister->commit_transaction;
412 }
413 elsif ( $action eq 'rollback' ) {
414 0 $persister->rollback_transaction;
415 }
416 36 });
417 }
418  
419 3 ▶sub _initialize_workflow_config {
Subroutine _initialize_workflow_config: called CC=4 SCAR=13.9
420 3 my $self = shift;
421 3 my $wf_type = shift;
422  
423 3 ▶ if ( ref( $self->config_callback ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->config_callback eq "CODE") 2 1 3
424 2 my $args = &{ $self->config_callback }($wf_type);
425 2 ▶ $self->add_config_from_file( %{$args} ) if $args && %{$args};
Branch: 1 branch
BranchTrueFalseTotal
if $args and %{$args;} 1 1 2
Condition66%2/3
$args and %{$args;}
!ll&&!rl&&r
011
MC/DC50%1/2
$args and %{$args;}
$args %{$args;}
Truth table66%2/3
A and B
A= $argsB= %{$args;}
ABresult
0X0
100
111
426 }
427 }
428  
429 81 ▶sub _get_workflow_config {
Subroutine _get_workflow_config: called CC=2 SCAR=6.9
430 81 my ( $self, $wf_type ) = @_;
431 $self->_initialize_workflow_config($wf_type)
432 81 ▶ unless $self->{_workflow_config}{$wf_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_workflow_config"}{$wf_type} 3 78 81
433 81 return $self->{_workflow_config}{$wf_type};
434 }
435  
436 0 ▶sub _insert_workflow {
Subroutine _insert_workflow: not called CC=1 SCAR=6.9
437 0 my ( $self, $wf ) = @_;
438 0 my $wf_config = $self->_get_workflow_config( $wf->type );
439 0 my $persister = $self->get_persister( $wf_config->{persister} );
440 0 my $id = $persister->create_workflow($wf);
441 0 $wf->id($id);
442 0 return $wf;
443  
444 }
445  
446 36 ▶sub save_workflow {
Subroutine save_workflow: called CC=2 SCAR=7.0
Pod documented
447 36 my ( $self, $wf ) = @_;
448  
449 36 my $old_update = $wf->last_update;
450 36 $wf->last_update( DateTime->now( time_zone => $wf->time_zone() ) );
451  
452 36 my $wf_config = $self->_get_workflow_config( $wf->type );
453 36 my $persister = $self->get_persister( $wf_config->{persister} );
454 try {
455 36 $persister->update_workflow($wf);
456 36 $self->log->info( "Workflow '", $wf->id, "' updated ok" );
457 36 my @unsaved = $wf->get_unsaved_history;
458 36 foreach my $h (@unsaved) {
459 9 $h->set_new_state( $wf->state );
460 }
461 36 $persister->create_history( $wf, @unsaved );
462 36 $self->log->info( "Created necessary history objects ok" );
463 }
464 36 catch ($error) {
465 0 $wf->last_update($old_update);
466 0 die $error;
467 }
468 36 $wf->notify_observers( 'save' );
469  
470 36 return $wf;
471 }
472  
473 6 ▶sub get_workflow_history {
Subroutine get_workflow_history: called CC=1 SCAR=0.0
Pod documented
474 6 my ( $self, $wf ) = @_;
475  
476 6 $self->log->debug( "Trying to fetch history for workflow ", $wf->id );
477 6 my $wf_config = $self->_get_workflow_config( $wf->type );
478 6 my $persister = $self->get_persister( $wf_config->{persister} );
479 6 return $persister->fetch_history($wf);
480 }
481  
482 ########################################
483 # ACTIONS
484  
485 63 ▶sub _add_action_config {
Subroutine _add_action_config: called CC=10 SCAR=23.1
486 63 my ( $self, @all_action_config ) = @_;
487  
488 63 ▶ return unless ( scalar @all_action_config );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @all_action_config 3 60 63
489  
490 60 foreach my $actions (@all_action_config) {
491 60 ▶ next unless ( ref $actions eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $actions eq "HASH" 21 39 60
492  
493 # TODO Handle optional type.
494 # Should we check here to see if this matches an existing
495 # workflow type? Maybe do a type check at the end of the config
496 # process?
497 39 ▶ my $type = exists $actions->{type} ? $actions->{type} : 'default';
Branch: 1 branch
BranchTrueFalseTotal
exists $actions->{'type'} ? : 13 26 39
498  
499 39 my $action;
500 39 ▶ if ( exists $actions->{action} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $actions->{'action'}) { } 36 3 39
501 36 $action = $actions->{action};
502 } else {
503 3 push @{$action}, $actions;
504 }
505  
506 39 foreach my $action_config ( @{$action} ) {
507 152 my $name = $action_config->{name};
508 152 $self->log->debug(
509 "Adding configuration for type '$type', action '$name'");
510 152 $self->{_action_config}{$type}{$name} = $action_config;
511 152 my $action_class = $action_config->{class};
512 152 ▶ unless ($action_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action_class) 0 152 152
513 0 configuration_error
514 "Action '$name' must be associated with a ",
515 "class using the 'class' attribute.";
516 }
517 $self->log->debug(
518 152 "Trying to include action class '$action_class'...");
519 try {
520 152 require_module( $action_class );
521 }
522 152 catch ($msg) {
523 0 $msg =~ s/\\n/ /g;
524 0 configuration_error
525 "Cannot include action class '$action_class': $msg";
526 }
527 152 $self->log->debug(
528 "Included action '$name' class '$action_class' ok");
529 152 ▶ if ($self->_validate_action_config) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_validate_action_config) 3 149 152
530 3 my $validate_name = $action_class . '::validate_config';
531 3 ▶ if (exists &$validate_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists &$validate_name) 2 1 3
532 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
533 2 $self->log->debug(
534 "Validating configuration for action '$name'");
535 2 $validate_name->($action_config);
536 }
537 }
538 } # End action for.
539 }
540 }
541  
542 43 ▶sub get_action_config {
Subroutine get_action_config: called CC=4 SCAR=14.2
Pod documented
543 43 my ( $self, $wf, $action_name ) = @_;
544 43 my $config = $self->{_action_config}{ $wf->type }{$action_name};
545 $config = $self->{_action_config}{default}{$action_name}
546 43 ▶ unless ($config and %{$config});
Branch: 1 branch
BranchTrueFalseTotal
unless $config and %{$config;} 38 5 43
Condition66%2/3
$config and %{$config;}
!ll&&!rl&&r
3805
MC/DC50%1/2
$config and %{$config;}
$config %{$config;}
Truth table66%2/3
A and B
A= $configB= %{$config;}
ABresult
0X0
100
111
547  
548 43 ▶ unless ($config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config) 0 43 43
549 0 workflow_error "No action with name '$action_name' available";
550 }
551 43 return $config;
552 }
553  
554 0 ▶sub get_action {
Subroutine get_action: not called CC=1 SCAR=6.9
Pod documented
555 0 my ( $self, $wf, $action_name ) = @_;
556 0 my $config = $self->get_action_config( $wf, $action_name );;
557 0 my $action_class = $config->{class};
558 0 return $action_class->new( $wf, $config );
559 }
560  
561 ########################################
562 # PERSISTERS
563  
564 50 ▶sub _add_persister_config {
Subroutine _add_persister_config: called CC=5 SCAR=16.3
565 50 my ( $self, @all_persister_config ) = @_;
566  
567 50 ▶ return unless ( scalar @all_persister_config );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @all_persister_config 21 29 50
568  
569 29 foreach my $persister_config (@all_persister_config) {
570 31 ▶ next unless ( ref $persister_config eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $persister_config eq "HASH" 11 20 31
571 20 my $name = $persister_config->{name};
572 20 $self->log->debug( "Adding configuration for persister '$name'" );
573 20 $self->{_persister_config}{$name} = $persister_config;
574 20 my $persister_class = $persister_config->{class};
575 20 ▶ unless ($persister_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($persister_class) 0 20 20
576 0 configuration_error "You must specify a 'class' in persister ",
577 "'$name' configuration";
578 }
579 $self->log->debug(
580 20 "Trying to include persister class '$persister_class'...");
581  
582 try {
583 20 require_module( $persister_class );
584 }
585 20 catch ($error) {
586 0 configuration_error "Cannot include persister class ",
587 "'$persister_class': $error";
588 }
589 20 $self->log->debug(
590 "Included persister '$name' class '$persister_class' ",
591 "ok; now try to instantiate persister..." );
592 20 my $persister;
593 try {
594 20 $persister = $persister_class->new($persister_config)
595 }
596 20 catch ($error) {
597 0 configuration_error "Failed to create instance of persister ",
598 "'$name' of class '$persister_class': $error";
599 };
600 20 $self->{_persister}{$name} = $persister;
601 20 $self->log->debug( "Instantiated persister '$name' ok" );
602 }
603 }
604  
605 108 ▶sub get_persister {
Subroutine get_persister: called CC=2 SCAR=7.4
Pod documented
606 108 my ( $self, $persister_name ) = @_;
607 108 my $persister = $self->{_persister}{$persister_name};
608 108 ▶ unless ($persister) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($persister) 0 108 108
609 0 workflow_error "No persister with name '$persister_name' available";
610 }
611 108 return $persister;
612 }
613  
614 0 ▶sub get_persisters {
Subroutine get_persisters: not called CC=1 SCAR=6.9
Pod documented
615 0 my $self = shift;
616 0 my @persisters = sort keys %{ $self->{_persister} };
617  
618 0 return @persisters;
619 }
620  
621 0 ▶sub get_persister_for_workflow_type {
Subroutine get_persister_for_workflow_type: not called CC=2 SCAR=17.9
Pod documented
622 0 my $self = shift;
623  
624 0 my ($type) = @_;
625 0 my $wf_config = $self->_get_workflow_config($type);
626 0 ▶ if ( not $wf_config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wf_config) 0 0 0
627 0 workflow_error "no workflow of type '$type' available";
628 }
629 0 my $persister = $self->get_persister( $wf_config->{'persister'} );
630  
631 0 return $persister;
632 }
633  
634 ########################################
635 # CONDITIONS
636  
637 62 ▶sub _add_condition_config {
Subroutine _add_condition_config: called CC=8 SCAR=20.9
638 62 my ( $self, @all_condition_config ) = @_;
639  
640 62 ▶ return unless ( scalar @all_condition_config );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @all_condition_config 4 58 62
641  
642 58 foreach my $conditions (@all_condition_config) {
643 59 ▶ next unless ( ref $conditions eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conditions eq "HASH" 27 32 59
644  
645 my $type
646 32 ▶ = exists $conditions->{type} ? $conditions->{type} : 'default';
Branch: 1 branch
BranchTrueFalseTotal
exists $conditions->{'type'} ? : 12 20 32
647  
648 32 my $c;
649 32 ▶ if ( exists $conditions->{condition} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conditions->{'condition'}) { } 29 3 32
650 29 $c = $conditions->{condition};
651 } else {
652 3 push @{$c}, $conditions;
653 }
654  
655 32 foreach my $condition_config ( @{$c} ) {
656 41 my $name = $condition_config->{name};
657 41 $self->log->debug( "Adding configuration for condition '$name'" );
658 41 $self->{_condition_config}{$type}{$name} = $condition_config;
659 41 my $condition_class = $condition_config->{class};
660 41 ▶ unless ($condition_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($condition_class) 0 41 41
661 0 configuration_error "Condition '$name' must be associated ",
662 "with a class using the 'class' attribute";
663 }
664 $self->log->debug(
665 41 "Trying to include condition class '$condition_class'");
666 try {
667 41 require_module( $condition_class );
668 }
669 41 catch ($error) {
670 0 configuration_error "Cannot include condition class ",
671 "'$condition_class': $error";
672 }
673 41 $self->log->debug(
674 "Included condition '$name' class '$condition_class' ",
675 "ok; now try to instantiate condition..." );
676 41 my $condition;
677 try {
678 41 $condition = $condition_class->new($condition_config)
679 }
680 41 catch ($error) {
681 0 configuration_error
682 "Cannot create condition '$name': $error";
683 }
684 41 $self->{_conditions}{$type}{$name} = $condition;
685 41 $self->log->debug( "Instantiated condition '$name' ok" );
686 }
687 }
688 }
689  
690 141 ▶sub get_condition {
Subroutine get_condition: called CC=8 SCAR=21.1
Pod documented
691 141 my ( $self, $name, $type ) = @_;
692  
693 141 my $condition;
694  
695 141 ▶ if ( defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 140 1 141
696 140 $condition = $self->{_conditions}{$type}{$name};
697 }
698  
699 # This catches cases where type isn't defined and cases
700 # where the condition was defined as the default rather than
701 # the current Workflow type.
702 141 ▶ if ( not defined $condition ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $condition) 125 16 141
703 125 $condition = $self->{_conditions}{'default'}{$name};
704 }
705  
706 141 ▶ if ( not defined $condition
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $condition and $name =~ / \A ! /msux) 9 132 141
Condition66%2/3
not defined $condition and $name =~ / \A ! /msux
!ll&&!rl&&r
13209
MC/DC50%1/2
not defined $condition and $name =~ / \A ! /msux
not defined $condition $name =~ / \A ! /msux
Truth table66%2/3
not(A) and B
A= not defined $conditionB= $name =~ / \A ! /msux
ABresult
0X0
100
111
707 and $name =~ m/ \A ! /msx ) {
708 9 my $negated = $name;
709 9 $negated =~ s/ \A ! //gx;
710  
711 9 ▶ if ( $self->get_condition( $negated, $type ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_condition($negated, $type)) 9 0 9
712 9 $condition = Workflow::Condition::Negated->new(
713 { name => $name }
714 );
715  
716 9 ▶ $type = 'default' unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 9 9
717 9 $self->{_conditions}{$type}{$name} = $condition;
718 }
719 }
720  
721 141 ▶ unless ($condition) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($condition) 0 141 141
722 0 workflow_error "No condition with name '$name' available";
723 }
724 141 return $condition;
725 }
726  
727 ########################################
728 # VALIDATORS
729  
730 51 ▶sub _add_validator_config {
Subroutine _add_validator_config: called CC=7 SCAR=19.6
731 51 my ( $self, @all_validator_config ) = @_;
732  
733 51 ▶ return unless (@all_validator_config);
Branch: 1 branch
BranchTrueFalseTotal
unless @all_validator_config 8 43 51
734  
735 43 foreach my $validators (@all_validator_config) {
736 43 ▶ next unless ( ref $validators eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $validators eq "HASH" 28 15 43
737  
738 15 my $v;
739 15 ▶ if ( exists $validators->{validator} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $validators->{'validator'}) { } 14 1 15
740 14 $v = $validators->{validator};
741 } else {
742 1 push @{$v}, $validators;
743 }
744  
745 15 for my $validator_config ( @{$v} ) {
746 15 my $name = $validator_config->{name};
747 15 $self->log->debug( "Adding configuration for validator '$name'" );
748 15 $self->{_validator_config}{$name} = $validator_config;
749 15 my $validator_class = $validator_config->{class};
750 15 ▶ unless ($validator_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($validator_class) 0 15 15
751 0 configuration_error
752 "Validator '$name' must be associated with ",
753 "a class using the 'class' attribute.";
754 }
755 $self->log->debug(
756 15 "Trying to include validator class '$validator_class'");
757 try {
758 15 require_module( $validator_class )
759 }
760 15 catch ($error) {
761 0 workflow_error
762 "Cannot include validator class '$validator_class': $error";
763 }
764 15 $self->log->debug(
765 "Included validator '$name' class '$validator_class' ",
766 " ok; now try to instantiate validator..."
767 );
768 15 my $validator;
769 try {
770 15 $validator = $validator_class->new($validator_config)
771 }
772 15 catch ($error) {
773 0 workflow_error "Cannot create validator '$name': $error";
774 }
775 15 $self->{_validators}{$name} = $validator;
776 15 $self->log->debug( "Instantiated validator '$name' ok" );
777 }
778 }
779 }
780  
781 13 ▶sub get_validator {
Subroutine get_validator: called CC=2 SCAR=7.6
Pod documented
782 13 my ( $self, $name ) = @_;
783 13 ▶ unless ( $self->{_validators}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_validators"}{$name}) 0 13 13
784 0 workflow_error "No validator with name '$name' available";
785 }
786 13 return $self->{_validators}{$name};
787 }
788  
789 0 ▶sub get_validators {
Subroutine get_validators: not called CC=1 SCAR=6.9
Pod documented
790 0 my $self = shift;
791 0 my @validators = sort keys %{ $self->{_validators} };
792 0 return @validators;
793 }
794  
795 152 ▶sub _validate_action_config {
Subroutine _validate_action_config: called CC=1 SCAR=0.0
796 152 return $VALIDATE_ACTION_CONFIG;
797 }
798  
799 ########################################
800 # Independent Observers
801  
802 48 ▶sub _add_observer_config {
Subroutine _add_observer_config: called CC=6 SCAR=17.9
803 48 my ( $self, @all_observer_config ) = @_;
804  
805 48 ▶ return unless (@all_observer_config);
Branch: 1 branch
BranchTrueFalseTotal
unless @all_observer_config 19 29 48
806  
807 29 foreach my $observers (@all_observer_config) {
808 29 ▶ next unless ( ref $observers eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $observers eq "HASH" 27 2 29
809  
810 my $v = exists $observers->{observer} ?
811 2 ▶ $observers->{observer} : [ $observers->{observer} ];
Branch: 1 branch
BranchTrueFalseTotal
exists $observers->{'observer'} ? : 2 0 2
812  
813 2 for my $observer_config ( @{$v} ) {
814 3 my $name = $observer_config->{name};
815 3 my $type = $observer_config->{type};
816  
817 3 $self->_load_observers( $type, [ $observer_config ] );
818 }
819 }
820  
821 29 return;
822 }
823  
824 28 1;
825  
826 __END__
« Workflow/State.pm ↑ Summary Workflow/Persister/DBI/AutoGeneratedId.pm »