Completed
Push — master ( c63e30...778a07 )
by Ryota
05:20
created

CouldNotSendNotification::curlError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace NotificationChannels\Line\Exceptions;
4
5
use LINE\LINEBot\Exception\CurlExecutionException;
6
use LINE\LINEBot\Response;
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