Passed
Push — master ( 37cafd...a8b392 )
by
unknown
07:18
created

UnexpectedJsonTextResponseException::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Datamate\SeafileApi\Exception;
4
5
final class UnexpectedJsonTextResponseException extends InvalidResponseException
6
{
7
    /**
8
     * @var string the unexpected JSON Text of the API response
9
     */
10
    private $jsonText;
11
12
    /**
13
     * utility method to shorten a string (e.g. to create shorter messages)
14
     *
15
     * @param string $buffer
16
     * @param int $size
17
     * @return string ASCII, printable with other characters escaped (C-slashes)
18
     */
19
    public static function shorten(string $buffer, int $size = 32): string
20
    {
21
        if ($size >= $len = strlen($buffer)) {
22
            $buffer = addcslashes($buffer, "\0..\37\42\134\177..\377");
23
            return "\"$buffer\"";
24
        }
25
26
        $buffer = substr($buffer, 0, max(0, $size - 8));
27
        $buffer = addcslashes($buffer, "\0..\37\42\134\177..\377");
28
29
        return "($len) \"$buffer ...";
30
    }
31
32
    public static function create(string $message, string $jsonText, \Throwable $previous = null): UnexpectedJsonTextResponseException
33
    {
34
        $exception = new self($message, 500, $previous);
35
        $exception->jsonText = $jsonText;
36
37
        return $exception;
38
    }
39
40
    public function getJsonText(): string
41
    {
42
        return $this->jsonText;
43
    }
44
}
45