Completed
Push — master ( 23ae93...9ef007 )
by Jan-Petter
02:52
created

StatusCodeParser::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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