Passed
Branch master (fc5382)
by Fabian
03:13
created

AbstractResponse::__construct()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 21
ccs 0
cts 13
cp 0
rs 9.2222
c 2
b 0
f 0
cc 6
nc 4
nop 1
crap 42
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
    public function __construct(RawResponse $raw) {
23
24
        $this->_raw = $raw;
25
26
        if($this->_isRateLimitReached()) {
27
28
            $detail = "";
29
            if(isset($raw->body['type']) && $raw->body['type'] == 'urn:ietf:params:acme:error:rateLimited') {
30
                $detail = $raw->body['detail'];
31
            }
32
33
            throw new Exception\RateLimitReached($raw->request, $detail);
34
        }
35
36
        $result = $this->_isValid();
37
        if(!$result) {
38
39
            $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...
40
            throw new Exception\InvalidResponse(
41
                $raw,
42
                isset($responseStatus[1]) ? $responseStatus[1] : null,
43
            );
44
        }
45
    }
46
47
    protected function _preg_match_headerLine(string $pattern) : ?array {
48
49
        foreach($this->_raw->header as $line) {
50
51
            if(preg_match($pattern, $line, $matches) === 1)
52
                return $matches;
53
        }
54
        return null;
55
    }
56
57
    protected function _isRateLimitReached() : bool {
58
        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...
59
    }
60
61
    protected function _isValid() : bool {
62
63
        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...
64
            $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...
65
            $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...
66
    }
67
68
    public function getRaw() : RawResponse {
69
        return $this->_raw;
70
    }
71
}