serviceRespondedWithAnError()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace NotificationChannels\Line\Exceptions;
4
5
use LINE\LINEBot\Response;
6
use LINE\LINEBot\Exception\CurlExecutionException;
7
8
class CouldNotSendNotification extends \Exception
9
{
10
    public static function serviceRespondedWithAnError(Response $response)
11
    {
12
        $responseBody = $response->getJSONDecodedBody();
13
        $errorMsg = $response->getHeader('X-Line-Request-Id').': '.$responseBody['message'];
14
15
        if (preg_match('/The request body has [\d]+ errors?/', $responseBody['message'], $matches)) {
16
            foreach ($responseBody['details'] as $detail) {
17
                $errorMsg .= PHP_EOL.$detail['message'];
18
                $errorMsg .= PHP_EOL.$detail['property'];
19
            }
20
        }
21
22
        return new static($errorMsg);
23
    }
24
25
    public static function curlError(CurlExecutionException $exeption)
26
    {
27
        return new static('LINEBot Curl error: '.$exeption->getMessage());
28
    }
29
30
    public static function exceededOfMessages(int $count)
31
    {
32
        return new static('It exceeds the maximum of messages that can be sent. max:5, attempted:'.$count);
33
    }
34
}
35