Workflow/Action.pm

filter: Statementstmt 94.0% 95 / 101 Branchbran 66.6% 8 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 94.1% 16 / 17 Podpod 100% 9 / 9 totaltotal 90.8% 129 / 142 CC 13 SCAR 11.8
CC
13
Cov
90%
init
6.1
validate
6.0
get_validators
2.0
CRAP 3.3
« Workflow/Config/XML.pm ↑ Summary Workflow/Persister.pm »
1 package Workflow::Action;
2  
3 # Note: we may implement a separate event mechanism so that actions
4 # can trigger other code (to read observations from database?)
5  
6 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use parent qw( Workflow::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Workflow::InputField;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Workflow::Validator::HasRequiredField;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use Workflow::Factory qw( FACTORY );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 28 $Workflow::Action::VERSION = '2.12';
16  
17 28 my @PROPS = qw( name class description group );
18 28 my @INTERNAL = qw( _factory );
19 28 __PACKAGE__->mk_accessors( @PROPS, @INTERNAL );
20  
21 ####################
22 # INPUT FIELDS
23  
24 71 ▶sub add_fields {
Subroutine add_fields: called CC=1 SCAR=0.0
Pod documented
25 71 my ( $self, @fields ) = @_;
26 71 push @{ $self->{_fields} }, @fields;
27 }
28  
29 43 ▶sub required_fields {
Subroutine required_fields: called CC=1 SCAR=0.0
Pod documented
30 43 my ($self) = @_;
31 43 return grep { $_->requirement() eq 'required' } @{ $self->{_fields} };
32 }
33  
34 0 ▶sub optional_fields {
Subroutine optional_fields: not called CC=1 SCAR=6.9
Pod documented
35 0 my ($self) = @_;
36 0 return grep { $_->requirement() eq 'optional' } @{ $self->{_fields} };
37 }
38  
39 1 ▶sub fields {
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
40 1 my ($self) = @_;
41 1 return @{ $self->{_fields} };
42 }
43  
44 ####################
45 # VALIDATION
46  
47 43 ▶sub add_validators {
Subroutine add_validators: called CC=2 SCAR=6.9
Pod documented
48 43 my ( $self, @validator_info ) = @_;
49 43 my @validators = ();
50 43 foreach my $conf (@validator_info) {
51 13 my $validator = $self->_factory()->get_validator( $conf->{name} );
52 13 my @args = $self->normalize_array( $conf->{arg} );
53 13 push @validators,
54 {
55 validator => $validator,
56 args => \@args
57 };
58 }
59 43 push @{ $self->{_validators} }, @validators;
60 }
61  
62 36 ▶sub get_validators {
Subroutine get_validators: called CC=2 SCAR=7.0
Pod documented
63 36 my ($self) = @_;
64 36 ▶ return () if ( not defined $self->{_validators} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_validators"} 0 36 36
65 36 return @{ $self->{_validators} };
66 }
67  
68 36 ▶sub validate {
Subroutine validate: called CC=6 SCAR=18.0
Pod documented
69 36 my ( $self, $wf, $action_args ) = @_;
70 36 my @validators = $self->get_validators;
71 36 ▶ return unless ( scalar @validators );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @validators 0 36 36
72  
73 36 ▶ $action_args //= {};
Condition50%1/2
$action_args //= {}
l!l
036
MC/DC0%0/1
$action_args //= {}
$action_args
Truth table50%1/2
A
A= $action_args
Aresult
11
00
74 my %all_args = (
75 36 %{ $wf->context->param() },
76 36 %{$action_args}
77 );
78 36 foreach my $validator_info (@validators) {
79 45 my $validator = $validator_info->{validator};
80 45 my $args = $validator_info->{args};
81  
82 45 my @runtime_args = ();
83 45 foreach my $arg ( @{$args} ) {
84 45 ▶ if ( $arg =~ /^\$(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^\$(.*)$/u) { } 9 36 45
85 9 push @runtime_args, $all_args{$1};
86 } else {
87 36 push @runtime_args, $arg;
88 }
89 }
90 45 $validator->validate($wf, @runtime_args);
91 }
92 }
93  
94 # Subclasses override...
95  
96 1 ▶sub execute {
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
97 1 my ( $self, $wf ) = @_;
98 1 croak "Class ", ref($self), " must implement 'execute()'\n";
99 }
100  
101 ########################################
102 # PRIVATE
103  
104 44 ▶sub init {
Subroutine init: called CC=6 SCAR=18.0
Pod documented
105 44 my ( $self, $wf, $params ) = @_;
106  
107 # So we don't destroy the original...
108 44 my %copy_params = %{$params};
109  
110 43 $self->_factory( $wf->_factory() );
111 43 $self->class( $copy_params{class} );
112 43 $self->name( $copy_params{name} );
113 43 $self->description( $copy_params{description} );
114 43 $self->group( $copy_params{group} );
115  
116 ## init normal fields
117 43 my @fields = $self->normalize_array( $copy_params{field} );
118 43 foreach my $field_info (@fields) {
119 71 ▶ if ( my $field_class = $field_info->{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field_class = $field_info->{'class'}) { } 0 71 71
120 0 $self->log->debug("Using custom field class $field_class");
121 0 $self->add_fields( $field_class->new($field_info) );
122 } else {
123 71 $self->log->debug("Using standard field class");
124 71 $self->add_fields(
125 Workflow::InputField->new($field_info) );
126 }
127 }
128  
129 ## establish validator for fields with is_required="yes"
130 43 @fields = $self->required_fields();
131 43 my $validator = Workflow::Validator::HasRequiredField->new(
132 { name => 'HasRequiredField for is_required fields',
133 class => 'Workflow::Validator::HasRequiredField'
134 }
135 );
136 43 my @args = ();
137 43 foreach my $field (@fields) {
138 58 ▶ next if ( not $field ); ## empty @fields array
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 58 58
139 58 push @args, $field->name();
140 }
141 43 push @{ $self->{_validators} },
142 {
143 validator => $validator,
144 args => \@args
145 };
146  
147 ## init normal validators
148 43 my @validator_info = $self->normalize_array( $copy_params{validator} );
149 43 $self->add_validators(@validator_info);
150  
151 43 delete @copy_params{(@PROPS, qw( field validator ))};
152  
153 # everything else is just a passthru param
154  
155 43 ▶ while ( my ( $key, $value ) = each %copy_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %copy_params) 3 43 46
156 3 $self->param( $key, $value );
157 }
158 }
159  
160 28 1;
161  
162 __END__
« Workflow/Config/XML.pm ↑ Summary Workflow/Persister.pm »