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
Pull Request — master (#8)
by Choraimy
07:33 queued 05:08
created

Pushwoosh::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace NotificationChannels\Pushwoosh;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\GuzzleException;
7
use GuzzleHttp\Psr7\Request;
8
use NotificationChannels\Pushwoosh\Exceptions\PushwooshException;
9
use NotificationChannels\Pushwoosh\Exceptions\UnknownDeviceException;
10
11
class Pushwoosh
12
{
13
    protected $application;
14
    protected $client;
15
    protected $token;
16
17
    /**
18
     * Create a new Pushwoosh API client.
19
     *
20
     * @param \GuzzleHttp\ClientInterface $client
21
     * @param string $application
22
     * @param string $token
23
     * @return void
24
     */
25 15
    public function __construct(ClientInterface $client, string $application, string $token)
26
    {
27 15
        $this->application = $application;
28 15
        $this->client = $client;
29 15
        $this->token = $token;
30 15
    }
31
32
    /**
33
     * Create the given message in the Pushwoosh API.
34
     *
35
     * @param \NotificationChannels\Pushwoosh\PushwooshPendingMessage $message
36
     * @return string[]
37
     */
38 15
    public function createMessage(PushwooshPendingMessage $message)
39
    {
40 15
        $headers = ['Accept' => 'application/json', 'Content-Type' => 'application/json'];
41 15
        $payload = \GuzzleHttp\json_encode(['request' => $message]);
42 15
        $request = new Request('POST', 'https://cp.pushwoosh.com/json/1.3/createMessage', $headers, $payload);
43
44
        try {
45 15
            $response = $this->client->send($request);
46
        } catch (GuzzleException $exception) {
47
            throw new PushwooshException('Failed to create message(s)', 0, $exception);
48
        }
49
50 15
        $response = \GuzzleHttp\json_decode($response->getBody()->getContents());
51
52 15
        if (isset($response->status_code) && $response->status_code !== 200) {
53 5
            throw new PushwooshException($response->status_message);
54
        }
55
56 10
        if (isset($response->response->UnknownDevices)) {
57 5
            throw new UnknownDeviceException($response->response->UnknownDevices);
58
        }
59
60 5
        $message->wasSent();
61
62 5
        if (isset($response->response->Messages)) {
63
            # Pushwoosh will not assign IDs to messages sent to less than 10 unique devices
64
            return array_map(function (string $identifier) {
65 5
                return $identifier !== 'CODE_NOT_AVAILABLE' ? $identifier : null;
66 5
            }, $response->response->Messages);
67
        }
68
69
        return [];
70
    }
71
72
    /**
73
     * Get the Pushwoosh API token.
74
     *
75
     * @return string
76
     */
77 15
    public function getApiToken()
78
    {
79 15
        return $this->token;
80
    }
81
82
    /**
83
     * Get the Pushwoosh application code.
84
     *
85
     * @return string
86
     */
87 15
    public function getApplicationCode()
88
    {
89 15
        return $this->application;
90
    }
91
92
    /**
93
     * Send the message.
94
     *
95
     * @param \NotificationChannels\Pushwoosh\PushwooshMessage $message
96
     * @return \NotificationChannels\Pushwoosh\PushwooshPendingMessage
97
     */
98
    public function send(PushwooshMessage $message)
99
    {
100
        return (new PushwooshPendingMessage($this))->queue($message);
101
    }
102
}
103