AbstractResponse::__construct()   B
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.0933

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 40
ccs 16
cts 21
cp 0.7619
rs 8.0555
cc 9
nc 6
nop 1
crap 10.0933
1
<?php
2
3
namespace LE_ACME2\Response;
4
5
use LE_ACME2\Exception;
6
7
use LE_ACME2\Connector\RawResponse;
8
9
abstract class AbstractResponse {
10
11
    /** @var RawResponse $_raw */
12
    protected $_raw;
13
14
    protected $_pattern_header_location = '/^Location: (\S+)$/i';
15
16
    /**
17
     * AbstractResponse constructor.
18
     *
19
     * @param RawResponse $raw
20
     * @throws Exception\InvalidResponse
21
     * @throws Exception\RateLimitReached
22
     * @throws Exception\ServiceUnavailable
23
     */
24 3
    public function __construct(RawResponse $raw) {
25
26 3
        $this->_raw = $raw;
27
28 3
        if($this->_isServiceUnavailable()) {
29
30 2
            $detail = "";
31 2
            if(isset($raw->body['detail'])) {
32 2
                $detail = $raw->body['detail'];
33
            }
34
35 2
            $retryAfterMatches = $this->_preg_match_headerLine('/^Retry-After: (.+)$/i');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $retryAfterMatches is correct as $this->_preg_match_heade...^Retry-After: (.+)$/i') targeting LE_ACME2\Response\Abstra...preg_match_headerLine() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
37 2
            throw new Exception\ServiceUnavailable(
38 2
                $raw->request,
39
                $detail,
40 2
                $retryAfterMatches !== null ? $retryAfterMatches[1] : null
0 ignored issues
show
introduced by
The condition $retryAfterMatches !== null is always false.
Loading history...
41
            );
42
        }
43
44 1
        if($this->_isRateLimitReached()) {
45
46 1
            $detail = "";
47 1
            if(isset($raw->body['type']) && $raw->body['type'] == 'urn:ietf:params:acme:error:rateLimited') {
48 1
                $detail = $raw->body['detail'];
49
            }
50
51 1
            throw new Exception\RateLimitReached(
52 1
                $raw->request,
53
                $detail,
54
            );
55
        }
56
57
        $result = $this->_isValid();
58
        if(!$result) {
59
60
            $responseStatus = $this->_preg_match_headerLine('/^HTTP\/.* [0-9]{3,} /i');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $responseStatus is correct as $this->_preg_match_heade...HTTP\/.* [0-9]{3,} /i') targeting LE_ACME2\Response\Abstra...preg_match_headerLine() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
61
            throw new Exception\InvalidResponse(
62
                $raw,
63
                isset($responseStatus[1]) ? $responseStatus[1] : null,
64
            );
65
        }
66
    }
67
68
    protected function _preg_match_headerLine(string $pattern) : ?array {
69
70
        foreach($this->_raw->header as $line) {
71
72
            $matches = [];
73
            if(preg_match($pattern, $line, $matches) === 1) {
74
                return $matches;
75
            }
76
        }
77
        return null;
78
    }
79
80 1
    protected function _isRateLimitReached() : bool {
81 1
        return $this->_preg_match_headerLine('/^HTTP\/.* 429/i') !== null;
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_preg_match_headerLine('/^HTTP\/.* 429/i') targeting LE_ACME2\Response\Abstra...preg_match_headerLine() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
    }
83
84 2
    protected function _isServiceUnavailable() : bool {
85 2
        return $this->_preg_match_headerLine('/^HTTP\/.* 503/i') !== null;
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_preg_match_headerLine('/^HTTP\/.* 503/i') targeting LE_ACME2\Response\Abstra...preg_match_headerLine() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
86
    }
87
88 1
    protected function _isValid() : bool {
89
90 1
        return $this->_preg_match_headerLine('/^HTTP\/.* 201/i') !== null || //Created
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_preg_match_headerLine('/^HTTP\/.* 201/i') targeting LE_ACME2\Response\Abstra...preg_match_headerLine() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91 1
            $this->_preg_match_headerLine('/^HTTP\/.* 200/i') !== null ||
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_preg_match_headerLine('/^HTTP\/.* 200/i') targeting LE_ACME2\Response\Abstra...preg_match_headerLine() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92 1
            $this->_preg_match_headerLine('/^HTTP\/.* 204/i') !== null;
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->_preg_match_headerLine('/^HTTP\/.* 204/i') targeting LE_ACME2\Response\Abstra...preg_match_headerLine() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
93
    }
94
95
    public function getRaw() : RawResponse {
96
        return $this->_raw;
97
    }
98
}