Completed
Push — master ( a7ec5f...7812c9 )
by Jan-Petter
02:03
created

HttpStatusCodeParser::__construct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 6
nc 2
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser;
3
4
use vipnytt\RobotsTxtParser\Exceptions;
5
6
class HttpStatusCodeParser
7
{
8
    /**
9
     * Status code
10
     * @var int
11
     */
12
    protected $code = 200;
13
14
    /**
15
     * Replacement coded
16
     * @var array
17
     */
18
    protected $unofficialCodes = [
19
        522 => 408, // CloudFlare could not negotiate a TCP handshake with the origin server.
20
        523 => 404, // CloudFlare could not reach the origin server; for example, if the DNS records for the origin server are incorrect.
21
        524 => 408, // CloudFlare was able to complete a TCP connection to the origin server, but did not receive a timely HTTP response.
22
    ];
23
24
    /**
25
     * Constructor
26
     *
27
     * @param integer $code - HTTP status code
28
     * @throws Exceptions\HttpStatusCodeException
29
     */
30
    public function __construct($code)
31
    {
32
        if (!is_int($code) ||
33
            $code < 100 ||
34
            $code > 599
35
        ) {
36
            throw new Exceptions\HttpStatusCodeException('Invalid HTTP status code');
37
        }
38
        $this->code = $code;
39
    }
40
41
    /**
42
     * Replace an unofficial code
43
     *
44
     * @return int|false
45
     */
46
    public function replaceUnofficial()
47
    {
48
        if (in_array($this->code, array_keys($this->unofficialCodes))) {
49
            $this->code = $this->unofficialCodes[$this->code];
50
            return $this->code;
51
        }
52
        return false;
53
    }
54
55
    /**
56
     * Determine the correct group
57
     *
58
     * @return string
59
     */
60
    public function isAllowed()
61
    {
62
        switch (floor($this->code / 100) * 100) {
63
            case 500:
64
                return false;
65
        }
66
        return true;
67
    }
68
}
69