|
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\Notification; |
|
11
|
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\Message\MessageInterface; |
|
13
|
|
|
use Zbox\UnifiedPush\Exception\InvalidArgumentException; |
|
14
|
|
|
use Zbox\UnifiedPush\Exception\MalformedNotificationException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class PayloadHandler |
|
18
|
|
|
* @package Zbox\UnifiedPush\Notification |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class PayloadHandler implements PayloadHandlerInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var MessageInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $message; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $notificationId; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param MessageInterface $message |
|
34
|
|
|
* @return bool |
|
35
|
|
|
*/ |
|
36
|
|
|
abstract public function isSupported(MessageInterface $message); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param MessageInterface $message |
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setMessage(MessageInterface $message) |
|
43
|
|
|
{ |
|
44
|
|
|
if (!$this->isSupported($message)) { |
|
45
|
|
|
throw new InvalidArgumentException('Message type is not supported'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->message = $message; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param int $id |
|
55
|
|
|
* @return $this |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setNotificationId($id) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->notificationId = $id; |
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Gets maximum size allowed for notification payload |
|
65
|
|
|
* |
|
66
|
|
|
* @return int |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getPayloadMaxLength() |
|
69
|
|
|
{ |
|
70
|
|
|
return static::PAYLOAD_MAX_LENGTH; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getCustomNotificationData() |
|
77
|
|
|
{ |
|
78
|
|
|
return array(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Check if maximum size allowed for a notification payload exceeded |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $packedPayload |
|
85
|
|
|
* @throws MalformedNotificationException |
|
86
|
|
|
*/ |
|
87
|
|
|
public function validatePayload($packedPayload) |
|
88
|
|
|
{ |
|
89
|
|
|
$messageType = $this->message->getMessageType(); |
|
90
|
|
|
$maxLength = $this->getPayloadMaxLength(); |
|
91
|
|
|
|
|
92
|
|
|
if (strlen($packedPayload) > $maxLength) { |
|
93
|
|
|
throw new MalformedNotificationException( |
|
94
|
|
|
sprintf( |
|
95
|
|
|
"The maximum size allowed for '%s' notification payload is %d bytes", |
|
96
|
|
|
$messageType, |
|
97
|
|
|
$maxLength |
|
98
|
|
|
) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|