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 ( d88fce...019e6b )
by Alexander
03:10
created

ServiceClient::sendRequest()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 11
nc 5
nop 0
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\ServiceClientBase;
13
use Zbox\UnifiedPush\Exception\ClientException;
14
use Zbox\UnifiedPush\Utils\SocketClient;
15
16
/**
17
 * Class ServiceClient
18
 * @package Zbox\UnifiedPush\NotificationService\APNS
19
 */
20
class ServiceClient extends ServiceClientBase
21
{
22
    const SECURE_TRANSPORT_DEFAULT = 'ssl';
23
24
    /**
25
     * Initializing socket client
26
     *
27
     * @return $this
28
     */
29
    protected function createClient()
30
    {
31
        $credentials         = $this->getCredentials();
32
        $url                 = $this->getServiceURL();
33
        $transport           = !empty($url['transport']) ? $url['transport'] : self::SECURE_TRANSPORT_DEFAULT;
34
35
        $this->serviceClient = new SocketClient($url['host'], $url['port']);
36
        $this->serviceClient
37
            ->setTransport($transport)
38
            ->setContextOptions(array(
39
                'local_cert' => $credentials->getCertificate(),
40
                'passphrase' => $credentials->getCertificatePassPhrase(),
41
            ))
42
            ->addConnectionFlag(STREAM_CLIENT_PERSISTENT)
43
            ->setBlockingMode(false)
44
        ;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Gets socket connection (reestablish, if needed)
51
     *
52
     * @return SocketClient
53
     */
54
    public function getClientConnection()
55
    {
56
        if (!$this->serviceClient) {
57
            $this->createClient();
58
        }
59
60
        if (!$this->serviceClient->isAlive()) {
61
            $this->serviceClient->connect();
62
        }
63
64
        return $this->serviceClient;
65
    }
66
67
    /**
68
     * If you send a notification that is accepted by APNs,
69
     * nothing is returned. If you send a notification that is malformed
70
     * or otherwise unintelligible, APNs returns an error-response packet
71
     *
72
     * @throws ClientException
73
     * @return bool
74
     */
75
    public function sendRequest()
76
    {
77
        $notification = $this->getNotificationOrThrowException();
78
79
        try {
80
            $connection = $this->getClientConnection();
81
            $connection->write($notification['body']);
82
83
            $errorResponseData = $connection->read(Response::ERROR_RESPONSE_LENGTH);
84
85
        } catch (\Exception $e) {
86
            throw new ClientException($e->getMessage());
87
        }
88
89
        if ($errorResponseData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errorResponseData of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
90
            new Response($errorResponseData, $notification['recipients']);
91
        }
92
        return true;
93
    }
94
}
95