Completed
Branch 2.0-dev (d250b8)
by Jan-Petter
03:02
created

StatusCodeParser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 13
c 4
b 2
f 0
lcom 1
cbo 1
dl 0
loc 105
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B isApplicable() 0 16 5
A replaceUnofficial() 0 8 2
B check() 0 14 5
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
     * Replacement coded
23
     * @var array
24
     */
25
    protected $unofficialCodes = [
26
        522 => 408, // CloudFlare could not negotiate a TCP handshake with the origin server.
27
        523 => 404, // CloudFlare could not reach the origin server; for example, if the DNS records for the baseUrl server are incorrect.
28
        524 => 408, // CloudFlare was able to complete a TCP connection to the origin server, but did not receive a timely HTTP response.
29
    ];
30
    /**
31
     * Status code
32
     * @var int
33
     */
34
    private $code;
35
    /**
36
     * Scheme
37
     * @var string|false
38
     */
39
    private $scheme;
40
    /**
41
     * Applicable
42
     * @var bool
43
     */
44
    private $applicable;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param int|null $code - HTTP status code
50
     * @param string|false $scheme
51
     * @throws StatusCodeException
52
     */
53
    public function __construct($code, $scheme)
54
    {
55
        $this->code = $code;
56
        $this->scheme = $scheme;
57
        $this->applicable = $this->isApplicable();
58
    }
59
60
    /**
61
     * Check if URL is Applicable for Status code parsing
62
     *
63
     * @return bool
64
     * @throws StatusCodeException
65
     */
66
    private function isApplicable()
67
    {
68
        if (
69
            !in_array($this->scheme, self::VALID_SCHEME) ||
70
            $this->code === null
71
        ) {
72
            return false;
73
        }
74
        if (
75
            $this->code < 100 ||
76
            $this->code > 599
77
        ) {
78
            throw new StatusCodeException('Invalid HTTP status code');
79
        }
80
        return true;
81
    }
82
83
    /**
84
     * Replace an unofficial code
85
     *
86
     * @return int|false
87
     */
88
    public function replaceUnofficial()
89
    {
90
        if (in_array($this->code, array_keys($this->unofficialCodes))) {
91
            $this->code = $this->unofficialCodes[$this->code];
92
            return $this->code;
93
        }
94
        return false;
95
    }
96
97
    /**
98
     * Determine the correct group
99
     *
100
     * @return string|null
101
     */
102
    public function check()
103
    {
104
        if (!$this->applicable) {
105
            return null;
106
        }
107
        switch (floor($this->code / 100) * 100) {
108
            case 300:
109
            case 400:
110
                return self::DIRECTIVE_ALLOW;
111
            case 500:
112
                return self::DIRECTIVE_DISALLOW;
113
        }
114
        return null;
115
    }
116
}
117