Completed
Push — master ( 97e8d1...15f2c9 )
by Cody
02:35
created

serviceRespondedWithAnApiError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace NotificationChannels\Discord\Exceptions;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Psr\Http\Message\ResponseInterface;
8
9
class CouldNotSendNotification extends Exception
10
{
11
    /**
12
     * @param \Psr\Http\Message\ResponseInterface $response
13
     *
14
     * @return static
15
     */
16
    public static function serviceRespondedWithAnHttpError(ResponseInterface $response)
17
    {
18
        $message = "Discord responded with an HTTP error: {$response->getStatusCode()}";
19
20
        if ($error = Arr::get(json_decode($response->getBody(), true), 'message')) {
21
            $message .= ": $error";
22
        }
23
24
        return new static($message);
25
    }
26
27
    /**
28
     * @param array $response
29
     *
30
     * @return static
31
     */
32
    public static function serviceRespondedWithAnApiError(array $response)
33
    {
34
        return new static("Discord responded with an API error: {$response['code']}: {$response['message']}");
35
    }
36
37
    /**
38
     * @param \Exception $exception
39
     *
40
     * @return static
41
     */
42
    public static function serviceCommunicationError(Exception $exception)
43
    {
44
        return new static("Communication with Discord failed: {$exception->getCode()}: {$exception->getMessage()}");
45
    }
46
}
47