|
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 Jgut\Tify\Service\AbstractService; |
|
13
|
|
|
use Jgut\Tify\Exception\ServiceException; |
|
14
|
|
|
use Jgut\Tify\Notification\AbstractNotification; |
|
15
|
|
|
use Jgut\Tify\Service\FeedbackInterface; |
|
16
|
|
|
|
|
17
|
|
|
class Manager |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Registered notifications. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $notifications = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Push notifications. |
|
28
|
|
|
* |
|
29
|
|
|
* @return \Jgut\Tify\Result[] |
|
30
|
|
|
*/ |
|
31
|
|
|
public function push() |
|
32
|
|
|
{ |
|
33
|
|
|
$results = []; |
|
34
|
|
|
|
|
35
|
|
|
foreach ($this->notifications as $notification) { |
|
36
|
|
|
$notification->setPending(); |
|
37
|
|
|
|
|
38
|
|
|
$notification->getService()->send($notification); |
|
39
|
|
|
|
|
40
|
|
|
$results = array_merge($results, $notification->getResults()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $results; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get feedback from service. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Jgut\Tify\Service\AbstractService $service |
|
50
|
|
|
* |
|
51
|
|
|
* @throws \Jgut\Tify\Exception\ServiceException |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function feedback(AbstractService $service) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$service instanceof FeedbackInterface) { |
|
58
|
|
|
throw new ServiceException(sprintf('%s is not a feedback enabled service', get_class($service))); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $service->feedback(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Retrieve registered notifications. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getNotifications() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->notifications; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Register notification. |
|
76
|
|
|
* |
|
77
|
|
|
* @param \Jgut\Tify\Notification\AbstractNotification $notification |
|
78
|
|
|
*/ |
|
79
|
|
|
public function addNotification(AbstractNotification $notification) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->notifications[] = $notification; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Clear list of notifications. |
|
88
|
|
|
*/ |
|
89
|
|
|
public function clearNotifications() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->notifications = []; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|