Completed
Push — master ( 020c5c...9dbce5 )
by Julián
08:49
created

Service::__construct()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 20
rs 8.8571
cc 5
eloc 11
nc 9
nop 2
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\PushAdapter;
16
17
/**
18
 * Notifications service.
19
 */
20
class Service
21
{
22
    /**
23
     * @var \Doctrine\Common\Collections\ArrayCollection
24
     */
25
    protected $adapters;
26
27
    /**
28
     * @var \Doctrine\Common\Collections\ArrayCollection
29
     */
30
    protected $notifications;
31
32
    /**
33
     * Manager constructor.
34
     *
35
     * @param \Jgut\Tify\Adapter\AbstractAdapter|\Jgut\Tify\Adapter\AbstractAdapter[]|null $adapters
36
     * @param \Jgut\Tify\Notification|\Jgut\Tify\Notification[]|null                       $notifications
37
     */
38
    public function __construct($adapters = null, $notifications = null)
39
    {
40
        $this->adapters = new ArrayCollection;
41
        if ($adapters !== null) {
42
            if (!is_array($adapters)) {
43
                $adapters = [$adapters];
44
            }
45
46
            $this->setAdapters($adapters);
47
        }
48
49
        $this->notifications = new ArrayCollection;
50
        if ($notifications !== null) {
51
            if (!is_array($notifications)) {
52
                $notifications = [$notifications];
53
            }
54
55
            $this->setNotifications($notifications);
56
        }
57
    }
58
59
    /**
60
     * Retrieve registered adapters.
61
     *
62
     * @return \Jgut\Tify\Adapter\AbstractAdapter[]
63
     */
64
    public function getAdapters()
65
    {
66
        return $this->adapters->toArray();
67
    }
68
69
    /**
70
     * Register adapters.
71
     *
72
     * @param array $adapters
73
     *
74
     * @return $this
75
     */
76
    public function setAdapters(array $adapters)
77
    {
78
        $this->adapters->clear();
79
80
        foreach ($adapters as $adapter) {
81
            $this->addAdapter($adapter);
82
        }
83
84
        return $this;
85
    }
86
87
    /**
88
     * Register adapter.
89
     *
90
     * @param \Jgut\Tify\Adapter\AbstractAdapter $adapter
91
     *
92
     * @return $this
93
     */
94
    public function addAdapter(AbstractAdapter $adapter)
95
    {
96
        $this->adapters->add($adapter);
97
98
        return $this;
99
    }
100
101
    /**
102
     * Clear list of adapters.
103
     */
104
    public function clearAdapters()
105
    {
106
        $this->adapters->clear();
107
108
        return $this;
109
    }
110
111
    /**
112
     * Retrieve registered notifications.
113
     *
114
     * @return \Jgut\Tify\Notification[]
115
     */
116
    public function getNotifications()
117
    {
118
        return $this->notifications->toArray();
119
    }
120
121
    /**
122
     * Register notifications.
123
     *
124
     * @param array $notifications
125
     *
126
     * @return $this
127
     */
128
    public function setNotifications(array $notifications)
129
    {
130
        $this->notifications->clear();
131
132
        foreach ($notifications as $notification) {
133
            $this->addNotification($notification);
134
        }
135
136
        return $this;
137
    }
138
139
    /**
140
     * Register notification.
141
     *
142
     * @param \Jgut\Tify\Notification $notification
143
     *
144
     * @return $this
145
     */
146
    public function addNotification(Notification $notification)
147
    {
148
        $this->notifications->add($notification);
149
150
        return $this;
151
    }
152
153
    /**
154
     * Clear list of notifications.
155
     */
156
    public function clearNotifications()
157
    {
158
        $this->notifications->clear();
159
160
        return $this;
161
    }
162
163
    /**
164
     * Push notifications.
165
     *
166
     * @return \Jgut\Tify\Result[]
167
     */
168
    public function push()
169
    {
170
        $pushResults = [];
171
172
        /** @var \Jgut\Tify\Adapter\PushAdapter[] $pushAdapters */
173
        $pushAdapters = array_filter(
174
            $this->adapters->toArray(),
175
            function (AbstractAdapter $adapter) {
176
                return $adapter instanceof PushAdapter;
177
            }
178
        );
179
180
        foreach ($pushAdapters as $adapter) {
181
            foreach ($this->notifications as $notification) {
182
                foreach ($adapter->push($notification) as $pushResult) {
183
                    $pushResults[] = $pushResult;
184
                }
185
            }
186
        }
187
188
        return $pushResults;
189
    }
190
191
    /**
192
     * Get feedback from push services.
193
     *
194
     * @return \Jgut\Tify\Result[]
195
     */
196
    public function feedback()
197
    {
198
        $feedbackResults = [];
199
200
        /** @var \Jgut\Tify\Adapter\FeedbackAdapter[] $feedbackAdapters */
201
        $feedbackAdapters = array_filter(
202
            $this->adapters->toArray(),
203
            function (AbstractAdapter $adapter) {
204
                return $adapter instanceof FeedbackAdapter;
205
            }
206
        );
207
208
        foreach ($feedbackAdapters as $adapter) {
209
            foreach ($adapter->feedback() as $feedbackResult) {
210
                $feedbackResults[] = $feedbackResult;
211
            }
212
        }
213
214
        return array_unique($feedbackResults, SORT_REGULAR);
215
    }
216
}
217