blib/lib/Siffra/Bootstrap.pm

filter: Statementstmt 51.4% 71 / 138 Branchbran 8.3% 2 / 24 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 79.1% 19 / 24 Podpod 100% 5 / 5 totaltotal 47.1% 99 / 210 CC 20 SCAR 33.9
CC
20
Cov
43%
loadApplication
110.0
DESTROY
13.8
run
12.0
CRAP 29.8
↑ Summary
1 package Siffra::Bootstrap;
2  
3 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 $Carp::Verbose = 1;
12  
13 2 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::Prompter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 $| = 1; #autoflush
17  
18 2 ▶use constant {
Subroutine BEGIN: called CC=2 SCAR=7.1
19 FALSE => 0,
20 TRUE => 1,
21 2 ▶ DEBUG => $ENV{ DEBUG } // 0,
Condition50%1/2
$ENV{"DEBUG"} // 0
l!l
02
MC/DC0%0/1
$ENV{"DEBUG"} // 0
$ENV{"DEBUG"}
Truth table50%1/2
A
A= $ENV{"DEBUG"}
Aresult
11
00
22 2 };
23  
24 BEGIN
25 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶ binmode( STDOUT, ":encoding(UTF-8)" );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 binmode( STDERR, ":encoding(UTF-8)" );
28  
29 2 require Siffra::Tools;
30 2 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 $VERSION = '0.11';
33 2 @ISA = qw(Siffra::Tools Exporter);
34  
35 #Give a hoot don't pollute, do not export more than needed by default
36 2 @EXPORT = qw();
37 2 @EXPORT_OK = qw();
38 2 %EXPORT_TAGS = ();
39 } ## end BEGIN
40  
41 UNITCHECK
42 {
43 # eval { use Fcntl qw(:flock); };
44 # $log->info( "Tentando lockar o programa [ $0 ]..." );
45 # unless ( flock( DATA, LOCK_EX | LOCK_NB ) )
46 # {
47 # $log->warn( "O programa [ $0 ] já está sendo executado. Saindo." );
48 # exit( 1 );
49 # }
50 # $log->info( "Programa [ $0 ] lockado com sucesso..." );
51 } ## end UNITCHECK
52  
53 #################### subroutine header begin ####################
54  
55 =head2 sample_function
56  
57 Usage : How to use this function/method
58 Purpose : What it does
59 Returns : What it returns
60 Argument : What it wants to know
61 Throws : Exceptions and other anomolies
62 Comment : This is a sample subroutine header.
63 : It is polite to include more pod and fewer comments.
64  
65 See Also :
66  
67 =cut
68  
69 #################### subroutine header end ####################
70  
71 =head2 C<new()>
72  
73 Usage : $self->block_new_method() within text_pm_file()
74 Purpose : Build 'new()' method as part of a pm file
75 Returns : String holding sub new.
76 Argument : $module: pointer to the module being built
77 (as there can be more than one module built by EU::MM);
78 for the primary module it is a pointer to $self
79 Throws : n/a
80 Comment : This method is a likely candidate for alteration in a subclass,
81 e.g., pass a single hash-ref to new() instead of a list of
82 parameters.
83  
84 =cut
85  
86 sub new
87 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
88 1 $log->debug( "new", { progname => $0, pid => $$, perl_version => $], package => __PACKAGE__ } );
89 1 my ( $class, %parameters ) = @_;
90 1 my $self = $class->SUPER::new( %parameters );
91  
92 $self->{ beachmarck } = {
93 1 created => $^T,
94 started => undef,
95 finished => undef
96 };
97  
98 1 $self->{ configurations } = \%parameters;
99  
100 1 $self->_initialize( %parameters );
101 1 return $self;
102 } ## end sub new
103  
104 sub _initialize()
105 1 ▶{
Subroutine _initialize: called CC=1 SCAR=0.0
106 1 $log->debug( "_initialize", { package => __PACKAGE__ } );
107 1 my ( $self, %parameters ) = @_;
108 1 $self->SUPER::_initialize( %parameters );
109 }
110  
111 sub _finalize()
112 0 ▶{
Subroutine _finalize: not called CC=1 SCAR=6.9
113 0 $log->debug( "_finalize", { package => __PACKAGE__ } );
114 0 my ( $self, %parameters ) = @_;
115 0 $self->SUPER::_finalize( %parameters );
116 }
117  
118 =head2 C<loadApplication()>
119  
120 Usage : $self->block_new_method() within text_pm_file()
121 Purpose : Build 'new()' method as part of a pm file
122 Returns : String holding sub new.
123 Argument : $module: pointer to the module being built
124 (as there can be more than one module built by EU::MM);
125 for the primary module it is a pointer to $self
126 Throws : n/a
127 Comment : This method is a likely candidate for alteration in a subclass,
128 e.g., pass a single hash-ref to new() instead of a list of
129 parameters.
130  
131 =cut
132  
133 sub loadApplication()
134 0 ▶{
Subroutine loadApplication: not called CC=10 SCAR=47.0
Pod documented
135 0 my ( $self, %parameters ) = @_;
136 0 $log->debug( "loadApplication", { package => __PACKAGE__ } );
137 0 my $configurationFile = $self->{ configurations }->{ configurationFile };
138  
139 0 ▶ if ( !-e $configurationFile )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $configurationFile) 0 0 0
140 {
141 0 ( $configurationFile = $0 ) =~ s/\.pl/\-config\.json/;
142 0 $log->error( "Não existe o arquivo de configuração...", { package => __PACKAGE__ } );
143  
144 0 ▶ if ( prompt 'Não existe o arquivo de configuração, deseja criar agora ?', -yn1, -default => 'y' )
Branch: 1 branch
BranchTrueFalseTotal
if (prompt("N\343o existe o arquivo de configura\347\343o, deseja criar agora ?", '-yn1', '-default', 'y')) { } 0 0 0
145 {
146 0 my $config;
147  
148 0 $config->{ $configurationFile }->{ applicationName } = prompt( 'Application Name :', -v, -echostyle => 'bold white' );
149 0 $config->{ $configurationFile }->{ applicationModule } = prompt( 'Application Module :', -v, -echostyle => 'bold white' );
150 0 $config->{ $configurationFile }->{ environment } = prompt( 'Environment [desenv] :', -v, -echostyle => 'bold white', -default => 'desenv' );
151  
152 # Mail Config
153 $config->{ $configurationFile }->{ mail } = {
154 0 server => prompt( 'E-mail server [mail] :', -v, -echostyle => 'bold white', -default => 'mail' ),
155 port => prompt( 'E-mail port [25] :', -v, -echostyle => 'bold white', -default => '25' ),
156 debug => prompt( 'E-mail debug [0] :', -v, -echostyle => 'bold white', -default => '0' ),
157 from => prompt( 'E-mail from :', -v, -echostyle => 'bold white' ),
158 };
159  
160 0 my $json_text = $self->{ json }->pretty( 1 )->canonical( 1 )->encode( $config->{ $configurationFile } );
161  
162 0 open FH, ">", $configurationFile;
163 0 print FH $json_text;
164 0 close FH;
165 } ## end if ( prompt 'Não existe o arquivo de configuração, deseja criar agora ?'...)
166 else
167 {
168 0 return FALSE;
169 }
170 } ## end if ( !-e $configurationFile...)
171  
172 0 my @configFiles = ( $configurationFile );
173 0 my $configAny = Config::Any->load_files( { files => \@configFiles, flatten_to_hash => 1, use_ext => 1 } );
174  
175 0 foreach my $configFile ( keys %$configAny )
176 {
177 0 $self->{ configurations }->{ $_ } = $configAny->{ $configFile }->{ $_ } foreach ( keys %{ $configAny->{ $configFile } } );
178 }
179  
180 0 $log->info( "Configurações lidas com sucesso na aplicação [ $self->{ configurations }->{ application }->{ name } ]..." );
181  
182 0 ▶ $log->error( 'Falta passar nome do package' ) if ( $self->{ configurations }->{ application }->{ package } !~ /^\D/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"configurations"}{"application"}{"package"} =~ /^\D/u 0 0 0
183  
184 0 eval "use $self->{ configurations }->{ application }->{ fileName };"; ## Usando o módulo.
185  
186 0 ▶ if ( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
187 {
188 0 $log->error( "Problemas ao usar o arquivo [ $self->{ configurations }->{ application }->{ fileName } ]..." );
189 0 return FALSE;
190 }
191 else
192 {
193 0 $self->{ application }->{ instance } = eval { $self->{ configurations }->{ application }->{ package }->new( bootstrap => $self ); };
194 0 ▶ if ( !$self->{ application }->{ instance } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"application"}{"instance"}) 0 0 0
195 {
196 0 $log->error( "Erro ao iniciar o módulo [ $self->{ configurations }->{ application }->{ package } ]..." );
197 0 return FALSE;
198 }
199 } ## end else [ if ( $@ ) ]
200  
201 0 my $emailDispatcher = $log->{ adapter }->{ dispatcher }->{ outputs }->{ Email };
202  
203 0 ▶ if ( $emailDispatcher )
Branch: 1 branch
BranchTrueFalseTotal
if ($emailDispatcher) 0 0 0
204 {
205 0 my $mailConf = $self->{ configurations }->{ mail };
206  
207 0 ▶ if ( $mailConf )
Branch: 1 branch
BranchTrueFalseTotal
if ($mailConf) 0 0 0
208 {
209 0 eval { require DateTime; };
210 0 $emailDispatcher->{ to } = [ $mailConf->{ to } ];
211 0 $emailDispatcher->{ from } = $mailConf->{ from };
212 0 $emailDispatcher->{ host } = $mailConf->{ host };
213 0 $emailDispatcher->{ port } = $mailConf->{ port };
214 0 $emailDispatcher->{ subject } = "[ " . DateTime->now()->datetime( ' ' ) . " ] " . $self->{ configurations }->{ application }->{ name };
215 } ## end if ( $mailConf )
216  
217 } ## end if ( $emailDispatcher ...)
218  
219 0 return TRUE;
220 } ## end sub loadApplication
221  
222 =head2 C<run()>
223 =cut
224  
225 sub run
226 0 ▶{
Subroutine run: not called CC=3 SCAR=24.8
Pod documented
227 0 my ( $self, %parameters ) = @_;
228 0 my $retorno = {};
229  
230 0 ▶ if ( ref $self->{ application }->{ instance } eq $self->{ configurations }->{ application }->{ package } )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'application'}{'instance'} eq $self->{'configurations'}{'application'}{'package'}) { } 0 0 0
231 {
232 0 $self->{ beachmarck }->{ started } = time();
233 0 $retorno = eval { $self->{ application }->{ instance }->start(); };
234 0 $self->{ beachmarck }->{ finished } = time();
235 0 ▶ $log->error( "Problemas durante execucao de start: $@" ) if ( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
236 } ## end if ( ref $self->{ application...})
237 else
238 {
239 0 $log->error( "Impossivel de executar $self->{ configurations }->{ application }->{ package }::start\nSub nao existe" );
240 }
241  
242 0 return $retorno;
243 } ## end sub run
244  
245 =head2 C<getExecutionTime()>
246 =cut
247  
248 sub getExecutionTime()
249 0 ▶{
Subroutine getExecutionTime: not called CC=3 SCAR=24.8
Pod documented
250 0 my ( $self, %parameters ) = @_;
251 0 ▶ return ( ( $self->{ beachmarck }->{ finished } // 0 ) - ( $self->{ beachmarck }->{ started } // 0 ) );
Condition0%0/4
$self->{"beachmarck"}{"finished"} // 0
l!l
00
$self->{"beachmarck"}{"started"} // 0
l!l
00
MC/DC0%0/1
$self->{"beachmarck"}{"finished"} // 0
$self->{"beachmarck"}{"finished"}
MC/DC0%0/1
$self->{"beachmarck"}{"started"} // 0
$self->{"beachmarck"}{"started"}
Truth table0%0/2
A
A= $self->{"beachmarck"}{"finished"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"beachmarck"}{"started"}
Aresult
11
00
252 }
253  
254 =head2 C<getExecutionInfo()>
255 =cut
256  
257 sub getExecutionInfo()
258 0 ▶{
Subroutine getExecutionInfo: not called CC=1 SCAR=6.9
Pod documented
259 0 my ( $self, %parameters ) = @_;
260  
261 0 $log->info( "Quantidade de erro(s): 0" );
262 0 $log->info( "Tempo de execucao: " . $self->getExecutionTime() . " segundo(s)" );
263 0 $log->info( "Fim da aplicacao." );
264 } ## end sub getExecutionInfo
265  
266 sub END
267 1 ▶{
Subroutine END: called CC=1 SCAR=0.0
268 1 $log->debug( "END", { package => __PACKAGE__, line => __LINE__ } );
269 1 my ( $self, %parameters ) = @_;
270 1 eval { $log->{ adapter }->{ dispatcher }->{ outputs }->{ Email }->flush; };
271 }
272  
273 sub DESTROY
274 1 ▶{
Subroutine DESTROY: called CC=6 SCAR=26.2
275 1 my ( $self, %parameters ) = @_;
276 1 ▶ if ( ${^GLOBAL_PHASE} eq 'DESTRUCT' )
Branch: 1 branch
BranchTrueFalseTotal
if (${^GLOBAL_PHASE} eq "DESTRUCT") 0 1 1
277 {
278 0 ▶ $self->getExecutionInfo() if ( blessed( $self ) && $self->isa( __PACKAGE__ ) );
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($self) and $self->isa(__PACKAGE__) 0 0 0
Condition0%0/3
builtin::blessed($self) and $self->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self) and $self->isa(__PACKAGE__)
builtin::blessed($self) $self->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= builtin::blessed($self)B= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
279 0 return;
280 }
281  
282 1 ▶ if ( blessed( $self ) && $self->isa( __PACKAGE__ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self) and $self->isa(__PACKAGE__)) { } 1 0 1
Condition33%1/3
builtin::blessed($self) and $self->isa(__PACKAGE__)
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($self) and $self->isa(__PACKAGE__)
builtin::blessed($self) $self->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($self)B= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
283 {
284 1 $log->debug( "DESTROY", { package => __PACKAGE__, GLOBAL_PHASE => ${^GLOBAL_PHASE}, blessed => TRUE } );
285 }
286 else
287 {
288 0 $log->debug( 'DESTROY', { package => __PACKAGE__, GLOBAL_PHASE => ${^GLOBAL_PHASE}, blessed => FALSE } );
289 }
290  
291 1 Siffra::Bootstrap->SUPER::DESTROY;
292 } ## end sub DESTROY
293  
294 #################### main pod documentation begin ###################
295 ## Below is the stub of documentation for your module.
296 ## You better edit it!
297  
298 =encoding UTF-8
299  
300  
301 =head1 NAME
302  
303 Siffra::Bootstrap - Module abstract (<= 44 characters) goes here
304  
305 =head1 SYNOPSIS
306  
307 use Siffra::Bootstrap;
308 blah blah blah
309  
310  
311 =head1 DESCRIPTION
312  
313 Stub documentation for this module was created by ExtUtils::ModuleMaker.
314 It looks like the author of the extension was negligent enough
315 to leave the stub unedited.
316  
317 Blah blah blah.
318  
319  
320 =head1 USAGE
321  
322  
323  
324 =head1 BUGS
325  
326  
327  
328 =head1 SUPPORT
329  
330  
331  
332 =head1 AUTHOR
333  
334 Luiz Benevenuto
335 CPAN ID: LUIZBENE
336 Siffra TI
337 luiz@siffra.com.br
338 https://siffra.com.br
339  
340 =head1 COPYRIGHT
341  
342 This program is free software; you can redistribute
343 it and/or modify it under the same terms as Perl itself.
344  
345 The full text of the license can be found in the
346 LICENSE file included with this module.
347  
348  
349 =head1 SEE ALSO
350  
351 perl(1).
352  
353 =cut
354  
355 #################### main pod documentation end ###################
356  
357 2 1;
358  
359 # The preceding line will help the module return a true value
360  
361 __DATA__
↑ Summary