Completed
Push — master ( dc3dad...b5f967 )
by Jan-Petter
01:56
created

StatusCodeParser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 100
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isApplicable() 0 13 4
A replaceUnofficial() 0 8 2
A check() 0 13 4
1
<?php
2
namespace vipnytt\RobotsTxtParser;
3
4
use vipnytt\RobotsTxtParser\Exceptions\StatusCodeException;
5
6
class StatusCodeParser implements RobotsTxtInterface
7
{
8
    /**
9
     * Valid schemes
10
     */
11
    const VALID_SCHEME = [
12
        'http',
13
        'https',
14
    ];
15
16
    /**
17
     * Status code
18
     * @var int
19
     */
20
    protected $code = 200;
21
22
    /**
23
     * Scheme
24
     * @var string
25
     */
26
    protected $scheme;
27
28
    /**
29
     * Applicable
30
     * @var bool
31
     */
32
    protected $applicable;
33
34
    /**
35
     * Replacement coded
36
     * @var array
37
     */
38
    protected $unofficialCodes = [
39
        522 => 408, // CloudFlare could not negotiate a TCP handshake with the origin server.
40
        523 => 404, // CloudFlare could not reach the origin server; for example, if the DNS records for the origin server are incorrect.
41
        524 => 408, // CloudFlare was able to complete a TCP connection to the origin server, but did not receive a timely HTTP response.
42
    ];
43
44
    /**
45
     * Constructor
46
     *
47
     * @param integer|null $code - HTTP status code
48
     * @param string|null $scheme
49
     * @throws StatusCodeException
50
     */
51
    public function __construct($code, $scheme)
52
    {
53
        $this->code = $code;
54
        $this->scheme = $scheme;
55
        $this->applicable = $this->isApplicable();
56
    }
57
58
    protected function isApplicable()
59
    {
60
        if (!in_array($this->scheme, self::VALID_SCHEME)) {
61
            return false;
62
        }
63
        if (
64
            $this->code < 100 ||
65
            $this->code > 599
66
        ) {
67
            throw new StatusCodeException('Invalid HTTP(S) status code');
68
        }
69
        return true;
70
    }
71
72
    /**
73
     * Replace an unofficial code
74
     *
75
     * @return int|false
76
     */
77
    public function replaceUnofficial()
78
    {
79
        if (in_array($this->code, array_keys($this->unofficialCodes))) {
80
            $this->code = $this->unofficialCodes[$this->code];
81
            return $this->code;
82
        }
83
        return false;
84
    }
85
86
    /**
87
     * Determine the correct group
88
     *
89
     * @return string|null
90
     * @throws Exceptions\ClientException
91
     */
92
    public function check()
93
    {
94
        if (!$this->applicable) {
95
            return null;
96
        }
97
        switch (floor($this->code / 100) * 100) {
98
            case 400:
99
                return self::DIRECTIVE_ALLOW;
100
            case 500:
101
                return self::DIRECTIVE_DISALLOW;
102
        }
103
        return null;
104
    }
105
}
106