GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 743bf3...5407ad )
by Alexander
02:22
created

Dispatcher::sendNotification()   B

Complexity

Conditions 4
Paths 13

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
rs 8.5806
cc 4
eloc 24
nc 13
nop 1
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;
11
12
use Zbox\UnifiedPush\Message\MessageInterface;
13
use Zbox\UnifiedPush\Message\MessageCollection;
14
use Zbox\UnifiedPush\Notification\Notification;
15
use Zbox\UnifiedPush\Notification\NotificationBuilder;
16
use Zbox\UnifiedPush\NotificationService\NotificationServices,
17
    Zbox\UnifiedPush\NotificationService\ServiceClientInterface,
18
    Zbox\UnifiedPush\NotificationService\ServiceClientFactory,
19
    Zbox\UnifiedPush\NotificationService\ResponseHandler;
20
use Zbox\UnifiedPush\Exception\ClientException,
21
    Zbox\UnifiedPush\Exception\RuntimeException;
22
use Psr\Log\LoggerAwareInterface,
23
    Psr\Log\LoggerInterface,
24
    Psr\Log\NullLogger;
25
26
/**
27
 * Class Dispatcher
28
 * @package Zbox\UnifiedPush
29
 */
30
class Dispatcher implements LoggerAwareInterface
31
{
32
    /**
33
     * @var array
34
     */
35
    private $connectionPool;
36
37
    /**
38
     * @var ServiceClientFactory
39
     */
40
    private $clientFactory;
41
42
    /**
43
     * @var NotificationBuilder
44
     */
45
    private $notificationBuilder;
46
47
    /**
48
     * @var ResponseHandler
49
     */
50
    private $responseHandler;
51
52
    /**
53
     * @var LoggerInterface
54
     */
55
    private $logger;
56
57
    /**
58
     * @param ServiceClientFactory $clientFactory
59
     * @param NotificationBuilder $notificationBuilder
60
     * @param ResponseHandler $responseHandler
61
     */
62
    public function __construct(
63
        ServiceClientFactory $clientFactory,
64
        NotificationBuilder $notificationBuilder,
65
        ResponseHandler $responseHandler
66
    ){
67
        $this->clientFactory        = $clientFactory;
68
        $this->notificationBuilder  = $notificationBuilder;
69
        $this->responseHandler      = $responseHandler;
70
71
        $this->setLogger(new NullLogger());
72
    }
73
74
    /**
75
     * @param LoggerInterface $logger
76
     * @return $this
77
     */
78
    public function setLogger(LoggerInterface $logger)
79
    {
80
        $this->logger = $logger;
81
        return $this;
82
    }
83
84
    /**
85
     * @param bool $isDevelopment
86
     * @return $this
87
     */
88
    public function setDevelopmentMode($isDevelopment)
89
    {
90
        $this->clientFactory->setDevelopmentMode($isDevelopment);
91
        return $this;
92
    }
93
94
    /**
95
     * Gets a service client connection by service name
96
     *
97
     * @param string $serviceName
98
     * @return ServiceClientInterface
99
     */
100
    public function getConnection($serviceName)
101
    {
102
        if (empty($this->connectionPool[$serviceName])) {
103
            $this->initConnection($serviceName);
104
        }
105
106
        return $this->connectionPool[$serviceName];
107
    }
108
109
    /**
110
     * Initialize service client connection by service name
111
     *
112
     * @param string $serviceName
113
     * @return $this
114
     */
115
    public function initConnection($serviceName)
116
    {
117
        $connection = $this->clientFactory->createServiceClient($serviceName);
118
        $this->connectionPool[$serviceName] = $connection;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Creates a feedback service connection
125
     *
126
     * @param string $serviceName
127
     * @return ServiceClientInterface
128
     */
129
    public function initFeedbackConnection($serviceName)
130
    {
131
        return $this->clientFactory->createServiceClient($serviceName, true);
132
    }
133
134
    /**
135
     * @return ResponseHandler
136
     */
137
    public function getResponseHandler()
138
    {
139
        return $this->responseHandler;
140
    }
141
142
    /**
143
     * Build notification and send it to notification service
144
     *
145
     * @param MessageInterface $message
146
     * @return $this
147
     */
148
    public function dispatch(MessageInterface $message)
149
    {
150
        $builder = $this->notificationBuilder;
151
152
        $builder->buildNotifications($message);
153
154
        $notifications = $builder->getNotificationCollection();
155
156
        /** @var Notification $notification */
157
        foreach ($notifications as $notification) {
158
            $this->sendNotification($notification);
159
        }
160
161
        return $this;
162
    }
163
164
    /**
165
     * Tries to dispatch all messages to notification service
166
     *
167
     * @param MessageCollection $messages
168
     * @return $this
169
     */
170
    public function dispatchAll(MessageCollection $messages)
171
    {
172
        $collection = $messages->getMessageCollection();
173
174
        while ($collection->valid()) {
175
            $message = $collection->current();
176
            $this->dispatch($message);
177
            $collection->next();
178
        }
179
180
        return $this;
181
    }
182
183
    /**
184
     * Tries to connect and send a notification
185
     *
186
     * @param Notification $notification
187
     * @return bool
188
     */
189
    public function sendNotification(Notification $notification)
190
    {
191
        try {
192
            $connection = $this->getConnection($notification->getType());
193
            $connection->setNotification($notification);
194
195
            $this->logger->info(
196
                sprintf(
197
                    "Dispatching notification id: %s",
198
                    $notification->getIdentifier()
199
                )
200
            );
201
202
            $this
203
                ->responseHandler
204
                ->addIdentifiedResponse(
205
                    $notification->getIdentifier(),
206
                    $connection->sendRequest()
207
                );
208
209
            return true;
210
211
        } catch (ClientException $e) {
212
            $this->logger->error(
213
                sprintf("Client connection error: %s", $e->getMessage())
214
            );
215
216
        } catch (RuntimeException $e) {
217
            $this->logger->error(
218
                sprintf("Runtime error: %s", $e->getMessage())
219
            );
220
221
        } catch (\Exception $e) {
222
            $this->logger->error(
223
                sprintf("Error occurs: %s", $e->getMessage())
224
            );
225
        }
226
227
        return false;
228
    }
229
230
    /**
231
     * Tries to connect and load feedback data
232
     *
233
     * @return $this
234
     * @throws RuntimeException
235
     * @throws \Exception
236
     */
237
    public function loadFeedback()
238
    {
239
        $serviceName = NotificationServices::APPLE_PUSH_NOTIFICATIONS_SERVICE;
240
241
        try {
242
            $this->logger->info(sprintf("Querying the feedback service '%s'", $serviceName));
243
244
            $connection = $this->initFeedbackConnection($serviceName);
245
246
            $this
247
                ->responseHandler
248
                ->addResponse(
249
                    $connection->sendRequest()
250
                );
251
252
        } catch (RuntimeException $e) {
253
            $this->logger->error(
254
                sprintf("Runtime error while acquiring feedback: %s", $e->getMessage())
255
            );
256
257
        } catch (\Exception $e) {
258
            $this->logger->error(
259
                sprintf("Error occurs while acquiring feedback: %s", $e->getMessage())
260
            );
261
        }
262
263
        return $this;
264
    }
265
}
266