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 ( 6cfff6...86409e )
by Alexander
02:30
created

Dispatcher::dispatchAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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