Completed
Branch 2.0-dev (d250b8)
by Jan-Petter
03:02
created

HostParser::parse()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 7.7777
cc 8
eloc 12
nc 11
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\UrlParser;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
7
/**
8
 * Class HostParser
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser\Directives
11
 */
12
class HostParser implements ParserInterface, RobotsTxtInterface
13
{
14
    use UrlParser;
15
16
    /**
17
     * Directive
18
     */
19
    const DIRECTIVE = self::DIRECTIVE_HOST;
20
21
    /**
22
     * Host array
23
     * @var string[]
24
     */
25
    private $array = [];
26
27
    /**
28
     * Host constructor.
29
     */
30
    public function __construct()
31
    {
32
    }
33
34
    /**
35
     * Add
36
     *
37
     * @param string $line
38
     * @return bool
39
     */
40
    public function add($line)
41
    {
42
        $host = $this->parse($line);
43
        if (
44
            $host === false ||
45
            $line !== $host ||
46
            in_array($host, $this->array)
47
        ) {
48
            return false;
49
        }
50
        $this->array[] = $line;
51
        return true;
52
    }
53
54
    /**
55
     * Client
56
     *
57
     * @param string $line
58
     * @return string|false
59
     */
60
    private function parse($line)
61
    {
62
        if (($parsed = parse_url(($line = $this->urlEncode(mb_strtolower($line))))) === false) {
63
            return false;
64
        }
65
        $line = isset($parsed['host']) ? $parsed['host'] : $parsed['path'];
66
        if (
67
            !$this->urlValidateHost($line) ||
68
            (
69
                isset($parsed['scheme']) &&
70
                !$this->urlValidateScheme($parsed['scheme'])
71
            )
72
        ) {
73
            return false;
74
        }
75
        $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '';
76
        $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
77
        return $scheme . $line . $port;
78
    }
79
80
    /**
81
     * Check
82
     *
83
     * @param string $url
84
     * @return bool
85
     */
86
    public function check($url)
87
    {
88
        if (empty($this->array)) {
89
            return false;
90
        }
91
        $url = mb_strtolower($this->urlEncode($url));
92
        $parts = [
93
            'scheme' => parse_url($url, PHP_URL_SCHEME),
94
            'host' => parse_url($url, PHP_URL_HOST),
95
        ];
96
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
97
        $cases = [
98
            $parts['host'],
99
            $parts['host'] . ':' . $parts['port'],
100
            $parts['scheme'] . '://' . $parts['host'],
101
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
102
        ];
103
        if (in_array($this->array[0], $cases)) {
104
            return true;
105
        }
106
        return false;
107
    }
108
109
    /**
110
     * Export rules
111
     *
112
     * @return string[][]
113
     */
114
    public function export()
115
    {
116
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
117
    }
118
119
    /**
120
     * Render
121
     *
122
     * @return string[]
123
     */
124 View Code Duplication
    public function render()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        $result = [];
127
        foreach ($this->array as $value) {
128
            $result[] = self::DIRECTIVE . ':' . $value;
129
        }
130
        return $result;
131
    }
132
}
133