Completed
Push — master ( 7c0812...a4a7d4 )
by Jan-Petter
02:10
created

StatusCodeParser::isApplicable()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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