Completed
Push — master ( 9b760e...8a0b19 )
by Arthur
01:59
created

Request::addOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
     * Curl options.
42
     *
43
     * @var array
44
     */
45
    private $options = [];
46
47
    public function __construct(Notification $notification, $isProductionEnv)
48
    {
49
        $url = $isProductionEnv ? $this->getProductionUrl($notification) : $this->getSandboxUrl($notification);
50
51
        if (!defined('CURL_HTTP_VERSION_2')) {
52
            define('CURL_HTTP_VERSION_2', 3);
53
        }
54
55
        $this->options = [
56
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2,
57
            CURLOPT_URL => $url,
58
            CURLOPT_PORT => self::APNS_PORT,
59
            CURLOPT_POST => true,
60
            CURLOPT_POSTFIELDS => $notification->getPayload()->toJson(),
61
            CURLOPT_RETURNTRANSFER => true,
62
            CURLOPT_TIMEOUT => 30,
63
            CURLOPT_HEADER => true,
64
        ];
65
66
        $this->prepareApnsHeaders($notification);
67
    }
68
69
    /**
70
     * Add curl options.
71
     *
72
     * @param int $key
73
     * @param $value
74
     * @return Request
75
     */
76
    public function addOption(int $key, $value): Request
77
    {
78
        $this->options[$key] = $value;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Add curl options.
85
     *
86
     * @param array $options
87
     * @return Request
88
     */
89
    public function addOptions(array $options): Request
90
    {
91
        $this->headers = array_merge($this->options, $options);
92
93
        return $this;
94
    }
95
96
    public function getOptions()
97
    {
98
        return $this->options;
99
    }
100
101
    /**
102
     * Add request header.
103
     *
104
     * @param string $header
105
     */
106
    public function addHeader(string $header)
107
    {
108
        $this->headers[] = $header;
109
    }
110
111
    /**
112
     * Add request headers.
113
     *
114
     * @param Notification[] $headers
115
     */
116
    public function addHeaders(array $headers)
117
    {
118
        $this->headers = array_merge($this->headers, $headers);
119
    }
120
121
    /**
122
     * Get request headers.
123
     *
124
     * @return array
125
     */
126
    public function getHeaders(): array
127
    {
128
        return $this->headers;
129
    }
130
131
    private function getProductionUrl(Notification $notification)
132
    {
133
        return self::APNS_PRODUCTION_SERVER . $this->getUrlPath($notification);
134
    }
135
136
    private function getSandboxUrl(Notification $notification)
137
    {
138
        return self::APNS_DEVELOPMENT_SERVER . $this->getUrlPath($notification);
139
    }
140
141
    private function getUrlPath(Notification $notification)
142
    {
143
        return str_replace("{token}", $notification->getDeviceToken(), self::APNS_PATH_SCHEMA);
144
    }
145
146
    private function prepareApnsHeaders(Notification $notification)
147
    {
148
        if ($notification->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $notification->getId() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
149
            $this->headers[] = self::HEADER_APNS_ID . ': ' . $notification->getId();
150
        }
151
152
        if ($notification->getExpirationAt()) {
153
            $this->headers[] = self::HEADER_APNS_EXPIRATION . ': ' . $notification->getExpirationAt();
154
        }
155
156
        if ($notification->getPriority()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $notification->getPriority() of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
157
            $this->headers[] = self::HEADER_APNS_PRIORITY . ': ' . $notification->getPriority();
158
        }
159
160
        if ($notification->getCollapseId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $notification->getCollapseId() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
161
            $this->headers[] = self::HEADER_APNS_COLLAPSE_ID . ': ' . $notification->getCollapseId();
162
        }
163
    }
164
165
}
166