AbstractResponse   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 16
c 1
b 0
f 0
dl 0
loc 61
ccs 13
cts 15
cp 0.8667
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hasError() 0 3 1
A getErrorCode() 0 3 1
A getErrorDescription() 0 3 1
A getErrorId() 0 3 1
A fromHttpResponse() 0 15 2
1
<?php
2
3
namespace Anticaptcha\Response;
4
5
use Anticaptcha\Entity;
6
use Exception;
7
use Psr\Http\Message\ResponseInterface;
8
use RuntimeException;
9
10
/**
11
 * Class Response
12
 */
13
abstract class AbstractResponse extends Entity
14
{
15
    /*
16
     * Error identifier.
17
     * 0 - no errors, operation completed successfully.
18
     * >1 - error identifier. Error code and short description transferred in errorCode and errorDescription properties.
19
     */
20
    public int $errorId = 0;
21
22
    /*
23
     * An error code, won't be included in the output if request produced no errors.
24
     */
25
    public string $errorCode = '';
26
27
    /*
28
     * Short description of the error
29
     */
30
    public string $errorDescription = '';
31
32
    /**
33
     * @param ResponseInterface $httpResponse
34
     *
35
     * @return $this
36
     *
37
     * @throws Exception
38
     */
39 2
    public static function fromHttpResponse(ResponseInterface $httpResponse): self
40
    {
41 2
        $properties = json_decode(
42 2
            $httpResponse->getBody()->__toString(),
43
            true,
44
            JSON_THROW_ON_ERROR
45
        );
46
47 2
        if (!is_array($properties)) {
48
            throw new RuntimeException(
49
                sprintf('Unexpected API response. Dump: %s', var_export($httpResponse, true))
50
            );
51
        }
52
53 2
        return new static($properties);
54
    }
55
56 2
    public function hasError(): bool
57
    {
58 2
        return $this->errorId > 0;
59
    }
60
61 2
    public function getErrorId(): int
62
    {
63 2
        return $this->errorId;
64
    }
65
66 2
    public function getErrorCode(): string
67
    {
68 2
        return $this->errorCode;
69
    }
70
71 2
    public function getErrorDescription(): string
72
    {
73 2
        return $this->errorDescription;
74
    }
75
}
76