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

UnexpectedJsonTextResponseException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A shorten() 0 11 2
A create() 0 6 1
A getJsonText() 0 3 1
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