Completed
Push — master ( 58620c...f23d9f )
by Vitaliy
02:20
created

Http20Builder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the AppleApnPush package
5
 *
6
 * (c) Vitaliy Zhuk <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code
10
 */
11
12
namespace Apple\ApnPush\Sender\Builder;
13
14
use Apple\ApnPush\Encoder\MessageEncoder;
15
use Apple\ApnPush\Encoder\MessageEncoderInterface;
16
use Apple\ApnPush\Protocol\Http\Authenticator\AuthenticatorInterface;
17
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactory;
18
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactoryInterface;
19
use Apple\ApnPush\Protocol\Http\Sender\CurlHttpSender;
20
use Apple\ApnPush\Protocol\Http\Sender\HttpSenderInterface;
21
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactory;
22
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactoryInterface;
23
use Apple\ApnPush\Protocol\Http\Visitor\AddApnIdHeaderVisitor;
24
use Apple\ApnPush\Protocol\Http\Visitor\AddExpirationHeaderVisitor;
25
use Apple\ApnPush\Protocol\Http\Visitor\AddPriorityHeaderVisitor;
26
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolChainVisitor;
27
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolVisitorInterface;
28
use Apple\ApnPush\Protocol\HttpProtocol;
29
use Apple\ApnPush\Sender\Sender;
30
use Apple\ApnPush\Sender\SenderInterface;
31
32
/**
33
 * Builder for create sender with HTTP 2.0 protocol
34
 */
35
class Http20Builder implements BuilderInterface
36
{
37
    /**
38
     * @var \SplPriorityQueue|HttpProtocolVisitorInterface[]
39
     */
40
    private $visitors;
41
42
    /**
43
     * @var UriFactoryInterface
44
     */
45
    private $uriFactory;
46
47
    /**
48
     * @var MessageEncoderInterface
49
     */
50
    private $messageEncoder;
51
52
    /**
53
     * @var AuthenticatorInterface
54
     */
55
    private $authenticator;
56
57
    /**
58
     * @var HttpSenderInterface
59
     */
60
    private $httpSender;
61
62
    /**
63
     * @var ExceptionFactoryInterface
64
     */
65
    private $exceptionFactory;
66
67
    /**
68
     * Constructor.
69
     *
70
     * @param AuthenticatorInterface $authenticator
71
     */
72
    public function __construct(AuthenticatorInterface $authenticator)
73
    {
74
        $this->authenticator = $authenticator;
75
        $this->visitors = new \SplPriorityQueue();
76
        $this->uriFactory = new UriFactory();
77
        $this->messageEncoder = new MessageEncoder();
78
        $this->httpSender = new CurlHttpSender();
79
        $this->exceptionFactory = new ExceptionFactory();
80
    }
81
82
    /**
83
     * Set authenticator
84
     *
85
     * @param AuthenticatorInterface $authenticator
86
     *
87
     * @return Http20Builder
88
     */
89
    public function setAuthenticator(AuthenticatorInterface $authenticator) : Http20Builder
90
    {
91
        $this->authenticator = $authenticator;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Add visitor
98
     *
99
     * @param HttpProtocolVisitorInterface $visitor
100
     * @param int                          $priority
101
     *
102
     * @return Http20Builder
103
     */
104
    public function addVisitor(HttpProtocolVisitorInterface $visitor, int $priority = 0) : Http20Builder
105
    {
106
        $this->visitors->insert($visitor, $priority);
107
108
        return $this;
109
    }
110
111
    /**
112
     * Add default visitors
113
     *
114
     * @return Http20Builder
115
     */
116
    public function addDefaultVisitors() : Http20Builder
117
    {
118
        $this->addVisitor(new AddExpirationHeaderVisitor());
119
        $this->addVisitor(new AddPriorityHeaderVisitor());
120
        $this->addVisitor(new AddApnIdHeaderVisitor());
121
122
        return $this;
123
    }
124
125
    /**
126
     * Set URI factory
127
     *
128
     * @param UriFactoryInterface $uriFactory
129
     *
130
     * @return Http20Builder
131
     */
132
    public function setUriFactory(UriFactoryInterface $uriFactory) : Http20Builder
133
    {
134
        $this->uriFactory = $uriFactory;
135
136
        return $this;
137
    }
138
139
    /**
140
     * Set message encoder
141
     *
142
     * @param MessageEncoderInterface $messageEncoder
143
     *
144
     * @return Http20Builder
145
     */
146
    public function setMessageEncoder(MessageEncoderInterface $messageEncoder) : Http20Builder
147
    {
148
        $this->messageEncoder = $messageEncoder;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Set http sender
155
     *
156
     * @param HttpSenderInterface $httpSender
157
     *
158
     * @return Http20Builder
159
     */
160
    public function setHttpSender(HttpSenderInterface $httpSender) : Http20Builder
161
    {
162
        $this->httpSender = $httpSender;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Set exception factory
169
     *
170
     * @param ExceptionFactoryInterface $exceptionFactory
171
     *
172
     * @return Http20Builder
173
     */
174
    public function setExceptionFactory(ExceptionFactoryInterface $exceptionFactory) : Http20Builder
175
    {
176
        $this->exceptionFactory = $exceptionFactory;
177
178
        return $this;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function build() : SenderInterface
185
    {
186
        $chainVisitor = $this->createChainVisitor();
187
188
        $protocol = new HttpProtocol(
189
            $this->authenticator,
190
            $this->httpSender,
191
            $this->messageEncoder,
192
            $this->uriFactory,
193
            $chainVisitor,
194
            $this->exceptionFactory
195
        );
196
197
        return new Sender($protocol);
198
    }
199
200
    /**
201
     * Create chain visitor
202
     *
203
     * @return HttpProtocolChainVisitor
204
     */
205
    private function createChainVisitor() : HttpProtocolChainVisitor
206
    {
207
        $chainVisitors = new HttpProtocolChainVisitor();
208
        $visitors = clone $this->visitors;
209
        $priority = 0;
210
211
        foreach ($visitors as $visitor) {
212
            $chainVisitors->add($visitor, $priority++);
213
        }
214
215
        return $chainVisitors;
216
    }
217
}
218