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 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 7
dl 0
loc 94
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createClient() 0 18 2
B sendRequest() 0 24 2
A getMPNSHeaders() 0 13 1
A getHeaders() 0 18 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\NotificationService\MPNS;
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\Exception\ClientException;
16
use Buzz\Browser;
17
use Buzz\Client\MultiCurl;
18
19
/**
20
 * Class ServiceClient
21
 * @package Zbox\UnifiedPush\NotificationService\MPNS
22
 */
23
class ServiceClient extends ServiceClientBase
24
{
25
    /**
26
     * Initializing HTTP client
27
     *
28
     * @return $this
29
     */
30
    protected function createClient()
31
    {
32
        $client = new MultiCurl();
33
34
        $credentials     = $this->getCredentials();
35
        $isAuthenticated = $credentials instanceof SSLCertificate;
36
37
        $client->setVerifyPeer($isAuthenticated);
38
39
        if ($isAuthenticated) {
40
            $client->setOption(CURLOPT_SSLCERT, $credentials->getCertificatePassPhrase());
41
            $client->setOption(CURLOPT_SSLCERTPASSWD, $credentials->getCertificatePassPhrase());
42
        }
43
44
        $this->serviceClient = new Browser($client);
45
46
        return $this;
47
    }
48
49
    /**
50
     * @throws ClientException
51
     * @return ResponseInterface
52
     */
53
    public function sendRequest()
54
    {
55
        $notification = $this->getNotificationOrThrowException();
56
57
        try {
58
            $connection  = $this->getClientConnection();
59
            $serviceURL  = $this->getServiceURL();
60
            $url         = str_replace('[TOKEN]', $notification->getRecipients()->current(), $serviceURL['url']);
61
62
            $response =
63
                $connection->post(
64
                    $url,
65
                    $this->getHeaders(),
66
                    $notification->getPayload()
67
                );
68
69
            $connection->getClient()->flush();
70
71
        } catch (\Exception $e) {
72
            throw new ClientException($e->getMessage());
73
        }
74
75
        return new Response($response, $notification->getRecipients());
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    protected function getMPNSHeaders()
82
    {
83
        $notification = $this->getNotificationOrThrowException();
84
85
        $customNotification = $notification->getCustomNotificationData();
86
87
        return
88
            array(
89
                'X-MessageID'           => $customNotification['message_id'],
90
                'X-NotificationClass'   => $customNotification['delay_interval'],
91
                'X-WindowsPhone-Target' => $customNotification['message_type']
92
            );
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    protected function getHeaders()
99
    {
100
        $headers = array(
101
            'Accept'        => 'application/*',
102
            'Content-Type'  => 'text/xml'
103
        );
104
105
        $headers += $this->getMPNSHeaders();
106
107
        return
108
            array_map(
109
                function ($value, $key) {
110
                    return sprintf("%s: %s'", $key, $value);
111
                },
112
                $headers,
113
                array_keys($headers)
114
            );
115
    }
116
}
117