CouldNotSendNotification::internalError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NotificationChannels\HipChat\Exceptions;
4
5
use Exception;
6
use GuzzleHttp\Exception\ClientException;
7
use NotificationChannels\HipChat\HipChatMessage;
8
9
class CouldNotSendNotification extends Exception
10
{
11
    /**
12
     * Thrown when there's a bad request from the HttpClient.
13
     *
14
     * @param \GuzzleHttp\Exception\ClientException $exception
15
     * @return static
16
     */
17
    public static function hipChatRespondedWithAnError(ClientException $exception)
18
    {
19
        return new static(sprintf(
20
            'HipChat responded with an error %s - %s',
21
            $exception->getResponse()->getStatusCode(),
22
            $exception->getResponse()->getBody()
23
        ));
24
    }
25
26
    /**
27
     * Thrown when to (room identifier) is missing.
28
     *
29
     * @return static
30
     */
31 1
    public static function missingTo()
32
    {
33 1
        return new static('Notification was not sent. Room identifier is missing.');
34
    }
35
36
    /**
37
     * Thrown when an invalid message object was passed.
38
     *
39
     * @param mixed $message
40
     * @return static
41
     */
42 1
    public static function invalidMessageObject($message)
43
    {
44 1
        return new static(sprintf(
45 1
            'Notification was not sent. The message should be an instance of or extend %s. Given % is invalid.',
46 1
            HipChatMessage::class,
47 1
            get_class($message) ?: 'Unknown'
48
        ));
49
    }
50
51
    /**
52
     * Thrown when any other exception is caught while sending a notification message.
53
     *
54
     * @return static
55
     */
56
    public static function internalError($exception = null)
57
    {
58
        return new static('Error occurred while sending the message.', 0, $exception);
59
    }
60
}
61