blib/lib/SignalWire/Relay/Message.pm

filter: Statementstmt 77.2% 61 / 79 Branchbran 45.4% 10 / 22 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 86.9% 20 / 23 Podpod 0.0% 0 / 5 totaltotal 64.7% 92 / 142 CC 16 SCAR 19.4
CC
16
Cov
66%
wait
20.0
dispatch_event
13.7
on_completed
6.4
CRAP 6.9
« blib/lib/SignalWire/Prefabs/Concierge.pm ↑ Summary blib/lib/SignalWire/Core/LoggingConfig.pm »
1 package SignalWire::Relay::Message;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use SignalWire::Relay::Constants qw(MESSAGE_TERMINAL_STATES);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 has 'message_id' => ( is => 'ro', required => 1 );
9 9 ▶has 'context' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
10 0 ▶has 'direction' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
11 7 ▶has 'from_number' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
12 7 ▶has 'to_number' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
13 7 ▶has 'body' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
14 9 ▶has 'media' => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
15 9 ▶has 'segments' => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
16 9 ▶has 'state' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
17 9 ▶has 'reason' => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
18 9 ▶has 'tags' => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 9 ▶has 'completed' => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
21 9 ▶has 'result' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
22  
23 9 ▶has '_on_completed' => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
24 9 ▶has '_on_event' => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 # Check if message has reached a terminal state
27 10 ▶sub is_done {
Subroutine is_done: called CC=1 SCAR=0.0
Pod undocumented
28 10 my ($self) = @_;
29 10 return $self->completed;
30 }
31  
32 # Register on_completed callback
33 2 ▶sub on_completed {
Subroutine on_completed: called CC=4 SCAR=18.6
Pod undocumented
34 2 my ($self, $cb) = @_;
35 2 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 2 0 2
36 2 $self->_on_completed($cb);
37 2 ▶ if ($self->completed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->completed) 0 2 2
38 0 eval { $cb->($self) };
39 0 ▶ warn "Message on_completed callback error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
40 }
41 2 return $self;
42 }
43 0 return $self->_on_completed;
44 }
45  
46 # Register event listener
47 0 ▶sub on {
Subroutine on: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my ($self, $cb) = @_;
49 0 push @{$self->_on_event}, $cb;
50 0 return $self;
51 }
52  
53 # Blocking wait
54 0 ▶sub wait {
Subroutine wait: not called CC=4 SCAR=30.0
Pod undocumented
55 0 my ($self, %opts) = @_;
56 0 ▶ my $timeout = $opts{timeout} || 30;
Condition0%0/2
$opts{'timeout'} || 30
l!l
00
MC/DC0%0/1
$opts{'timeout'} || 30
$opts{'timeout'}
Truth table0%0/2
A
A= $opts{'timeout'}
Aresult
11
00
57 0 my $start = time();
58 0 ▶ while (!$self->completed && (time() - $start) < $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->completed and time - $start < $timeout) 0 0 0
Condition0%0/3
not $self->completed and time - $start < $timeout
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->completed and time - $start < $timeout
not $self->completed time - $start < $timeout
Truth table0%0/3
not(A) and B
A= not $self->completedB= time - $start < $timeout
ABresult
0X0
100
111
59 0 select(undef, undef, undef, 0.1);
60 }
61 0 return $self->result;
62 }
63  
64 # Handle a messaging.state event
65 9 ▶sub dispatch_event {
Subroutine dispatch_event: called CC=10 SCAR=26.2
Pod undocumented
66 9 my ($self, $event) = @_;
67  
68 9 ▶ my $message_state = $event->can('message_state') ? $event->message_state : '';
Branch: 1 branch
BranchTrueFalseTotal
$event->can('message_state') ? : 9 0 9
69 9 ▶ $self->state($message_state) if $message_state;
Branch: 1 branch
BranchTrueFalseTotal
if $message_state 9 0 9
70  
71 # Update fields from event
72 9 ▶ $self->reason($event->reason) if $event->can('reason') && $event->reason;
Branch: 1 branch
BranchTrueFalseTotal
if $event->can("reason") and $event->reason 0 9 9
Condition33%1/3
$event->can("reason") and $event->reason
!ll&&!rl&&r
090
MC/DC0%0/2
$event->can("reason") and $event->reason
$event->can("reason") $event->reason
Truth table33%1/3
A and B
A= $event->can("reason")B= $event->reason
ABresult
0X0
100
111
73  
74 # Fire event callbacks
75 9 for my $cb (@{$self->_on_event}) {
76 0 eval { $cb->($self, $event) };
77 0 ▶ warn "Message event callback error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
78 }
79  
80 # Check for terminal state
81 9 ▶ if (MESSAGE_TERMINAL_STATES->{$message_state}) {
Branch: 1 branch
BranchTrueFalseTotal
if ({"delivered" => 1,"failed" => 1,"undelivered" => 1}->{$message_state}) 5 4 9
82 5 $self->completed(1);
83 5 $self->result($event);
84 5 ▶ if (my $cb = $self->_on_completed) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->_on_completed) 2 3 5
85 2 eval { $cb->($self) };
86 2 ▶ warn "Message on_completed callback error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
87 }
88 }
89 }
90  
91 3 1;
« blib/lib/SignalWire/Prefabs/Concierge.pm ↑ Summary blib/lib/SignalWire/Core/LoggingConfig.pm »