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.

Pushwoosh   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 69.69%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
c 2
b 0
f 0
dl 0
loc 112
ccs 23
cts 33
cp 0.6969
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tryAgainIfCausedByPushwooshServerError() 0 11 3
A getApiToken() 0 3 1
A __construct() 0 5 1
A getApplicationCode() 0 3 1
B createMessage() 0 32 7
A send() 0 3 1
1
<?php
2
3
namespace NotificationChannels\Pushwoosh;
4
5
use GuzzleHttp\ClientInterface;
6
use function GuzzleHttp\json_decode;
7
use GuzzleHttp\Psr7\Request;
8
use NotificationChannels\Pushwoosh\Concerns\DetectsPushwooshErrors;
9
use NotificationChannels\Pushwoosh\Exceptions\PushwooshException;
10
use NotificationChannels\Pushwoosh\Exceptions\UnknownDeviceException;
11
use Throwable;
12
13
class Pushwoosh
14
{
15
    use DetectsPushwooshErrors;
16
17
    protected $application;
18
    protected $client;
19
    protected $token;
20
21
    /**
22
     * Create a new Pushwoosh API client.
23
     *
24
     * @param \GuzzleHttp\ClientInterface $client
25
     * @param string $application
26
     * @param string $token
27
     * @return void
28
     */
29 18
    public function __construct(ClientInterface $client, string $application, string $token)
30
    {
31 18
        $this->application = $application;
32 18
        $this->client = $client;
33 18
        $this->token = $token;
34 18
    }
35
36
    /**
37
     * Create the given message in the Pushwoosh API.
38
     *
39
     * @param \NotificationChannels\Pushwoosh\PushwooshPendingMessage $message
40
     * @return string[]
41
     */
42 18
    public function createMessage(PushwooshPendingMessage $message)
43
    {
44 18
        $headers = ['Accept' => 'application/json', 'Content-Type' => 'application/json'];
45 18
        $payload = \GuzzleHttp\json_encode(['request' => $message]);
46 18
        $request = new Request('POST', 'https://cp.pushwoosh.com/json/1.3/createMessage', $headers, $payload);
47
48
        try {
49 18
            $response = $this->client->send($request);
50
        } catch (Throwable $e) {
51
            $response = $this->tryAgainIfCausedByPushwooshServerError($request, $e);
52
        }
53
54 18
        $response = json_decode($response->getBody()->getContents());
55
56 18
        if (isset($response->status_code) && $response->status_code !== 200) {
57 6
            throw new PushwooshException($response->status_message);
58
        }
59
60 12
        if (isset($response->response->UnknownDevices)) {
61 6
            throw new UnknownDeviceException($response->response->UnknownDevices);
62
        }
63
64 6
        $message->wasSent();
65
66 6
        if (isset($response->response->Messages)) {
67
            # Pushwoosh will not assign IDs to messages sent to less than 10 unique devices
68
            return array_map(function (string $identifier) {
69 6
                return $identifier !== 'CODE_NOT_AVAILABLE' ? $identifier : null;
70 6
            }, $response->response->Messages);
71
        }
72
73
        return [];
74
    }
75
76
    /**
77
     * Get the Pushwoosh API token.
78
     *
79
     * @return string
80
     */
81 18
    public function getApiToken()
82
    {
83 18
        return $this->token;
84
    }
85
86
    /**
87
     * Get the Pushwoosh application code.
88
     *
89
     * @return string
90
     */
91 18
    public function getApplicationCode()
92
    {
93 18
        return $this->application;
94
    }
95
96
    /**
97
     * Send the message.
98
     *
99
     * @param \NotificationChannels\Pushwoosh\PushwooshMessage $message
100
     * @return \NotificationChannels\Pushwoosh\PushwooshPendingMessage
101
     */
102
    public function send(PushwooshMessage $message)
103
    {
104
        return (new PushwooshPendingMessage($this))->queue($message);
105
    }
106
107
    /**
108
     * Handle a Pushwoosh communication error.
109
     *
110
     * @param \GuzzleHttp\Psr7\Request $request
111
     * @param \Throwable $e
112
     * @return \Psr\Http\Message\ResponseInterface
113
     */
114
    protected function tryAgainIfCausedByPushwooshServerError(Request $request, Throwable $e)
115
    {
116
        if ($this->causedByPushwooshServerError($e)) {
117
            try {
118
                return $this->client->send($request);
119
            } catch (Throwable $e) {
120
                // Do nothing...
121
            }
122
        }
123
124
        throw new PushwooshException('Failed to create message(s)', 0, $e);
125
    }
126
}
127