Completed
Pull Request — master (#28)
by Adam
03:07
created

serviceRespondedWithAnError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace NotificationChannels\Webhook\Exceptions;
4
5
use GuzzleHttp\Psr7\Response;
6
7
class CouldNotSendNotification extends \Exception
8
{
9
    private $response;
10
11
    /**
12
     * CouldNotSendNotification constructor.
13
     * @param Response $response
14
     * @param string $message
15
     * @param int|null $code
16
     */
17 1
    public function __construct(Response $response, string $message, int $code = null)
18
    {
19 1
        $this->response = $response;
20 1
        $this->message = $message;
21 1
        $this->code = $code ?? $response->getStatusCode();
22
23 1
        parent::__construct($message, $code);
24 1
    }
25
26
    /**
27
     * @param Response $response
28
     * @return CouldNotSendNotification
29
     */
30 1
    public static function serviceRespondedWithAnError(Response $response)
31
    {
32 1
        return new self(
33 1
            $response,
34 1
            sprintf('Webhook responded with an error: `%s`', $response->getBody()->getContents())
35
        );
36
    }
37
38
    /**
39
     * @return Response
40
     */
41
    public function getResponse()
42
    {
43
        return $this->response;
44
    }
45
}
46