1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\Message; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Exception\DomainException; |
13
|
|
|
use Zbox\UnifiedPush\Exception\InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class RecipientDevice |
17
|
|
|
* @package Zbox\UnifiedPush\Message |
18
|
|
|
*/ |
19
|
|
|
class RecipientDevice |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Device identifier statuses |
23
|
|
|
*/ |
24
|
|
|
const DEVICE_ACTUAL = 1; |
25
|
|
|
const DEVICE_NOT_READY = 2; |
26
|
|
|
const DEVICE_NOT_REGISTERED = 3; |
27
|
|
|
const IDENTIFIER_NEED_TO_BE_REPLACED = 4; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Recipient device identifier |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $identifier; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Current status |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $identifierStatus; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Sender should replace the ID on future requests |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $idToReplaceTo; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $deviceIdentifier |
51
|
|
|
* @param MessageInterface $message |
52
|
|
|
*/ |
53
|
|
|
public function __construct($deviceIdentifier, MessageInterface $message) |
54
|
|
|
{ |
55
|
|
|
$this->setIdentifier($deviceIdentifier, $message); |
56
|
|
|
$this->setIdentifierStatus(self::DEVICE_ACTUAL); |
57
|
|
|
|
58
|
|
|
return $this; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getIdentifier() |
65
|
|
|
{ |
66
|
|
|
return $this->identifier; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $identifier |
71
|
|
|
* @param MessageInterface $message |
72
|
|
|
* @return $this |
73
|
|
|
* @throws InvalidArgumentException |
74
|
|
|
*/ |
75
|
|
|
public function setIdentifier($identifier, MessageInterface $message) |
76
|
|
|
{ |
77
|
|
|
if (!$message->validateRecipient($identifier)) { |
78
|
|
|
throw new InvalidArgumentException("Device identifier is not valid"); |
79
|
|
|
} |
80
|
|
|
$this->identifier = $identifier; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
public function getIdentifierStatus() |
89
|
|
|
{ |
90
|
|
|
return $this->identifierStatus; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param int $identifierStatus |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setIdentifierStatus($identifierStatus) |
98
|
|
|
{ |
99
|
|
|
if (!in_array($identifierStatus, array( |
100
|
|
|
self::DEVICE_ACTUAL, |
101
|
|
|
self::DEVICE_NOT_READY, |
102
|
|
|
self::DEVICE_NOT_REGISTERED, |
103
|
|
|
self::IDENTIFIER_NEED_TO_BE_REPLACED |
104
|
|
|
))) { |
105
|
|
|
throw new DomainException("Unknown status of device identifier"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->identifierStatus = $identifierStatus; |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getIdToReplaceTo() |
116
|
|
|
{ |
117
|
|
|
return $this->idToReplaceTo; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $idToReplaceTo |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function setIdToReplaceTo($idToReplaceTo) |
125
|
|
|
{ |
126
|
|
|
$this->setIdentifierStatus(self::IDENTIFIER_NEED_TO_BE_REPLACED); |
127
|
|
|
$this->idToReplaceTo = $idToReplaceTo; |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function isInvalidRecipient() |
135
|
|
|
{ |
136
|
|
|
return in_array( |
137
|
|
|
$this->identifierStatus, |
138
|
|
|
array( |
139
|
|
|
static::DEVICE_NOT_REGISTERED, |
140
|
|
|
static::IDENTIFIER_NEED_TO_BE_REPLACED |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function __toString() |
149
|
|
|
{ |
150
|
|
|
return $this->getIdentifier(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|