|
1
|
|
|
<?php |
|
2
|
|
|
namespace nstdio\notymo; |
|
3
|
|
|
use nstdio\notymo\exception\PushNotificationException; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class AbstractNotification |
|
7
|
|
|
* |
|
8
|
|
|
* @package nstdio\notymo |
|
9
|
|
|
* @author Edgar Asatryan <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AbstractNotification implements PushNotificationInterface, LifeCycleCallback |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var MessageQueue |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $messageQueue; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Connection |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $stream; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var CallbackInvoker |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $invoker; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $retryCount = 1; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* AbstractNotification constructor. |
|
35
|
|
|
*/ |
|
36
|
20 |
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
20 |
|
$this->messageQueue = new MessageQueue(); |
|
39
|
20 |
|
$this->invoker = new CallbackInvoker(); |
|
40
|
20 |
|
} |
|
41
|
|
|
|
|
42
|
8 |
|
final public function send() |
|
43
|
|
|
{ |
|
44
|
8 |
|
if ($this->messageQueue->isEmpty()) { |
|
45
|
2 |
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
6 |
|
$this->openConnection(); |
|
49
|
|
|
|
|
50
|
6 |
|
$attempt = 0; |
|
51
|
6 |
|
while ($attempt < $this->retryCount && !$this->messageQueue->isEmpty()) { |
|
52
|
|
|
/** @var MessageInterface $message */ |
|
53
|
6 |
|
foreach ($this->messageQueue as $message) { |
|
54
|
6 |
|
$this->sendImpl($message); |
|
55
|
6 |
|
} |
|
56
|
6 |
|
$attempt++; |
|
57
|
6 |
|
} |
|
58
|
|
|
|
|
59
|
6 |
|
$this->notifyOnComplete($this->messageQueue); |
|
60
|
6 |
|
if ($this->messageQueue->isEmpty()) { |
|
61
|
2 |
|
$this->detach(); |
|
62
|
2 |
|
} |
|
63
|
6 |
|
$this->stream->close(); |
|
64
|
6 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
abstract protected function sendImpl(MessageInterface $message); |
|
67
|
|
|
|
|
68
|
6 |
|
public function enqueue(MessageInterface $message) |
|
69
|
|
|
{ |
|
70
|
6 |
|
$this->messageQueue->enqueue($message); |
|
71
|
6 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Connection $wrapper |
|
75
|
|
|
*/ |
|
76
|
9 |
|
public function setStreamWrapper(Connection $wrapper) |
|
77
|
|
|
{ |
|
78
|
9 |
|
$this->stream = $wrapper; |
|
79
|
9 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param int $count |
|
83
|
|
|
*/ |
|
84
|
10 |
|
public function setRetryCount($count) |
|
85
|
|
|
{ |
|
86
|
10 |
|
$this->retryCount = intval($count); |
|
87
|
10 |
|
if ($this->retryCount <= 0) { |
|
88
|
7 |
|
throw new \InvalidArgumentException("retryCount must be grader then zero."); |
|
89
|
|
|
} |
|
90
|
3 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
abstract protected function createPayload(MessageInterface $message); |
|
93
|
|
|
|
|
94
|
6 |
|
final protected function openConnection() |
|
95
|
|
|
{ |
|
96
|
6 |
|
$this->lazyInitStream(); |
|
97
|
|
|
|
|
98
|
6 |
|
$this->stream->open($this->getConnectionParams(), null); |
|
99
|
|
|
|
|
100
|
6 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
6 |
|
protected function notifyOnComplete(MessageQueue $messages) |
|
104
|
|
|
{ |
|
105
|
6 |
|
$this->invoker->callOnComplete($messages); |
|
106
|
6 |
|
} |
|
107
|
|
|
|
|
108
|
5 |
|
protected function notifyOnEachSent(MessageInterface $message, $feedBack) |
|
109
|
|
|
{ |
|
110
|
5 |
|
$this->invoker->callOnEachSent($message, $feedBack); |
|
111
|
5 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function notifyOnError(MessageInterface $message, PushNotificationException $exc) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->invoker->callOnError($message, $exc); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
public function onComplete(\Closure $listener) |
|
119
|
|
|
{ |
|
120
|
2 |
|
$this->invoker->onComplete($listener); |
|
121
|
2 |
|
} |
|
122
|
|
|
|
|
123
|
4 |
|
public function onEachSent(\Closure $callback) |
|
124
|
|
|
{ |
|
125
|
4 |
|
$this->invoker->onEachSent($callback); |
|
126
|
4 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function onError(\Closure $callback) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->invoker->onError($callback); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
public function detach() |
|
134
|
|
|
{ |
|
135
|
2 |
|
$this->invoker->detach(); |
|
136
|
2 |
|
} |
|
137
|
|
|
|
|
138
|
6 |
|
private function lazyInitStream() |
|
139
|
|
|
{ |
|
140
|
6 |
|
if ($this->stream === null) { |
|
141
|
1 |
|
$this->stream = new CurlWrapper(); |
|
142
|
1 |
|
} |
|
143
|
6 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
|
|
abstract protected function getConnectionParams(); |
|
149
|
|
|
} |