1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Push notification services abstraction (http://github.com/juliangut/tify) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/tify for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://github.com/juliangut/tify/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Tify; |
11
|
|
|
|
12
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
13
|
|
|
use Jgut\Tify\Adapter\AbstractAdapter; |
14
|
|
|
use Jgut\Tify\Adapter\FeedbackAdapter; |
15
|
|
|
use Jgut\Tify\Adapter\SendAdapter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Notifications service. |
19
|
|
|
*/ |
20
|
|
|
class Service |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \Jgut\Tify\Adapter\AbstractAdapter |
24
|
|
|
*/ |
25
|
|
|
protected $adapters; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Jgut\Tify\Notification[] |
29
|
|
|
*/ |
30
|
|
|
protected $notifications; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Manager constructor. |
34
|
|
|
* |
35
|
|
|
* @param \Jgut\Tify\Adapter\AbstractAdapter[] $adapters |
36
|
|
|
* @param \Jgut\Tify\Notification[] $notifications |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $adapters = [], array $notifications = []) |
39
|
|
|
{ |
40
|
|
|
$this->adapters = new ArrayCollection; |
|
|
|
|
41
|
|
|
foreach ($adapters as $adapter) { |
42
|
|
|
$this->addAdapter($adapter); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->notifications = new ArrayCollection; |
|
|
|
|
46
|
|
|
foreach ($notifications as $notification) { |
47
|
|
|
$this->addNotification($notification); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Retrieve registered adapters. |
53
|
|
|
* |
54
|
|
|
* @return \Jgut\Tify\Adapter\AbstractAdapter[] |
55
|
|
|
*/ |
56
|
|
|
public function getAdapters() |
57
|
|
|
{ |
58
|
|
|
return $this->adapters->toArray(); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Register adapter. |
63
|
|
|
* |
64
|
|
|
* @param \Jgut\Tify\Adapter\AbstractAdapter $adapter |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function addAdapter(AbstractAdapter $adapter) |
69
|
|
|
{ |
70
|
|
|
$this->adapters->add($adapter); |
|
|
|
|
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Clear list of adapters. |
77
|
|
|
*/ |
78
|
|
|
public function clearAdapters() |
79
|
|
|
{ |
80
|
|
|
$this->adapters->clear(); |
|
|
|
|
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Retrieve registered notifications. |
87
|
|
|
* |
88
|
|
|
* @return \Jgut\Tify\Notification[] |
89
|
|
|
*/ |
90
|
|
|
public function getNotifications() |
91
|
|
|
{ |
92
|
|
|
return $this->notifications->toArray(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Register notification. |
97
|
|
|
* |
98
|
|
|
* @param \Jgut\Tify\Notification $notification |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function addNotification(Notification $notification) |
103
|
|
|
{ |
104
|
|
|
$this->notifications->add($notification); |
|
|
|
|
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Clear list of notifications. |
111
|
|
|
*/ |
112
|
|
|
public function clearNotifications() |
113
|
|
|
{ |
114
|
|
|
$this->notifications->clear(); |
|
|
|
|
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Push notifications. |
121
|
|
|
* |
122
|
|
|
* @return \Jgut\Tify\Result[] |
123
|
|
|
*/ |
124
|
|
|
public function push() |
125
|
|
|
{ |
126
|
|
|
$results = []; |
127
|
|
|
|
128
|
|
|
/** @var \Jgut\Tify\Adapter\SendAdapter[] $pushAdapters */ |
129
|
|
|
$pushAdapters = array_filter( |
130
|
|
|
$this->adapters->toArray(), |
|
|
|
|
131
|
|
|
function (AbstractAdapter $adapter) { |
132
|
|
|
return $adapter instanceof SendAdapter; |
133
|
|
|
} |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
foreach ($pushAdapters as $adapter) { |
137
|
|
|
foreach ($this->notifications as $notification) { |
138
|
|
|
$notification->clearResults(); |
139
|
|
|
|
140
|
|
|
$adapter->send($notification); |
141
|
|
|
|
142
|
|
|
$results[] = $notification->getResults(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$return = []; |
147
|
|
|
array_walk_recursive($results, function ($current) use (&$return) { |
148
|
|
|
$return[] = $current; |
149
|
|
|
}); |
150
|
|
|
|
151
|
|
|
return $return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get feedback from push services. |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function feedback() |
160
|
|
|
{ |
161
|
|
|
$results = []; |
162
|
|
|
|
163
|
|
|
/** @var \Jgut\Tify\Adapter\FeedbackAdapter[] $feedbackAdapters */ |
164
|
|
|
$feedbackAdapters = array_filter( |
165
|
|
|
$this->adapters->toArray(), |
|
|
|
|
166
|
|
|
function (AbstractAdapter $adapter) { |
167
|
|
|
return $adapter instanceof FeedbackAdapter; |
168
|
|
|
} |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
foreach ($feedbackAdapters as $adapter) { |
172
|
|
|
$results[] = $adapter->feedback(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$return = []; |
176
|
|
|
array_walk_recursive($results, function ($current) use (&$return) { |
177
|
|
|
$return[] = $current; |
178
|
|
|
}); |
179
|
|
|
|
180
|
|
|
return array_unique($return); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..