blib/lib/APNS/Agent.pm

filter: Statementstmt 66.6% 100 / 150 Branchbran 34.2% 13 / 38 Conditioncond 4.7% 1 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 75.6% 28 / 37 Podpod 0.0% 0 / 3 totaltotal 54.4% 142 / 261 CC 28 SCAR 20.1
CC
28
Cov
55%
run
30.0
__ANON__
20.0
parse_options
20.0
CRAP 7.5
↑ Summary
1 package APNS::Agent;
2 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = "0.06";
7  
8 2 ▶use AnyEvent::APNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Cache::LRU;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Encode qw/decode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Log::Minimal;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Router::Boom::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Class::Accessor::Lite::Lazy 0.03 (
Subroutine BEGIN: called CC=1 SCAR=1.0
17 new => 1,
18 ro => [qw/
19 certificate
20 private_key
21 sandbox
22 debug_port
23 /],
24 ro_lazy => {
25 0 ▶ on_error_response => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
27 0 my $self = shift;
28 0 my %d = %{$_[0]};
29 0 ▶ warnf "identifier:%s\tstate:%s\ttoken:%s", $d{identifier}, $d{state}, $d{token} || '';
Condition0%0/2
$d{'token'} || ''
l!l
00
MC/DC0%0/1
$d{'token'} || ''
$d{'token'}
Truth table0%0/2
A
A= $d{'token'}
Aresult
11
00
30 }
31 0 },
32 1 ▶ disconnect_interval => sub { 60 },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 ▶ send_interval => sub { 0.01 },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 1 ▶ _sent_cache => sub { Cache::LRU->new(size => 10000) },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 1 ▶ _queue => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
36 __apns => '_build_apns',
37 1 ▶ _sent => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 },
39 2 rw => [qw/_last_sent_at _disconnect_timer/],
40 2 );
41  
42 1 ▶sub to_app {
Subroutine to_app: called CC=1 SCAR=0.0
Pod undocumented
43 1 my $self = shift;
44  
45 1 my $router = Router::Boom::Method->new;
46 1 $router->add(POST => '/' => '_do_main');
47 1 $router->add(GET => '/monitor' => '_do_monitor');
48  
49 3 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
50 3 my $env = shift;
51 3 my ($target_method) = $router->match(@$env{qw/REQUEST_METHOD PATH_INFO/});
52  
53 3 ▶ return [404, [], ['NOT FOUND']] unless $target_method;
Branch: 1 branch
BranchTrueFalseTotal
unless $target_method 0 3 3
54  
55 3 my $req = Plack::Request->new($env);
56 3 $self->$target_method($req);
57 1 };
58 }
59  
60 1 ▶sub _do_main {
Subroutine _do_main: called CC=7 SCAR=23.3
61 1 my ($self, $req) = @_;
62  
63 1 ▶ my $token = $req->param('token') or return [400, [], ['Bad Request']];
Branch: 1 branch
BranchTrueFalseTotal
my $token = $req->param("token") or return [400, [], ["Bad Request"]] 1 0 1
64  
65 1 my $payload;
66 1 ▶ if (my $payload_json = $req->param('payload') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $payload_json = $req->param('payload')) { } 0 1 1
elsif (my $alert = $req->param('alert')) { } 1 0 1
67 0 state $json_driver = JSON::XS->new->utf8;
68 0 local $@;
69 0 $payload = eval { $json_driver->decode($payload_json) };
70 0 ▶ return [400, [], ['BAD REQUEST']] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
71 }
72 elsif (my $alert = $req->param('alert')) {
73 1 $payload = +{
74 alert => decode_utf8($alert),
75 };
76 }
77 1 ▶ return [400, [], ['BAD REQUEST']] unless $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless $payload 0 1 1
78  
79 1 my @payloads = map {[$_, $payload]} split /,/, $token;
80 1 push @{$self->_queue}, @payloads;
81  
82 1 infof "event:payload queued\ttoken:%s", $token;
83 1 ▶ if ($self->__apns->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->__apns->connected) { } 0 1 1
84 0 $self->_sending;
85 }
86 else {
87 1 $self->_connect_to_apns;
88 }
89 1 return [200, [], ['Accepted']];
90 }
91  
92 2 ▶sub _do_monitor {
Subroutine _do_monitor: called CC=1 SCAR=0.0
93 2 my ($self, $req) = @_;
94  
95 my $result = {
96 sent => $self->_sent,
97 2 queued => scalar( @{ $self->_queue } ),
98 };
99 2 my $body = encode_json($result);
100  
101 2 return [200, [
102 'Content-Type' => 'application/json; charset=utf-8',
103 'Content-Length' => length($body),
104 ], [$body]];
105 }
106  
107 1 ▶sub _build_apns {
Subroutine _build_apns: called CC=2 SCAR=10.7
108 1 my $self = shift;
109  
110 AnyEvent::APNS->new(
111 certificate => $self->certificate,
112 private_key => $self->private_key,
113 sandbox => $self->sandbox,
114 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 my ($handle, $fatal, $message) = @_;
116  
117 0 my $t; $t = AnyEvent->timer(
118 after => 0,
119 interval => 10,
120 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 0 undef $t;
122 0 infof "event:reconnect";
123 0 $self->_connect_to_apns;
124 },
125 0 );
126 0 warnf "event:error\tfatal:$fatal\tmessage:$message";
127 },
128 1 ▶ on_connect => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
129 1 infof "event:on_connect";
130 1 $self->_disconnect_timer($self->_build_disconnect_timer);
131  
132 1 ▶ if (@{$self->_queue}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_queue;}) 1 0 1
133 1 $self->_sending;
134 }
135 },
136 0 ▶ on_error_response => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
137 0 my ($identifier, $state) = @_;
138 0 ▶ my $data = $self->_sent_cache->get($identifier) || {};
Condition0%0/2
$self->_sent_cache->get($identifier) || {}
l!l
00
MC/DC0%0/1
$self->_sent_cache->get($identifier) || {}
$self->_sent_cache->get($identifier)
Truth table0%0/2
A
A= $self->_sent_cache->get($identifier)
Aresult
11
00
139 $self->on_error_response->($self, {
140 identifier => $identifier,
141 state => $state,
142 token => $data->{token},
143 payload => $data->{payload},
144 0 });
145 },
146 1 ▶ ($self->debug_port ? (debug_port => $self->debug_port) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$self->debug_port ? : 1 0 1
147 );
148 }
149  
150 2 ▶sub _apns {
Subroutine _apns: called CC=2 SCAR=6.9
151 2 my $self = shift;
152  
153 2 my $apns = $self->__apns;
154 2 ▶ $apns->connect unless $apns->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $apns->connected 1 1 2
155 2 $apns;
156 }
157 1 ▶sub _connect_to_apns { goto \&_apns }
Subroutine _connect_to_apns: called CC=1 SCAR=0.0
158  
159 1 ▶sub _build_disconnect_timer {
Subroutine _build_disconnect_timer: called CC=2 SCAR=13.3
160 1 my $self = shift;
161  
162 1 ▶ if (my $interval = $self->disconnect_interval) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $interval = $self->disconnect_interval) { } 1 0 1
163 AnyEvent->timer(
164 after => $interval,
165 interval => $interval,
166 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
167 0 ▶ if ($self->{__apns} && (time - ($self->_last_sent_at || 0) > $interval)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__apns"} and time - ($self->_last_sent_at || 0) > $interval) 0 0 0
Condition0%0/5
$self->{"__apns"} and time - ($self->_last_sent_at || 0) > $interval
!ll&&!rl&&r
000
$self->_last_sent_at || 0
l!l
00
MC/DC0%0/2
$self->{"__apns"} and time - ($self->_last_sent_at || 0) > $interval
$self->{"__apns"} time - ($self->_last_sent_at || 0) > $interval
MC/DC0%0/1
$self->_last_sent_at || 0
$self->_last_sent_at
Truth table0%0/3
A and B
A= $self->{"__apns"}B= time - ($self->_last_sent_at || 0) > $interval
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->_last_sent_at
Aresult
11
00
168 0 delete $self->{__apns};
169 0 delete $self->{_disconnect_timer};
170 0 infof "event:close apns";
171 }
172 },
173 1 );
174 }
175 0 else { undef }
176 }
177  
178 1 ▶sub _sending {
Subroutine _sending: called CC=2 SCAR=7.6
179 1 my $self = shift;
180  
181 $self->{_send_timer} ||= AnyEvent->timer(
182 after => $self->send_interval,
183 interval => $self->send_interval,
184 1 ▶ cb => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
185 1 my $msg = shift @{ $self->_queue };
186 1 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) { } 1 0 1
187 1 $self->_send(@$msg);
188 }
189 else {
190 0 delete $self->{_send_timer};
191 }
192 },
193 1 ▶ );
Condition33%1/3
$self->{"_send_timer"} ||= "AnyEvent"->timer("after", $self->send_interval, "interval", $self->send_interval, "cb", sub { my $msg = shift @{$self->_queue;}; if ($msg) { $self->_send(@$msg); } else { delete $self->{"_send_timer"}; } ; } )
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"_send_timer"} ||= "AnyEvent"->timer("after", $self->send_interval, "interval", $self->send_interval, "cb", sub { my $msg = shift @{$self->_queue;}; if ($msg) { $self->_send(@$msg); } else { delete $self->{"_send_timer"}; } ; } )
$self->{"_send_timer"} "AnyEvent"->timer("after", $self->send_interval, "interval", $self->send_interval, "cb", sub { my $msg = shift @{$self->_queue;}; if ($msg) { $self->_send(@$msg); } else { delete $self->{"_send_timer"}; } ; } )
Truth table33%1/3
A ||= B
A= $self->{"_send_timer"}B= "AnyEvent"->timer("after", $self->send_interval, "interval", $self->send_interval, "cb", sub { my $msg = shift @{$self->_queue;}; if ($msg) { $self->_send(@$msg); } else { delete $self->{"_send_timer"}; } ; } )
ABresult
1X1
011
000
194 }
195  
196 1 ▶sub _send {
Subroutine _send: called CC=3 SCAR=13.1
197 1 my ($self, $token, $payload) = @_;
198  
199 1 local $@;
200 1 my $identifier;
201 1 eval {
202 1 $identifier = $self->_apns->send(pack("H*", $token) => {
203 aps => $payload,
204 });
205 };
206  
207 1 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) { } 0 1 1
208 0 ▶ if ($err =~ m!Can't call method "push_write" on an undefined value!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /Can't call method "push_write" on an undefined value/) { } 0 0 0
209 # AnyEvent::APNS->handle is missing
210 0 delete $self->{_send_timer};
211 0 unshift @{ $self->_queue }, [$token, $payload];
212 0 $self->_connect_to_apns;
213 }
214 else {
215 0 die $err;
216 }
217 }
218 else {
219 1 $self->_sent_cache->set($identifier => {
220 token => $token,
221 payload => $payload,
222 });
223 1 $self->_last_sent_at(time);
224 1 infof "event:send\ttoken:$token\tidentifier:$identifier";
225 1 $self->{_sent}++;
226 1 $identifier;
227 }
228 }
229  
230 0 ▶sub parse_options {
Subroutine parse_options: not called CC=4 SCAR=30.0
Pod undocumented
231 0 my ($class, @argv) = @_;
232  
233 0 require Getopt::Long;
234 0 require Pod::Usage;
235 0 require Hash::Rename;
236  
237 0 my $p = Getopt::Long::Parser->new(
238 config => [qw/posix_default no_ignore_case auto_help pass_through bundling/]
239 );
240 0 ▶ $p->getoptionsfromarray(\@argv, \my %opt, qw/
Branch: 1 branch
BranchTrueFalseTotal
$p->getoptionsfromarray(\@argv, \my %opt, ("certificate=s", "private-key=s", "disconnect-interval=i", "sandbox!", "debug-port=i")) or Pod::Usage::pod2usage() 0 0 0
241 certificate=s
242 private-key=s
243 disconnect-interval=i
244 sandbox!
245 debug-port=i
246 /) or Pod::Usage::pod2usage();
247 0 ▶ Pod::Usage::pod2usage() if !$opt{certificate} || !$opt{'private-key'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"certificate"} and $opt{"private-key"} 0 0 0
Condition0%0/3
$opt{"certificate"} and $opt{"private-key"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opt{"certificate"} and $opt{"private-key"}
$opt{"certificate"} $opt{"private-key"}
Truth table0%0/3
A and B
A= $opt{"certificate"}B= $opt{"private-key"}
ABresult
0X0
100
111
248  
249 0 ▶ Hash::Rename::hash_rename(\%opt, code => sub {tr/-/_/});
Subroutine __ANON__: not called CC=1 SCAR=6.9
250 0 (\%opt, \@argv);
251 }
252  
253 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
254 0 my $self = shift;
255 0 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
256 0 ▶ if (!$args{listen} && !$args{port} && !$ENV{SERVER_STARTER_PORT}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"listen"} or $args{"port"} or $ENV{"SERVER_STARTER_PORT"}) 0 0 0
Condition0%0/6
$args{"listen"} or $args{"port"} or $ENV{"SERVER_STARTER_PORT"}
l!l&&r!l&&!r
000
$args{"listen"} or $args{"port"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{"listen"} or $args{"port"} or $ENV{"SERVER_STARTER_PORT"}
$args{"listen"} $args{"port"} $ENV{"SERVER_STARTER_PORT"}
Truth table0%0/4
A or B or C
A= $args{"listen"}B= $args{"port"}C= $ENV{"SERVER_STARTER_PORT"}
ABCresult
1XX1
01X1
0011
0000
257 0 $args{port} = 4905;
258 }
259 0 require Plack::Loader;
260 0 Plack::Loader->load(Twiggy => %args)->run($self->to_app);
261 }
262  
263 2 1;
264 __END__
↑ Summary