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

StatusCodeParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 10
Bugs 3 Features 1
Metric Value
wmc 10
c 10
b 3
f 1
lcom 1
cbo 2
dl 0
loc 64
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A validate() 0 7 2
B accessOverride() 0 13 5
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