blib/lib/Net/APNs/HTTP2.pm

filter: Statementstmt 37.0% 37 / 100 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 45.8% 11 / 24 Podpod 100% 3 / 3 totaltotal 28.0% 51 / 182 SCAR 52.1
CC
20
Cov
26%
prepare
42.0
close
30.0
__ANON__
20.0
CRAP 182.9
↑ Summary Build »
1 package Net::APNs::HTTP2;
2 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = "0.01";
7  
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Crypt::JWT;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Cache::Memory::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Protocol::HTTP2::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 has [qw/auth_key key_id team_id bundle_id is_development/] => (
19 is => 'rw',
20 );
21  
22 1 has apns_port => (
23 is => 'rw',
24 default => 443, # can use 2197
25 );
26  
27 has on_error => (
28 is => 'rw',
29 default => sub {
30 0 ▶ sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 },
32 1 );
33  
34 sub _host {
35 0 ▶ my $self = shift;
Subroutine _host: not called CC=2 SCAR=17.9
36 0 ▶ $self->is_development ? 'api.development.push.apple.com' : 'api.push.apple.com';
Branch: 1 branch
BranchTrueFalseTotal
$self->is_development ? : 0 0 0
37 }
38  
39 sub _client {
40 0 ▶ my $self = shift;
Subroutine _client: not called CC=2 SCAR=17.9
41 0 ▶ $self->{_client} ||= Protocol::HTTP2::Client->new(keepalive => 1);
Condition0%0/3
$self->{"_client"} ||= "Protocol::HTTP2::Client"->new("keepalive", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_client"} ||= "Protocol::HTTP2::Client"->new("keepalive", 1)
$self->{"_client"} "Protocol::HTTP2::Client"->new("keepalive", 1)
Truth table0%0/3
A ||= B
A= $self->{"_client"}B= "Protocol::HTTP2::Client"->new("keepalive", 1)
ABresult
1X1
011
000
42 }
43  
44 sub _handle {
45 0 ▶ my $self = shift;
Subroutine _handle: not called CC=2 SCAR=17.9
46  
47 0 ▶ unless ($self->_handle_connected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_handle_connected) 0 0 0
48 my $handle = AnyEvent::Handle->new(
49 keepalive => 1,
50 connect => [ $self->_host, $self->apns_port ],
51 tls => 'connect',
52 tls_ctx => {
53 verify => 1,
54 verify_peername => 'https',
55 },
56 autocork => 1,
57 on_error => sub {
58 0 ▶ my ($handle, $fatal, $message) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 $self->on_error->($fatal, $message);
60 0 $handle->destroy;
61 0 $self->{_condvar}->send;
62 },
63 on_eof => sub {
64 0 ▶ my $handle = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 $self->{_condvar}->send;
66 },
67 on_read => sub {
68 0 ▶ my $handle = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
69 0 $self->_client->feed(delete $handle->{rbuf});
70 0 ▶ while (my $frame = $self->_client->next_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frame = $self->_client->next_frame) 0 0 0
71 0 $handle->push_write($frame);
72 }
73 0 ▶ if ($self->_client->shutdown) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_client->shutdown) 0 0 0
74 0 $handle->push_shutdown;
75 0 return;
76 }
77  
78 0 ▶ unless ($self->_client->{active_streams} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_client->{"active_streams"} > 0) 0 0 0
79 0 $self->{_condvar}->send;
80 0 return;
81 }
82 },
83 0 );
84  
85 0 $self->{_handle} = $handle;
86 }
87  
88 0 return $self->{_handle};
89 }
90  
91 sub _handle_connected {
92 0 ▶ my $self = shift;
Subroutine _handle_connected: not called CC=3 SCAR=24.8
93  
94 0 my $handle = $self->{_handle};
95 0 ▶ return if !$handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle 0 0 0
96 0 ▶ return if $handle->destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $handle->destroyed 0 0 0
97 0 return 1;
98 }
99  
100 sub _provider_authentication_token {
101 0 ▶ my $self = shift;
Subroutine _provider_authentication_token: not called CC=2 SCAR=17.9
102  
103 0 ▶ $self->{_cache} ||= Cache::Memory::Simple->new;
Condition0%0/3
$self->{"_cache"} ||= "Cache::Memory::Simple"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_cache"} ||= "Cache::Memory::Simple"->new
$self->{"_cache"} "Cache::Memory::Simple"->new
Truth table0%0/3
A ||= B
A= $self->{"_cache"}B= "Cache::Memory::Simple"->new
ABresult
1X1
011
000
104 $self->{_cache}->get_or_set('provider_authentication_token', sub {
105 0 ▶ my $craims = {
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 iss => $self->team_id,
107 iat => time,
108 };
109 0 my $jwt = Crypt::JWT::encode_jwt(
110 payload => $craims,
111 key => [ $self->auth_key ],
112 alg => 'ES256',
113 extra_headers => { kid => $self->key_id },
114 );
115 0 return $jwt;
116 0 }, 60 * 50);
117 }
118  
119 sub prepare {
120 0 ▶ my ($self, $device_token, $payload, $cb, $extra_header) = @_;
Subroutine prepare: not called CC=6 SCAR=37.4
Pod documented
121 0 ▶ my $apns_expiration = $extra_header->{apns_expiration} || 0;
Condition0%0/2
$extra_header->{'apns_expiration'} || 0
l!l
00
MC/DC0%0/1
$extra_header->{'apns_expiration'} || 0
$extra_header->{'apns_expiration'}
Truth table0%0/2
A
A= $extra_header->{'apns_expiration'}
Aresult
11
00
122 0 ▶ my $apns_priority = $extra_header->{apns_priority} || 10;
Condition0%0/2
$extra_header->{'apns_priority'} || 10
l!l
00
MC/DC0%0/1
$extra_header->{'apns_priority'} || 10
$extra_header->{'apns_priority'}
Truth table0%0/2
A
A= $extra_header->{'apns_priority'}
Aresult
11
00
123 0 ▶ my $apns_topic = $extra_header->{apns_topic} || $self->bundle_id;
Condition0%0/3
$extra_header->{'apns_topic'} || $self->bundle_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$extra_header->{'apns_topic'} || $self->bundle_id
$extra_header->{'apns_topic'} $self->bundle_id
Truth table0%0/3
A || B
A= $extra_header->{'apns_topic'}B= $self->bundle_id
ABresult
1X1
011
000
124 0 my $apns_id = $extra_header->{apns_id};
125 0 my $apns_collapse_id = $extra_header->{apns_collapse_id};
126  
127 0 my $clinet = $self->_client;
128 0 ▶ $clinet->request(
Branch: 2 branches
BranchTrueFalseTotal
defined $apns_id ? : 0 0 0
defined $apns_collapse_id ? : 0 0 0
129 ':scheme' => 'https',
130 ':authority' => join(':', $self->_host, $self->apns_port),
131 ':path' => sprintf('/3/device/%s', $device_token),
132 ':method' => 'POST',
133 headers => [
134 'authorization' => sprintf('bearer %s', $self->_provider_authentication_token),
135 'apns-expiration' => $apns_expiration,
136 'apns-priority' => $apns_priority,
137 'apns-topic' => $apns_topic,
138 defined $apns_id ? ('apns-id' => $apns_id) : (),
139 defined $apns_collapse_id ? ('apns-collapse-id' => $apns_collapse_id) : (),
140 ],
141 data => JSON::encode_json($payload),
142 on_done => $cb,
143 );
144  
145 0 return $self;
146 }
147  
148 sub send {
149 0 ▶ my $self = shift;
Subroutine send: not called CC=2 SCAR=17.9
Pod documented
150  
151 0 local $self->{_condvar} = AnyEvent->condvar;
152  
153 0 my $handle = $self->_handle;
154 0 my $clinet = $self->_client;
155 0 ▶ while (my $frame = $clinet->next_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frame = $clinet->next_frame) 0 0 0
156 0 $handle->push_write($frame);
157 }
158  
159 0 $self->{_condvar}->recv;
160  
161 0 return 1;
162 }
163  
164 sub close {
165 0 ▶ my $self = shift;
Subroutine close: not called CC=5 SCAR=34.0
Pod documented
166 0 ▶ if ($self->{_client} && !$self->{_client}->shutdown) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_client"} and not $self->{"_client"}->shutdown) 0 0 0
Condition0%0/3
$self->{"_client"} and not $self->{"_client"}->shutdown
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_client"} and not $self->{"_client"}->shutdown
$self->{"_client"} not $self->{"_client"}->shutdown
Truth table0%0/3
A and not(B)
A= $self->{"_client"}B= not $self->{"_client"}->shutdown
ABresult
0X0
100
111
167 0 $self->{_client}->close;
168 }
169 0 ▶ if ($self->{_handle} && !$self->{_handle}->destroyed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_handle"} and not $self->{"_handle"}->destroyed) 0 0 0
Condition0%0/3
$self->{"_handle"} and not $self->{"_handle"}->destroyed
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_handle"} and not $self->{"_handle"}->destroyed
$self->{"_handle"} not $self->{"_handle"}->destroyed
Truth table0%0/3
A and not(B)
A= $self->{"_handle"}B= not $self->{"_handle"}->destroyed
ABresult
0X0
100
111
170 0 $self->{_handle}->destroy;
171 }
172 0 delete $self->{_cache};
173 0 delete $self->{_handle};
174 0 delete $self->{_client};
175  
176 0 return 1;
177 }
178  
179 1 1;
180 __END__
↑ Summary Build »