StatusCodeParser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A isValid() 0 7 2
A accessOverride() 0 13 5
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Parser;
10
11
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
12
13
/**
14
 * Class StatusCodeParser
15
 *
16
 * @package vipnytt\RobotsTxtParser\Parser
17
 */
18
class StatusCodeParser implements RobotsTxtInterface
19
{
20
    /**
21
     * Status code
22
     * @var int
23
     */
24
    private $code;
25
26
    /**
27
     * Scheme
28
     * @var string|false
29
     */
30
    private $scheme;
31
32
    /**
33
     * Constructor
34
     *
35
     * @param int|null $code - HTTP status code
36
     * @param string|false $scheme
37
     */
38
    public function __construct($code, $scheme)
39
    {
40
        $this->code = $code === null ? 200 : $code;
41
        $this->scheme = $scheme;
42
    }
43
44
    /**
45
     * Validate
46
     *
47
     * @return bool
48
     */
49
    public function isValid()
50
    {
51
        return (
52
            $this->code >= 100 &&
53
            $this->code <= 599
54
        );
55
    }
56
57
    /**
58
     * Check if the code overrides the robots.txt file
59
     *
60
     * @link https://developers.google.com/webmasters/control-crawl-index/docs/robots_txt#handling-http-result-codes
61
     * @link https://yandex.com/support/webmaster/controlling-robot/robots-txt.xml#additional-info
62
     *
63
     * @return string|false
64
     */
65
    public function accessOverride()
66
    {
67
        if (strpos($this->scheme, 'http') === 0) {
68
            switch (floor($this->code / 100) * 100) {
69
                case 300:
70
                case 400:
71
                    return self::DIRECTIVE_ALLOW;
72
                case 500:
73
                    return self::DIRECTIVE_DISALLOW;
74
            }
75
        }
76
        return false;
77
    }
78
}
79