Completed
Push — master ( 7ad252...378d82 )
by Arthur
05:06
created

Request::addOptions()   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 1
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
    public function __construct(Notification $notification, $isProductionEnv)
55
    {
56
        $this->uri = $isProductionEnv ? $this->getProductionUrl($notification) : $this->getSandboxUrl($notification);
57
        $this->body = $notification->getPayload()->toJson();
58
59
        $this->prepareApnsHeaders($notification);
60
    }
61
62
    /**
63
     * Add request header.
64
     *
65
     * @param string $key
66
     * @param $value
67
     */
68
    public function addHeader(string $key, $value)
69
    {
70
        $this->headers[$key] = $value;
71
    }
72
73
    /**
74
     * Add request headers.
75
     *
76
     * @param Notification[] $headers
77
     */
78
    public function addHeaders(array $headers)
79
    {
80
        $this->headers = array_merge($this->headers, $headers);
81
    }
82
83
    /**
84
     * Get request headers.
85
     *
86
     * @return array
87
     */
88
    public function getHeaders(): array
89
    {
90
        return $this->headers;
91
    }
92
93
    /**
94
     * Get request uri.
95
     *
96
     * @return string
97
     */
98
    public function getUri(): string
99
    {
100
        return $this->uri;
101
    }
102
103
    /**
104
     * Get request body.
105
     *
106
     * @return string
107
     */
108
    public function getBody(): string
109
    {
110
        return $this->body;
111
    }
112
113
    private function getProductionUrl(Notification $notification)
114
    {
115
        return self::APNS_PRODUCTION_SERVER . $this->getUrlPath($notification);
116
    }
117
118
    private function getSandboxUrl(Notification $notification)
119
    {
120
        return self::APNS_DEVELOPMENT_SERVER . $this->getUrlPath($notification);
121
    }
122
123
    private function getUrlPath(Notification $notification)
124
    {
125
        return str_replace("{token}", $notification->getDeviceToken(), self::APNS_PATH_SCHEMA);
126
    }
127
128
    private function prepareApnsHeaders(Notification $notification)
129
    {
130
        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...
131
            $this->headers[self::HEADER_APNS_ID] =  $notification->getId();
132
        }
133
134
        if ($notification->getExpirationAt() instanceof \DateTime) {
135
            $this->headers[self::HEADER_APNS_EXPIRATION] = $notification->getExpirationAt()->getTimestamp();
136
        }
137
138
        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...
139
            $this->headers[self::HEADER_APNS_PRIORITY] =  $notification->getPriority();
140
        }
141
142
        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...
143
            $this->headers[self::HEADER_APNS_COLLAPSE_ID ] = $notification->getCollapseId();
144
        }
145
    }
146
}
147