Completed
Push — master ( 5595ec...d8eb26 )
by Jan-Petter
02:13
created

StatusCodeParser::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser;
3
4
use vipnytt\RobotsTxtParser\Parser\Directives\DirectiveParserCommons;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
7
/**
8
 * Class StatusCodeParser
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser
11
 */
12
class StatusCodeParser implements RobotsTxtInterface
13
{
14
    use DirectiveParserCommons;
15
16
    /**
17
     * Status code
18
     * @var int
19
     */
20
    private $code;
21
22
    /**
23
     * Scheme
24
     * @var string|false
25
     */
26
    private $scheme;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param int|null $code - HTTP status code
32
     * @param string|false $scheme
33
     */
34
    public function __construct($code, $scheme)
35
    {
36
        $this->code = $code === null ? 200 : $code;
37
        $this->scheme = $scheme;
38
    }
39
40
    /**
41
     * Validate
42
     *
43
     * @return bool
44
     */
45
    public function isValid()
46
    {
47
        return (
48
            $this->code >= 100 &&
49
            $this->code <= 599
50
        );
51
    }
52
53
    /**
54
     * Check if the code overrides the robots.txt file
55
     *
56
     * @return string|false
57
     */
58
    public function accessOverride()
59
    {
60
        if (stripos($this->scheme, 'http') === 0) {
61
            switch (floor($this->code / 100) * 100) {
62
                case 300:
63
                case 400:
64
                    return self::DIRECTIVE_ALLOW;
65
                case 500:
66
                    return self::DIRECTIVE_DISALLOW;
67
            }
68
        }
69
        return false;
70
    }
71
}
72