lib/Mojolicious/Plugin/Fondation/Workflow.pm

filter: Statementstmt 73.4% 61 / 83 Branchbran 34.0% 15 / 44 Conditioncond 19.2% 5 / 26 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 88.8% 8 / 9 Podpod 33.3% 1 / 3 totaltotal 50.8% 90 / 177 CC 39 SCAR 48.3
CC
39
Cov
53%
register
181.6
__ANON__
6.7
__ANON__
6.0
CRAP 125.0
↑ Summary lib/Mojolicious/Plugin/Fondation/Workflow/Action/Workflow.pm »
1 package Mojolicious::Plugin::Fondation::Workflow;
2 2 $Mojolicious::Plugin::Fondation::Workflow::VERSION = '0.02';
3 # ABSTRACT: Workflow plugin — state machines with UI and authorization for Fondation
4  
5 2 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 2 ▶use Workflow::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Workflow::Persister::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Mojolicious::Plugin::Fondation::Workflow::Proxy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶sub fondation_meta {
Subroutine fondation_meta: called CC=1 SCAR=0.0
Pod undocumented
13 return {
14 2 dependencies => [],
15 provides_actions => ['Workflow'],
16 after => ['Fondation::Model::DBIx::Async'],
17 defaults => {
18 workflow_dir => 'share/workflows',
19 persister_file_dir => 'data/workflows',
20 models => {
21 workflow => { source => 'Workflow' },
22 workflow_history => { source => 'WorkflowHistory' },
23 },
24 },
25 setup => {
26 label => 'Workflow',
27 description => 'Workflow engine configuration',
28 parameters => [
29 {
30 key => 'persister.dsn',
31 label => 'Persister DSN',
32 type => 'string',
33 default => 'dbi:SQLite:dbname=data/app.db',
34 required => 1,
35 placeholder => 'dbi:SQLite:dbname=data/app.db',
36 },
37 {
38 key => 'persister.user',
39 label => 'Persister user',
40 type => 'string',
41 default => '',
42 required => 0,
43 placeholder => '',
44 },
45 {
46 key => 'persister.password',
47 label => 'Persister password',
48 type => 'password',
49 default => '',
50 required => 0,
51 placeholder => '',
52 },
53 {
54 key => 'workflow_dir',
55 label => 'Workflow directory',
56 type => 'string',
57 default => 'share/workflows',
58 required => 1,
59 placeholder => 'share/workflows',
60 },
61 {
62 key => 'persister_file_dir',
63 label => 'File persister directory',
64 type => 'string',
65 default => 'data/workflows',
66 required => 1,
67 placeholder => 'data/workflows',
68 },
69 {
70 key => 'models.workflow.source',
71 label => 'Workflow table source',
72 type => 'string',
73 default => 'Workflow',
74 required => 1,
75 },
76 {
77 key => 'models.workflow_history.source',
78 label => 'Workflow History table source',
79 type => 'string',
80 default => 'WorkflowHistory',
81 required => 1,
82 },
83 ],
84 },
85 };
86 }
87  
88 2 ▶sub register ($self, $app, $config) {
Subroutine register: called CC=32 SCAR=52.0
Pod documented
89 2 ▶ my $workflows = $config->{workflows} || {};
Condition50%1/2
$config->{'workflows'} || {}
l!l
20
MC/DC0%0/1
$config->{'workflows'} || {}
$config->{'workflows'}
Truth table50%1/2
A
A= $config->{'workflows'}
Aresult
11
00
90 2 ▶ my $persister_cfg = $config->{persister} || {};
Condition50%1/2
$config->{'persister'} || {}
l!l
02
MC/DC0%0/1
$config->{'persister'} || {}
$config->{'persister'}
Truth table50%1/2
A
A= $config->{'persister'}
Aresult
11
00
91  
92 # ── Initialize Workflow::Factory ──────────────────────────────────
93  
94 2 my $factory = Workflow::Factory->instance;
95  
96 # Configure the DBI persister (default for DB-backed workflows)
97 2 ▶ if (!$persister_cfg->{dsn}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($persister_cfg->{"dsn"}) 2 0 2
98 # Auto-detect DSN from Fondation::Model::DBIx::Async if loaded
99 2 my $api = eval { $app->fondation };
100 2 ▶ my $reg = $api ? $api->registry : {};
Branch: 1 branch
BranchTrueFalseTotal
$api ? : 0 2 2
101 2 my $dbix = $reg->{'Mojolicious::Plugin::Fondation::Model::DBIx::Async'};
102 2 ▶ if ($dbix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbix) 0 2 2
103 my $be_name = $dbix->{config}{default_backend}
104 0 ▶ || ($dbix->{config}{backends} && $dbix->{config}{backends}[0]);
Condition0%0/6
$dbix->{'config'}{'default_backend'} || $dbix->{'config'}{'backends'} && $dbix->{'config'}{'backends'}[0]
l!l&&r!l&&!r
000
$dbix->{"config"}{"backends"} and $dbix->{"config"}{"backends"}[0]
!ll&&!rl&&r
000
MC/DC0%0/3
$dbix->{'config'}{'default_backend'} || $dbix->{'config'}{'backends'} && $dbix->{'config'}{'backends'}[0]
$dbix->{'config'}{'default_backend'} $dbix->{"config"}{"backends"} $dbix->{"config"}{"backends"}[0]
Truth table0%0/4
A || B and C
A= $dbix->{'config'}{'default_backend'}B= $dbix->{"config"}{"backends"}C= $dbix->{"config"}{"backends"}[0]
ABCresult
1XX1
0111
00X0
0100
105 0 ▶ if ($be_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($be_name) 0 0 0
106 0 ▶ my $belist = $dbix->{config}{backends} // [];
Condition0%0/2
$dbix->{"config"}{"backends"} // []
l!l
00
MC/DC0%0/1
$dbix->{"config"}{"backends"} // []
$dbix->{"config"}{"backends"}
Truth table0%0/2
A
A= $dbix->{"config"}{"backends"}
Aresult
11
00
107 0 ▶ for (my $i = 0; $i < @$belist; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$belist) 0 0 0
108 0 ▶ next unless $belist->[$i] eq $be_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $belist->[$i] eq $be_name 0 0 0
109 0 ▶ my $be = $belist->[$i + 1] // {};
Condition0%0/2
$$belist[$i + 1] // {}
l!l
00
MC/DC0%0/1
$$belist[$i + 1] // {}
$$belist[$i + 1]
Truth table0%0/2
A
A= $$belist[$i + 1]
Aresult
11
00
110 0 ▶ $persister_cfg->{dsn} = $be->{dsn} if $be->{dsn};
Branch: 1 branch
BranchTrueFalseTotal
if $be->{"dsn"} 0 0 0
111 0 ▶ $persister_cfg->{user} = $be->{user} if $be->{user};
Branch: 1 branch
BranchTrueFalseTotal
if $be->{"user"} 0 0 0
112 0 ▶ $persister_cfg->{password} = $be->{password} if $be->{password};
Branch: 1 branch
BranchTrueFalseTotal
if $be->{"password"} 0 0 0
113 0 last;
114 }
115 }
116 }
117 }
118  
119 2 ▶ if ($persister_cfg->{dsn}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($persister_cfg->{'dsn'}) { } 0 2 2
120 $factory->add_config(
121 persister => {
122 name => 'FondationWorkflow',
123 class => 'Workflow::Persister::DBI',
124 dsn => $persister_cfg->{dsn},
125 user => $persister_cfg->{user} || '',
126 0 ▶ password => $persister_cfg->{password} || '',
Condition0%0/4
$persister_cfg->{'user'} || ''
l!l
00
$persister_cfg->{'password'} || ''
l!l
00
MC/DC0%0/1
$persister_cfg->{'user'} || ''
$persister_cfg->{'user'}
MC/DC0%0/1
$persister_cfg->{'password'} || ''
$persister_cfg->{'password'}
Truth table0%0/2
A
A= $persister_cfg->{'user'}
Aresult
11
00
Truth table0%0/2
A
A= $persister_cfg->{'password'}
Aresult
11
00
127 },
128 );
129 } else {
130 2 $self->log->warn("No DSN configured — DB-backed workflows will fail");
131 }
132  
133 # Load all workflow YAML files
134 2 ▶ my $workflow_dir = $config->{workflow_dir} // 'share/workflows';
Condition50%1/2
$config->{"workflow_dir"} // "share/workflows"
l!l
20
MC/DC0%0/1
$config->{"workflow_dir"} // "share/workflows"
$config->{"workflow_dir"}
Truth table50%1/2
A
A= $config->{"workflow_dir"}
Aresult
11
00
135  
136 # Auto-discover YAML files if no explicit workflows configured
137 2 ▶ unless (%$workflows) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$workflows) 0 2 2
138 0 my $dir = $app->home->child($workflow_dir);
139 0 ▶ if (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 0 0 0
140 0 ▶ $dir->list({ dir => 0 })->each(sub ($file, $idx) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
141 0 ▶ return unless $file->basename =~ /^(.+)\.ya?ml$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->basename =~ /^(.+)\.ya?ml$/iu 0 0 0
142 0 my $type = $1;
143 0 $workflows->{$type} = $file->to_string;
144 0 });
145 }
146 }
147  
148 2 my %registered_persisters;
149 2 foreach my $type (keys %$workflows) {
150 2 my $file = $workflows->{$type};
151 2 my $path = $file;
152 2 $self->log->info("Loading '$type' from $path");
153  
154 2 require YAML;
155 2 my $config_data = YAML::LoadFile($path);
156  
157 # Register additional persisters declared in the YAML
158 # (e.g. Workflow::Persister::File for wizards that don't need a DB)
159 2 ▶ if (my $persisters = $config_data->{persister_config}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $persisters = $config_data->{"persister_config"}) 2 0 2
160 2 for my $p (@$persisters) {
161 2 ▶ my $name = $p->{name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $name = $p->{"name"} or next 2 0 2
162 2 ▶ next if $registered_persisters{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $registered_persisters{$name}++ 0 2 2
163 # Set default path for File persistors and create the directory
164 2 ▶ if (($p->{class} // '') eq 'Workflow::Persister::File') {
Branch: 1 branch
BranchTrueFalseTotal
if (($p->{"class"} // "") eq "Workflow::Persister::File") 2 0 2
Condition50%1/2
$p->{"class"} // ""
l!l
20
MC/DC0%0/1
$p->{"class"} // ""
$p->{"class"}
Truth table50%1/2
A
A= $p->{"class"}
Aresult
11
00
165 2 ▶ $p->{path} //= $config->{persister_file_dir} // 'data/workflows';
Condition25%1/4
$p->{"path"} //= $config->{"persister_file_dir"} // "data/workflows"
l!l
20
$config->{"persister_file_dir"} // "data/workflows"
l!l
00
MC/DC0%0/1
$p->{"path"} //= $config->{"persister_file_dir"} // "data/workflows"
$p->{"path"}
Truth table50%1/2
A
A= $p->{"path"}
Aresult
11
00
166 2 my $dir = $app->home->child($p->{path});
167 2 ▶ $dir->make_path unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 0 2
168 }
169 2 $factory->add_config(persister => $p);
170 2 $self->log->info("Registered persister '$name' ($p->{class})");
171 }
172 }
173  
174 $factory->add_config(
175 2 workflow => [ $config_data ],
176 action => [ $config_data ],
177 );
178 }
179  
180 # ── Helper: $c->workflow() ───────────────────────────────────────
181  
182 7 ▶ $app->helper(workflow => sub ($c, $type, $resource_id = undef, $context = undef) {
Subroutine __ANON__: called CC=6 SCAR=19.0
183 7 ▶ if (ref $type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq "HASH") 0 7 7
184 0 $context = $type->{context};
185 0 $resource_id = $type->{resource_id};
186 0 $type = $type->{type};
187 }
188  
189 7 my $wf;
190 7 ▶ if (defined $resource_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resource_id) { } 1 6 7
191 # fetch_workflow may die (e.g. Persister::File throws persist_error
192 # when the file no longer exists after a cleanup)
193 1 $wf = eval { $factory->fetch_workflow($type, $resource_id) };
194 1 ▶ return undef unless $wf;
Branch: 1 branch
BranchTrueFalseTotal
unless $wf 0 1 1
195 } else {
196 6 $wf = $factory->create_workflow($type, $context);
197 }
198  
199 7 return Mojolicious::Plugin::Fondation::Workflow::Proxy->new(
200 wf => $wf,
201 factory => $factory,
202 c => $c,
203 type => $type,
204 );
205 2 });
206  
207 2 return $self;
208 }
209  
210 2 ▶sub fondation_finalyze ($self, $app, $long_name) {
Subroutine fondation_finalyze: called CC=2 SCAR=8.4
Pod undocumented
211 2 my $registry = $app->fondation->registry;
212  
213 # Cross-plugin relation: WorkflowHistory → User (who did the action)
214 2 ▶ if ($registry->{'Mojolicious::Plugin::Fondation::User'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($registry->{"Mojolicious::Plugin::Fondation::User"}) 0 2 2
215 0 my $history_class = 'Mojolicious::Plugin::Fondation::Workflow::Schema::Result::WorkflowHistory';
216  
217 0 $history_class->belongs_to(
218 'user',
219 'Mojolicious::Plugin::Fondation::User::Schema::Result::User',
220 { 'foreign.id' => 'self.user_id' },
221 );
222 }
223  
224 2 return 1;
225 }
226  
227 2 1;
228  
229 __END__
↑ Summary lib/Mojolicious/Plugin/Fondation/Workflow/Action/Workflow.pm »