Completed
Branch 2.0-dev (131e57)
by Jan-Petter
02:08
created

StatusCodeParser::isApplicable()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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