« blib/lib/SignalWire/Agents/Prefabs/Concierge.pm
↑ Summary
blib/lib/SignalWire/Agents/SWML/Service.pm »
| 1 | package SignalWire::Agents::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::Agents::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
| |||||||||||||||||||||||||||||
| 36 | 2 | $self->_on_completed($cb); | |||||||||||||||||||||||||||
| 37 | 2 | ▶ | if ($self->completed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | eval { $cb->($self) }; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | warn "Message on_completed callback error: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$opts{'timeout'} || 30
$opts{'timeout'}
Truth table0%0/2
A
A= $opts{'timeout'}
| |||||||||||||||||||||||||||||
| 57 | 0 | my $start = time(); | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | while (!$self->completed && (time() - $start) < $timeout) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->completed and time - $start < $timeout
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | 9 | ▶ | $self->state($message_state) if $message_state; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Update fields from event | ||||||||||||||||||||||||||||
| 72 | 9 | ▶ | $self->reason($event->reason) if $event->can('reason') && $event->reason; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$event->can("reason") and $event->reason
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Check for terminal state | ||||||||||||||||||||||||||||
| 81 | 9 | ▶ | if (MESSAGE_TERMINAL_STATES->{$message_state}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 5 | $self->completed(1); | |||||||||||||||||||||||||||
| 83 | 5 | $self->result($event); | |||||||||||||||||||||||||||
| 84 | 5 | ▶ | if (my $cb = $self->_on_completed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 2 | eval { $cb->($self) }; | |||||||||||||||||||||||||||
| 86 | 2 | ▶ | warn "Message on_completed callback error: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 3 | 1; | |||||||||||||||||||||||||||