Http20Builder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Sender\Builder;
15
16
use Apple\ApnPush\Encoder\PayloadEncoder;
17
use Apple\ApnPush\Encoder\PayloadEncoderInterface;
18
use Apple\ApnPush\Protocol\Http\Authenticator\AuthenticatorInterface;
19
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactory;
20
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactoryInterface;
21
use Apple\ApnPush\Protocol\Http\Sender\CurlHttpSender;
22
use Apple\ApnPush\Protocol\Http\Sender\HttpSenderInterface;
23
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactory;
24
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactoryInterface;
25
use Apple\ApnPush\Protocol\Http\Visitor\AddApnIdHeaderVisitor;
26
use Apple\ApnPush\Protocol\Http\Visitor\AddCollapseIdHeaderVisitor;
27
use Apple\ApnPush\Protocol\Http\Visitor\AddExpirationHeaderVisitor;
28
use Apple\ApnPush\Protocol\Http\Visitor\AddPriorityHeaderVisitor;
29
use Apple\ApnPush\Protocol\Http\Visitor\AddPushTypeHeaderVisitor;
30
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolChainVisitor;
31
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolVisitorInterface;
32
use Apple\ApnPush\Protocol\HttpProtocol;
33
use Apple\ApnPush\Protocol\ProtocolInterface;
34
use Apple\ApnPush\Sender\Sender;
35
use Apple\ApnPush\Sender\SenderInterface;
36
37
/**
38
 * Builder for create sender with HTTP/2 protocol
39
 */
40
class Http20Builder implements BuilderInterface
41
{
42
    /**
43
     * @var \SplPriorityQueue|HttpProtocolVisitorInterface[]
44
     */
45
    private $visitors;
46
47
    /**
48
     * @var UriFactoryInterface
49
     */
50
    private $uriFactory;
51
52
    /**
53
     * @var PayloadEncoderInterface
54
     */
55
    private $payloadEncoder;
56
57
    /**
58
     * @var AuthenticatorInterface
59
     */
60
    private $authenticator;
61
62
    /**
63
     * @var HttpSenderInterface
64
     */
65
    private $httpSender;
66
67
    /**
68
     * @var ExceptionFactoryInterface
69
     */
70
    private $exceptionFactory;
71
72
    /**
73
     * @var bool
74
     */
75
    private $addedDefaultVisitors;
76
77
    /**
78
     * Constructor.
79
     *
80
     * @param AuthenticatorInterface $authenticator
81
     */
82
    public function __construct(AuthenticatorInterface $authenticator)
83
    {
84
        $this->authenticator = $authenticator;
85
        $this->visitors = new \SplPriorityQueue();
86
        $this->uriFactory = new UriFactory();
87
        $this->payloadEncoder = new PayloadEncoder();
88
        $this->httpSender = new CurlHttpSender();
89
        $this->exceptionFactory = new ExceptionFactory();
90
91
        $this->addDefaultVisitors();
92
    }
93
94
    /**
95
     * Set authenticator
96
     *
97
     * @param AuthenticatorInterface $authenticator
98
     *
99
     * @return Http20Builder
100
     */
101
    public function setAuthenticator(AuthenticatorInterface $authenticator): Http20Builder
102
    {
103
        $this->authenticator = $authenticator;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Add visitor
110
     *
111
     * @param HttpProtocolVisitorInterface $visitor
112
     * @param int                          $priority
113
     *
114
     * @return Http20Builder
115
     */
116
    public function addVisitor(HttpProtocolVisitorInterface $visitor, int $priority = 0): Http20Builder
117
    {
118
        $this->visitors->insert($visitor, $priority);
119
120
        return $this;
121
    }
122
123
    /**
124
     * Add default visitors
125
     *
126
     * @return Http20Builder
127
     *
128
     * @deprecated This method is deprecated and will be a move to private scope.
129
     *             Please do not use this method in your code.
130
     *             This method will be called from the constructor of this builder.
131
     */
132
    public function addDefaultVisitors(): Http20Builder
133
    {
134
        if ($this->addedDefaultVisitors) {
135
            return $this;
136
        }
137
138
        $this->addedDefaultVisitors = true;
139
140
        $this->addVisitor(new AddExpirationHeaderVisitor());
141
        $this->addVisitor(new AddPriorityHeaderVisitor());
142
        $this->addVisitor(new AddApnIdHeaderVisitor());
143
        $this->addVisitor(new AddCollapseIdHeaderVisitor());
144
        $this->addVisitor(new AddPushTypeHeaderVisitor());
145
146
        return $this;
147
    }
148
149
    /**
150
     * Set URI factory
151
     *
152
     * @param UriFactoryInterface $uriFactory
153
     *
154
     * @return Http20Builder
155
     */
156
    public function setUriFactory(UriFactoryInterface $uriFactory): Http20Builder
157
    {
158
        $this->uriFactory = $uriFactory;
159
160
        return $this;
161
    }
162
163
    /**
164
     * Set notification encoder
165
     *
166
     * @param PayloadEncoderInterface $payloadEncoder
167
     *
168
     * @return Http20Builder
169
     */
170
    public function setPayloadEncoder(PayloadEncoderInterface $payloadEncoder): Http20Builder
171
    {
172
        $this->payloadEncoder = $payloadEncoder;
173
174
        return $this;
175
    }
176
177
    /**
178
     * Set http sender
179
     *
180
     * @param HttpSenderInterface $httpSender
181
     *
182
     * @return Http20Builder
183
     */
184
    public function setHttpSender(HttpSenderInterface $httpSender): Http20Builder
185
    {
186
        $this->httpSender = $httpSender;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Set exception factory
193
     *
194
     * @param ExceptionFactoryInterface $exceptionFactory
195
     *
196
     * @return Http20Builder
197
     */
198
    public function setExceptionFactory(ExceptionFactoryInterface $exceptionFactory): Http20Builder
199
    {
200
        $this->exceptionFactory = $exceptionFactory;
201
202
        return $this;
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function buildProtocol(): ProtocolInterface
209
    {
210
        $chainVisitor = $this->createChainVisitor();
211
212
        return new HttpProtocol(
213
            $this->authenticator,
214
            $this->httpSender,
215
            $this->payloadEncoder,
216
            $this->uriFactory,
217
            $chainVisitor,
218
            $this->exceptionFactory
219
        );
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function build(): SenderInterface
226
    {
227
        $protocol = $this->buildProtocol();
228
229
        return new Sender($protocol);
230
    }
231
232
    /**
233
     * Create chain visitor
234
     *
235
     * @return HttpProtocolChainVisitor
236
     */
237
    private function createChainVisitor(): HttpProtocolChainVisitor
238
    {
239
        $chainVisitors = new HttpProtocolChainVisitor();
240
        $visitors = clone $this->visitors;
241
        $priority = 0;
242
243
        foreach ($visitors as $visitor) {
244
            $chainVisitors->add($visitor, ++$priority);
245
        }
246
247
        return $chainVisitors;
248
    }
249
}
250