blib/lib/AnyEvent/APNS.pm

filter: Statementstmt 87.0% 121 / 139 Branchbran 64.7% 22 / 34 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 79.3% 23 / 29 Podpod 100% 2 / 2 totaltotal 79.4% 170 / 214 CC 21 SCAR 12.6
CC
21
Cov
81%
__ANON__
7.2
send
6.1
__ANON__
6.0
CRAP 3.5
↑ Summary
1 package AnyEvent::APNS;
2 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use AnyEvent 4.80;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use AnyEvent::TLS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 require bytes;
11 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 our $VERSION = '0.10';
17  
18 5 has certificate => (
19 is => 'rw',
20 isa => 'Str | ScalarRef',
21 required => 1,
22 );
23  
24 5 has private_key => (
25 is => 'rw',
26 isa => 'Str | ScalarRef',
27 required => 1,
28 );
29  
30 5 has sandbox => (
31 is => 'rw',
32 isa => 'Bool',
33 default => 0,
34 );
35  
36 5 has handler => (
37 is => 'rw',
38 isa => 'AnyEvent::Handle',
39 predicate => 'connected',
40 clearer => 'clear_handler',
41 );
42  
43 has json_driver => (
44 is => 'rw',
45 isa => 'Object',
46 lazy => 1,
47 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
48 3 JSON::Any->new( utf8 => 1 );
49 },
50 5 );
51  
52 has on_error => (
53 is => 'rw',
54 isa => 'CodeRef',
55 0 ▶ default => sub { sub { warn @_ } },
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
56 5 );
57  
58 5 has on_eof => (
59 is => 'rw',
60 isa => 'CodeRef',
61 );
62  
63 has on_connect => (
64 is => 'rw',
65 isa => 'CodeRef',
66 4 ▶ default => sub { sub {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
67 5 );
68  
69 5 has on_error_response => (
70 is => 'rw',
71 isa => 'CodeRef',
72 );
73  
74 5 has debug_port => (
75 is => 'rw',
76 isa => 'Int',
77 predicate => 'is_debug',
78 );
79  
80 5 has _con_guard => (
81 is => 'rw',
82 isa => 'Object',
83 );
84  
85 has last_identifier => (
86 is => 'rw',
87 isa => 'Int',
88 7 ▶ default => sub { 0; }
Subroutine __ANON__: called CC=1 SCAR=0.0
89 5 );
90  
91 5 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 3 ▶sub send {
Subroutine send: called CC=6 SCAR=18.1
Pod documented
94 3 my $self = shift;
95 3 my ($token, $payload, $expiry) = @_;
96  
97 3 my $json = encode_utf8( $self->json_driver->encode($payload) );
98  
99 # http://developer.apple.com/library/ios/#DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html
100 # Expiry—A fixed UNIX epoch date expressed in seconds (UTC) that identifies when the notification is no longer valid and can be discarded. The expiry value should be in network order (big endian). If the expiry value is positive, APNs tries to deliver the notification at least once. You can specify zero or a value less than zero to request that APNs not store the notification at all.
101 # default to 24 hours
102 3 ▶ $expiry = defined $expiry ? $expiry : time() + 3600 * 24;
Branch: 1 branch
BranchTrueFalseTotal
defined $expiry ? : 0 3 3
103  
104 # Identifier—An arbitrary value that identifies this notification. This same identifier is returned in a error-response packet if APNs cannot interpret a notification.
105 3 my $next_identifier = $self->_increment_identifier;
106  
107 3 my $h = $self->handler;
108  
109 3 $h->push_write( pack('C', 1) ); # command
110 3 $h->push_write( pack('N', $next_identifier) );
111 3 $h->push_write( pack('N', $expiry) );
112 3 $h->push_write( pack('n', bytes::length($token)) ); # token length
113 3 $h->push_write( $token ); # device token
114  
115 # Apple Push Notification Service refuses string values as badge number
116 3 ▶ if ($payload->{aps}{badge} && looks_like_number($payload->{aps}{badge})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"aps"}{"badge"} and &looks_like_number($payload->{"aps"}{"badge"})) 0 3 3
Condition33%1/3
$payload->{"aps"}{"badge"} and &looks_like_number($payload->{"aps"}{"badge"})
!ll&&!rl&&r
300
MC/DC0%0/2
$payload->{"aps"}{"badge"} and &looks_like_number($payload->{"aps"}{"badge"})
$payload->{"aps"}{"badge"} &looks_like_number($payload->{"aps"}{"badge"})
Truth table33%1/3
A and B
A= $payload->{"aps"}{"badge"}B= &looks_like_number($payload->{"aps"}{"badge"})
ABresult
0X0
100
111
117 0 $payload->{aps}{badge} += 0;
118 }
119  
120 # The maximum size allowed for a notification payload is 256 bytes;
121 # Apple Push Notification Service refuses any notification that exceeds this limit.
122 3 ▶ if ( (my $exceeded = bytes::length($json) - 256) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $exceeded = &bytes::length($json) - 256) > 0) 2 1 3
123 2 ▶ if (ref $payload->{aps}{alert} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $payload->{'aps'}{'alert'} eq 'HASH') { } 1 1 2
124 $payload->{aps}{alert}{body} =
125 1 $self->_trim_utf8($payload->{aps}{alert}{body}, $exceeded);
126 }
127 else {
128 1 $payload->{aps}{alert} = $self->_trim_utf8($payload->{aps}{alert}, $exceeded);
129 }
130  
131 2 $json = encode_utf8( $self->json_driver->encode($payload) );
132 }
133  
134 3 $h->push_write( pack('n', bytes::length($json)) ); # payload length
135 3 $h->push_write( $json ); # payload
136  
137 3 return $next_identifier;
138 }
139  
140 2 ▶sub _trim_utf8 {
Subroutine _trim_utf8: called CC=4 SCAR=13.9
141 2 my ($self, $string, $trim_length) = @_;
142  
143 2 my $string_bytes = encode_utf8($string);
144 2 my $trimmed = '';
145  
146 2 my $start_length = bytes::length($string_bytes) - $trim_length;
147 2 ▶ return $trimmed if $start_length <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start_length <= 0 0 2 2
148  
149 2 for my $len ( reverse $start_length - 6 .. $start_length ) {
150 6 local $@;
151 6 eval {
152 6 $trimmed = decode_utf8(substr($string_bytes, 0, $len), Encode::FB_CROAK);
153 };
154 6 ▶ last if $trimmed;
Branch: 1 branch
BranchTrueFalseTotal
if $trimmed 2 4 6
155 }
156  
157 2 return $trimmed;
158 }
159  
160 5 ▶sub connect {
Subroutine connect: called CC=5 SCAR=16.6
Pod documented
161 5 my $self = shift;
162  
163 5 ▶ if ($self->connected && $self->handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connected and $self->handler) 0 5 5
Condition33%1/3
$self->connected and $self->handler
!ll&&!rl&&r
500
MC/DC0%0/2
$self->connected and $self->handler
$self->connected $self->handler
Truth table33%1/3
A and B
A= $self->connectedB= $self->handler
ABresult
0X0
100
111
164 0 warn 'Already connected!';
165 0 return;
166 }
167  
168 5 ▶ my $host = $self->sandbox
Branch: 1 branch
BranchTrueFalseTotal
$self->sandbox ? : 0 5 5
169 ? 'gateway.sandbox.push.apple.com'
170 : 'gateway.push.apple.com';
171 5 my $port = 2195;
172  
173 5 ▶ if ($self->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_debug) 5 0 5
174 5 $host = '127.0.0.1';
175 5 $port = $self->debug_port;
176 }
177 5 ▶ my $g = tcp_connect $host, $port, sub {
Subroutine __ANON__: called CC=7 SCAR=19.7
178 5 ▶ my ($fh) = @_
Branch: 1 branch
BranchTrueFalseTotal
my($fh) = or return $self->on_error->(undef, 1, $!) 5 0 5
179 or return $self->on_error->(undef, 1, $!);
180  
181 5 my $tls_setting = {};
182 5 ▶ if (ref $self->certificate) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->certificate) { } 2 3 5
183 2 $tls_setting->{cert} = ${ $self->certificate };
184 }
185 else {
186 3 $tls_setting->{cert_file} = $self->certificate;
187 }
188  
189 5 ▶ if (ref $self->private_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->private_key) { } 2 3 5
190 2 $tls_setting->{key} = ${ $self->private_key };
191 }
192 else {
193 3 $tls_setting->{key_file} = $self->private_key;
194 }
195  
196 my $handle = AnyEvent::Handle->new(
197 fh => $fh,
198 1 ▶ on_error => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
199 1 $self->on_error->(@_);
200 1 $self->clear_handler;
201 1 $_[0]->destroy;
202 },
203 5 ▶ !$self->is_debug ? (
Branch: 1 branch
BranchTrueFalseTotal
!$self->is_debug ? : 0 5 5
204 tls => 'connect',
205 tls_ctx => $tls_setting,
206 ) : (),
207 );
208 5 $self->handler( $handle );
209  
210 5 ▶ if ($self->on_eof) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_eof) 1 4 5
211 1 ▶ $handle->on_eof(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
212 1 $self->on_eof->(@_);
213 1 $self->clear_handler;
214 1 $_[0]->destroy;
215 1 });
216 }
217  
218 5 ▶ if ( $self->on_error_response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_error_response) { } 0 5 5
219 $handle->on_read(
220 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 0 $self->_on_read_with_error_callback( @_ );
222 }
223 0 );
224 }
225 else {
226 0 ▶ $handle->on_read( sub { delete $_[0]->{rbuf} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
227 }
228  
229 5 $self->on_connect->();
230 5 };
231  
232 5 Scalar::Util::weaken($self);
233 5 $self->_con_guard($g);
234  
235 5 $self;
236 }
237  
238 0 ▶sub _on_read_with_error_callback {
Subroutine _on_read_with_error_callback: not called CC=1 SCAR=6.9
239 0 my ($self, $handle) = @_;
240 $handle->push_read( chunk => 1,
241 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
242 0 my $command = unpack( 'C', $_[1] );
243 0 ▶ if ( $command != 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command != 8) 0 0 0
244 # something is wrong
245 # auto reconnect
246 0 $self->clear_handler;
247 0 $self->connect;
248 }
249 0 });
250 $handle->push_read( chunk => 5,
251 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
252 0 my $status = unpack( 'C', substr( $_[1], 0, 1) );
253 0 my $identifier = unpack( 'N', substr( $_[1], 1, 4) );
254 0 $self->on_error_response->( $identifier => $status );
255 0 });
256 }
257  
258 # 0 ... 2**32-1, 0 ... 2**32-1, 0 ...
259 3 ▶sub _increment_identifier {
Subroutine _increment_identifier: called CC=2 SCAR=7.4
260 3 my ($self) = @_;
261 3 my $next_identifier = $self->last_identifier + 1;
262 3 ▶ if ( $next_identifier >= 2 ** 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_identifier >= "4294967296") 0 3 3
263 # identifier is only 4 bytes
264 0 $next_identifier = 0;
265 }
266 3 $self->last_identifier( $next_identifier );
267 }
268  
269 5 __PACKAGE__->meta->make_immutable;
270  
271 __END__
↑ Summary