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.

ServiceClient   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 73
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createClient() 0 20 2
A getClientConnection() 0 12 3
A sendRequest() 0 16 3
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\NotificationService\APNS;
11
12
use Zbox\UnifiedPush\NotificationService\ResponseInterface;
13
use Zbox\UnifiedPush\NotificationService\ServiceClientBase;
14
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\SSLCertificate;
15
use Zbox\UnifiedPush\Utils\SocketClient;
16
use Zbox\UnifiedPush\Exception\ClientException;
17
18
/**
19
 * Class ServiceClient
20
 * @package Zbox\UnifiedPush\NotificationService\APNS
21
 */
22
class ServiceClient extends ServiceClientBase
23
{
24
    const SECURE_TRANSPORT_DEFAULT = 'ssl';
25
26
    /**
27
     * Initializing socket client
28
     *
29
     * @return $this
30
     */
31
    protected function createClient()
32
    {
33
        /** @var SSLCertificate $credentials */
34
        $credentials         = $this->getCredentials();
35
        $url                 = $this->getServiceURL();
36
        $transport           = !empty($url['transport']) ? $url['transport'] : self::SECURE_TRANSPORT_DEFAULT;
37
38
        $this->serviceClient = new SocketClient($url['host'], $url['port']);
39
        $this->serviceClient
40
            ->setTransport($transport)
41
            ->setContextOptions(array(
42
                'local_cert' => $credentials->getCertificate(),
43
                'passphrase' => $credentials->getCertificatePassPhrase(),
44
            ))
45
            ->addConnectionFlag(STREAM_CLIENT_PERSISTENT)
46
            ->setBlockingMode(false)
47
        ;
48
49
        return $this;
50
    }
51
52
    /**
53
     * Gets socket connection (reestablish, if needed)
54
     *
55
     * @return SocketClient
56
     */
57
    public function getClientConnection()
58
    {
59
        if (!$this->serviceClient) {
60
            $this->createClient();
61
        }
62
63
        if (!$this->serviceClient->isAlive()) {
64
            $this->serviceClient->connect();
65
        }
66
67
        return $this->serviceClient;
68
    }
69
70
    /**
71
     * If you send a notification that is accepted by APNs,
72
     * nothing is returned. If you send a notification that is malformed
73
     * or otherwise unintelligible, APNs returns an error-response packet
74
     *
75
     * @throws ClientException
76
     * @return ResponseInterface
77
     */
78
    public function sendRequest()
79
    {
80
        $notification = $this->getNotificationOrThrowException();
81
82
        try {
83
            $connection = $this->getClientConnection();
84
            $connection->write($notification->getPayload());
85
86
            $errorResponseData = $connection->read(Response::ERROR_RESPONSE_LENGTH);
87
88
        } catch (\Exception $e) {
89
            throw new ClientException($e->getMessage());
90
        }
91
92
        return new Response($errorResponseData ? : '', $notification->getRecipients());
93
    }
94
}
95