blib/lib/AnyEvent/Timer/Cron.pm

filter: Statementstmt 52.8% 28 / 53 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 7 / 14 Podpod 0.0% 0 / 3 totaltotal 39.3% 35 / 89 CC 9 SCAR 20.9
CC
9
Cov
40%
__ANON__
20.0
create_timer
12.0
next_event
12.0
CRAP 8.0
↑ Summary
1 package AnyEvent::Timer::Cron;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = '0.002000';
5 1 $VERSION = eval $VERSION;
6  
7 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DateTime::Event::Cron;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 has 'cb' => (is => 'ro', required => 1);
15 1 has 'time_zone' => (is => 'ro');
16 has '_cron' => (
17 is => 'ro',
18 required => 1,
19 init_arg => 'cron',
20 0 ▶ coerce => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
21 0 my $cron = shift;
22 0 ▶ if (!ref $cron) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $cron) 0 0 0
23 0 $cron = DateTime::Event::Cron->new($cron);
24 }
25 0 ▶ if ($cron->$_can('next')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cron->$AnyEvent::Timer::Cron::_can('next')) { } 0 0 0
elsif ($cron->$AnyEvent::Timer::Cron::_can('get_next_valid_time_after')) { } 0 0 0
26 0 ▶ return sub { $cron->next(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 }
28 elsif ($cron->$_can('get_next_valid_time_after')) {
29 0 ▶ return sub { $cron->get_next_valid_time_after(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 }
31 0 die "Invalid cron!";
32 },
33 1 );
34 1 has '_timer' => (is => 'rw');
35  
36 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my $self = shift;
38 0 $self->create_timer;
39 }
40  
41 0 ▶sub create_timer {
Subroutine create_timer: not called CC=3 SCAR=24.8
Pod undocumented
42 0 my $self = shift;
43 0 weaken $self;
44 0 my $now = DateTime->from_epoch(epoch => AnyEvent->now);
45 0 ▶ $now->set_time_zone( $self->time_zone ) if $self->time_zone;
Branch: 1 branch
BranchTrueFalseTotal
if $self->time_zone 0 0 0
46 0 my $next = $self->next_event($now);
47 return
48 0 ▶ if not $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 0 0 0
49 0 my $interval = $next->subtract_datetime_absolute($now)->in_units('nanoseconds') / 1_000_000_000;
50 $self->_timer(AnyEvent->timer(
51 after => $interval,
52 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
53 0 $self->{cb}->();
54 0 ▶ $self && $self->create_timer;
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->create_timer 0 0 0
55 },
56 0 ));
57 }
58  
59 0 ▶sub next_event {
Subroutine next_event: not called CC=3 SCAR=24.8
Pod undocumented
60 0 my $self = shift;
61 0 ▶ my $now = shift || DateTime->from_epoch(epoch => AnyEvent->now);
Condition0%0/3
shift() || 'DateTime'->from_epoch('epoch', 'AnyEvent'->now)
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || 'DateTime'->from_epoch('epoch', 'AnyEvent'->now)
shift() 'DateTime'->from_epoch('epoch', 'AnyEvent'->now)
Truth table0%0/3
A || B
A= shift()B= 'DateTime'->from_epoch('epoch', 'AnyEvent'->now)
ABresult
1X1
011
000
62 0 ▶ $now->set_time_zone( $self->time_zone ) if $self->time_zone;
Branch: 1 branch
BranchTrueFalseTotal
if $self->time_zone 0 0 0
63 0 $self->_cron->($now);
64 }
65  
66 1 1;
67 __END__
↑ Summary