Completed
Push — master ( 8d719e...0f62ec )
by Arthur
08:07
created

Request::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Pushok package.
5
 *
6
 * (c) Arthur Edamov <[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 Pushok;
13
14
/**
15
 * Class Request
16
 * @package Pushok
17
 *
18
 * @see http://bit.ly/communicating-with-apns
19
 */
20
class Request
21
{
22
    const APNS_DEVELOPMENT_SERVER = 'https://api.development.push.apple.com';
23
    const APNS_PRODUCTION_SERVER = 'https://api.push.apple.com';
24
    const APNS_PORT = 443;
25
    const APNS_PATH_SCHEMA = '/3/device/{token}';
26
27
    const HEADER_APNS_ID = 'apns-id';
28
    const HEADER_APNS_EXPIRATION = 'apns-expiration';
29
    const HEADER_APNS_PRIORITY = 'apns-priority';
30
    const HEADER_APNS_TOPIC = 'apns-topic';
31
    const HEADER_APNS_COLLAPSE_ID = 'apns-collapse-id';
32
33
    /**
34
     * Request headers.
35
     *
36
     * @var array
37
     */
38
    private $headers = [];
39
40
    /**
41
     * Request uri.
42
     *
43
     * @var string
44
     */
45
    private $uri;
46
47
    /**
48
     * Request body.
49
     *
50
     * @var string
51
     */
52
    private $body;
53
54
    /**
55
     * Curl options.
56
     *
57
     * @var array
58
     */
59
    private $options;
60
61
    public function __construct(Notification $notification, $isProductionEnv)
62
    {
63
        $this->uri = $isProductionEnv ? $this->getProductionUrl($notification) : $this->getSandboxUrl($notification);
64
        $this->body = $notification->getPayload()->toJson();
65
66
        $this->options = [
67
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2,
68
            CURLOPT_URL => $this->uri,
69
            CURLOPT_PORT => self::APNS_PORT,
70
            CURLOPT_POST => true,
71
            CURLOPT_POSTFIELDS => $notification->getPayload()->toJson(),
72
            CURLOPT_RETURNTRANSFER => true,
73
            CURLOPT_TIMEOUT => 10,
74
            CURLOPT_HEADER => true,
75
        ];
76
77
        $this->prepareApnsHeaders($notification);
78
    }
79
80
    /**
81
     * Add request header.
82
     *
83
     * @param string $key
84
     * @param $value
85
     */
86
    public function addHeader(string $key, $value)
87
    {
88
        $this->headers[$key] = $value;
89
    }
90
91
    /**
92
     * Add request headers.
93
     *
94
     * @param array $headers
95
     */
96
    public function addHeaders(array $headers)
97
    {
98
        $this->headers = array_merge($this->headers, $headers);
99
    }
100
101
    /**
102
     * Get request headers.
103
     *
104
     * @return array
105
     */
106
    public function getHeaders(): array
107
    {
108
        return $this->headers;
109
    }
110
111
    /**
112
     * Get decorated request headers.
113
     *
114
     * @return array
115
     */
116
    public function getDecoratedHeaders(): array
117
    {
118
        $decoratedHeaders = [];
119
        foreach ($this->headers as $name => $value) {
120
            $decoratedHeaders[] = $name . ': ' . $value;
121
        }
122
        return $decoratedHeaders;
123
    }
124
125
    /**
126
     * Get request uri.
127
     *
128
     * @return string
129
     */
130
    public function getUri(): string
131
    {
132
        return $this->uri;
133
    }
134
135
    /**
136
     * Get request body.
137
     *
138
     * @return string
139
     */
140
    public function getBody(): string
141
    {
142
        return $this->body;
143
    }
144
145
    /**
146
     * Get request options.
147
     *
148
     * @return array
149
     */
150
    public function getOptions(): array
151
    {
152
        return $this->options;
153
    }
154
155
    /**
156
     * Get Url for APNs production server.
157
     *
158
     * @param Notification $notification
159
     * @return string
160
     */
161
    private function getProductionUrl(Notification $notification)
162
    {
163
        return self::APNS_PRODUCTION_SERVER . $this->getUrlPath($notification);
164
    }
165
166
    /**
167
     * Get Url for APNs sandbox server.
168
     *
169
     * @param Notification $notification
170
     * @return string
171
     */
172
    private function getSandboxUrl(Notification $notification)
173
    {
174
        return self::APNS_DEVELOPMENT_SERVER . $this->getUrlPath($notification);
175
    }
176
177
    /**
178
     * Get Url path.
179
     *
180
     * @param Notification $notification
181
     * @return mixed
182
     */
183
    private function getUrlPath(Notification $notification)
184
    {
185
        return str_replace("{token}", $notification->getDeviceToken(), self::APNS_PATH_SCHEMA);
186
    }
187
188
    /**
189
     * Prepare APNs headers before sending request.
190
     *
191
     * @param Notification $notification
192
     */
193
    private function prepareApnsHeaders(Notification $notification)
194
    {
195
        if (!empty($notification->getId())) {
196
            $this->headers[self::HEADER_APNS_ID] =  $notification->getId();
197
        }
198
199
        if ($notification->getExpirationAt() instanceof \DateTime) {
200
            $this->headers[self::HEADER_APNS_EXPIRATION] = $notification->getExpirationAt()->getTimestamp();
201
        }
202
203
        if (is_int($notification->getPriority())) {
204
            $this->headers[self::HEADER_APNS_PRIORITY] =  $notification->getPriority();
205
        }
206
207
        if (!empty($notification->getCollapseId())) {
208
            $this->headers[self::HEADER_APNS_COLLAPSE_ID ] = $notification->getCollapseId();
209
        }
210
    }
211
}
212