Test Failed
Push — master ( 5093c2...558a62 )
by Fabian
02:30
created

AbstractResponse   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 41.38%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 15
eloc 29
c 5
b 0
f 0
dl 0
loc 70
rs 10
ccs 12
cts 29
cp 0.4138

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _isRateLimitReached() 0 2 1
A _preg_match_headerLine() 0 10 3
A getRaw() 0 2 1
B __construct() 0 27 7
A _isValid() 0 5 3
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
     */
23 2
    public function __construct(RawResponse $raw) {
24
25 2
        $this->_raw = $raw;
26
27 2
        if($this->_isRateLimitReached()) {
28
29 2
            $detail = "";
30 2
            if(isset($raw->body['type']) && $raw->body['type'] == 'urn:ietf:params:acme:error:rateLimited') {
31 2
                $detail = $raw->body['detail'];
32
            }
33
34 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...
35
36 2
            throw new Exception\RateLimitReached(
37 2
                $raw->request,
38
                $detail,
39 2
                $retryAfterMatches !== null ? $retryAfterMatches[1] : null
0 ignored issues
show
introduced by
The condition $retryAfterMatches !== null is always false.
Loading history...
40
            );
41
        }
42
43
        $result = $this->_isValid();
44
        if(!$result) {
45
46
            $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...
47
            throw new Exception\InvalidResponse(
48
                $raw,
49
                isset($responseStatus[1]) ? $responseStatus[1] : null,
50
            );
51
        }
52
    }
53
54
    protected function _preg_match_headerLine(string $pattern) : ?array {
55
56
        foreach($this->_raw->header as $line) {
57
58
            $matches = [];
59
            if(preg_match($pattern, $line, $matches) === 1) {
60
                return $matches;
61
            }
62
        }
63
        return null;
64
    }
65
66 2
    protected function _isRateLimitReached() : bool {
67 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...
68
    }
69
70
    protected function _isValid() : bool {
71
72
        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...
73
            $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...
74
            $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...
75
    }
76
77
    public function getRaw() : RawResponse {
78
        return $this->_raw;
79
    }
80
}