HttpProtocol::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 6
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the AppleApnPush package
7
 *
8
 * (c) Vitaliy Zhuk <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code
12
 */
13
14
namespace Apple\ApnPush\Protocol;
15
16
use Apple\ApnPush\Encoder\PayloadEncoderInterface;
17
use Apple\ApnPush\Exception\SendNotification\SendNotificationException;
18
use Apple\ApnPush\Model\Notification;
19
use Apple\ApnPush\Model\Receiver;
20
use Apple\ApnPush\Protocol\Http\Authenticator\AuthenticatorInterface;
21
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactoryInterface;
22
use Apple\ApnPush\Protocol\Http\Request;
23
use Apple\ApnPush\Protocol\Http\Sender\Exception\HttpSenderException;
24
use Apple\ApnPush\Protocol\Http\Sender\HttpSenderInterface;
25
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactoryInterface;
26
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolVisitorInterface;
27
28
/**
29
 * Implement HTTP protocol for send push notification
30
 */
31
class HttpProtocol implements ProtocolInterface
32
{
33
    /**
34
     * @var AuthenticatorInterface
35
     */
36
    private $authenticator;
37
38
    /**
39
     * @var HttpSenderInterface
40
     */
41
    private $httpSender;
42
43
    /**
44
     * @var PayloadEncoderInterface
45
     */
46
    private $payloadEncoder;
47
48
    /**
49
     * @var UriFactoryInterface
50
     */
51
    private $uriFactory;
52
53
    /**
54
     * @var HttpProtocolVisitorInterface
55
     */
56
    private $visitor;
57
58
    /**
59
     * @var ExceptionFactoryInterface
60
     */
61
    private $exceptionFactory;
62
63
    /**
64
     * Constructor.
65
     *
66
     * @param AuthenticatorInterface       $authenticator
67
     * @param HttpSenderInterface          $httpSender
68
     * @param PayloadEncoderInterface      $payloadEncoder
69
     * @param UriFactoryInterface          $uriFactory
70
     * @param HttpProtocolVisitorInterface $visitor
71
     * @param ExceptionFactoryInterface    $exceptionFactory
72
     */
73
    public function __construct(AuthenticatorInterface $authenticator, HttpSenderInterface $httpSender, PayloadEncoderInterface $payloadEncoder, UriFactoryInterface $uriFactory, HttpProtocolVisitorInterface $visitor, ExceptionFactoryInterface $exceptionFactory)
74
    {
75
        $this->authenticator = $authenticator;
76
        $this->httpSender = $httpSender;
77
        $this->payloadEncoder = $payloadEncoder;
78
        $this->uriFactory = $uriFactory;
79
        $this->visitor = $visitor;
80
        $this->exceptionFactory = $exceptionFactory;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     *
86
     * @throws HttpSenderException
87
     */
88
    public function send(Receiver $receiver, Notification $notification, bool $sandbox): void
89
    {
90
        try {
91
            $this->doSend($receiver, $notification, $sandbox);
92
        } catch (HttpSenderException $e) {
93
            $this->httpSender->close();
94
95
            throw $e;
96
        }
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function closeConnection(): void
103
    {
104
        $this->httpSender->close();
105
    }
106
107
    /**
108
     * Inner send process
109
     *
110
     * @param Receiver     $receiver
111
     * @param Notification $notification
112
     * @param bool         $sandbox
113
     *
114
     * @throws SendNotificationException
115
     * @throws HttpSenderException
116
     */
117
    private function doSend(Receiver $receiver, Notification $notification, bool $sandbox): void
118
    {
119
        $payloadEncoded = $this->payloadEncoder->encode($notification->getPayload());
120
        $uri = $this->uriFactory->create($receiver->getToken(), $sandbox);
121
122
        $request = new Request($uri, $payloadEncoded);
123
124
        $headers = [
125
            'content-type' => 'application/json',
126
            'accept'       => 'application/json',
127
            'apns-topic'   => $receiver->getTopic(),
128
        ];
129
130
        $request = $request->withHeaders($headers);
131
        $request = $this->authenticator->authenticate($request);
132
133
        $request = $this->visitor->visit($notification, $request);
134
135
        $response = $this->httpSender->send($request);
136
137
        if ($response->getStatusCode() !== 200) {
138
            throw $this->exceptionFactory->create($response);
139
        }
140
    }
141
}
142