HostParserCore::getParts()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.6111
c 0
b 0
f 0
cc 5
nc 16
nop 1
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\Directives;
10
11
use vipnytt\RobotsTxtParser\Parser\UriParser;
12
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
13
14
/**
15
 * Class HostParser
16
 *
17
 * @link http://tools.ietf.org/html/rfc952
18
 *
19
 * @package vipnytt\RobotsTxtParser\Parser\Directives
20
 */
21
abstract class HostParserCore implements ParserInterface, RobotsTxtInterface
22
{
23
    /**
24
     * Base uri
25
     * @var string
26
     */
27
    protected $base;
28
29
    /**
30
     * Effective uri
31
     * @var string
32
     */
33
    protected $effective;
34
35
    /**
36
     * Host values
37
     * @var string[]
38
     */
39
    protected $host = [];
40
41
    /**
42
     * HostParser constructor.
43
     *
44
     * @param string $base
45
     * @param string $effective
46
     */
47
    public function __construct($base, $effective)
48
    {
49
        $this->base = $base;
50
        $this->effective = $effective;
51
    }
52
53
    /**
54
     * Add
55
     *
56
     * @param string $line
57
     * @return bool
58
     */
59
    public function add($line)
60
    {
61
        $host = $this->parse($line);
62
        if ($host === false ||
63
            $host !== $line ||
64
            in_array($host, $this->host)
65
        ) {
66
            return false;
67
        }
68
        $this->host[] = $line;
69
        return true;
70
    }
71
72
    /**
73
     * Parse
74
     *
75
     * @param string $line
76
     * @return string|false
77
     */
78
    private function parse($line)
79
    {
80
        $uriParser = new UriParser($line);
81
        $line = $uriParser->encode();
82
        if ($uriParser->validateIP() ||
83
            !$uriParser->validateHost() ||
84
            (
85
                parse_url($line, PHP_URL_SCHEME) !== null &&
86
                !$uriParser->validateScheme()
87
            )
88
        ) {
89
            return false;
90
        }
91
        $parts = $this->getParts($line);
92
        return $parts['scheme'] . $parts['host'] . $parts['port'];
93
    }
94
95
    /**
96
     * Get URI parts
97
     *
98
     * @param string $uri
99
     * @return string[]|false
100
     */
101
    private function getParts($uri)
102
    {
103
        return ($parsed = parse_url($uri)) === false ? false : [
104
            'scheme' => isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '',
105
            'host' => isset($parsed['host']) ? $parsed['host'] : $parsed['path'],
106
            'port' => isset($parsed['port']) ? ':' . $parsed['port'] : '',
107
        ];
108
    }
109
}
110