Passed
Push — master ( 1a72b2...62395a )
by Fabian
02:20
created

AbstractResponse::__construct()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.8142

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 27
ccs 10
cts 15
cp 0.6667
rs 8.8333
cc 7
nc 4
nop 1
crap 8.8142
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
    protected $_raw = NULL;
12
13
    protected $_pattern_header_location = '/^Location: (\S+)$/i';
14
15
    /**
16
     * AbstractResponse constructor.
17
     *
18
     * @param RawResponse $raw
19
     * @throws Exception\InvalidResponse
20
     * @throws Exception\RateLimitReached
21
     */
22 2
    public function __construct(RawResponse $raw) {
23
24 2
        $this->_raw = $raw;
25
26 2
        if($this->_isRateLimitReached()) {
27
28 2
            $detail = "";
29 2
            if(isset($raw->body['type']) && $raw->body['type'] == 'urn:ietf:params:acme:error:rateLimited') {
30 2
                $detail = $raw->body['detail'];
31
            }
32
33 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...
34
35 2
            throw new Exception\RateLimitReached(
36 2
                $raw->request,
37
                $detail,
38 2
                $retryAfterMatches !== null ? $retryAfterMatches[1] : null
0 ignored issues
show
introduced by
The condition $retryAfterMatches !== null is always false.
Loading history...
39
            );
40
        }
41
42
        $result = $this->_isValid();
43
        if(!$result) {
44
45
            $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...
46
            throw new Exception\InvalidResponse(
47
                $raw,
48
                isset($responseStatus[1]) ? $responseStatus[1] : null,
49
            );
50
        }
51
    }
52
53
    protected function _preg_match_headerLine(string $pattern) : ?array {
54
55
        foreach($this->_raw->header as $line) {
56
57
            if(preg_match($pattern, $line, $matches) === 1)
58
                return $matches;
59
        }
60
        return null;
61
    }
62
63 2
    protected function _isRateLimitReached() : bool {
64 2
        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...
65
    }
66
67 1
    protected function _isValid() : bool {
68
69 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...
70 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...
71 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...
72
    }
73
74
    public function getRaw() : RawResponse {
75
        return $this->_raw;
76
    }
77
}