Completed
Push — master ( e69112...9db678 )
by Jan-Petter
04:16
created

HostParser::add()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 17
rs 8.8571
cc 6
eloc 11
nc 2
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Client\Directives\HostClient;
5
use vipnytt\RobotsTxtParser\Parser\UrlParser;
6
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
7
8
/**
9
 * Class HostParser
10
 *
11
 * @package vipnytt\RobotsTxtParser\Parser\Directives
12
 */
13
class HostParser implements ParserInterface, RobotsTxtInterface
14
{
15
    use UrlParser;
16
17
    /**
18
     * Base Uri
19
     * @var string
20
     */
21
    private $base;
22
23
    /**
24
     * Parent directive
25
     * @var string|null
26
     */
27
    private $parent;
28
29
    /**
30
     * Host array
31
     * @var string[]
32
     */
33
    private $host = [];
34
35
    /**
36
     * Host constructor.
37
     *
38
     * @param string $base
39
     * @param string|null $parentDirective
40
     */
41
    public function __construct($base, $parentDirective = null)
42
    {
43
        $this->base = $base;
44
        $this->parent = $parentDirective;
45
    }
46
47
    /**
48
     * Add
49
     *
50
     * @param string $line
51
     * @return bool
52
     */
53
    public function add($line)
54
    {
55
        $host = $this->parse($line);
56
        if (
57
            $host === false ||
58
            $line !== $host ||
59
            in_array($host, $this->host) ||
60
            (
61
                $this->parent === null &&
62
                !empty($this->host)
63
            )
64
        ) {
65
            return false;
66
        }
67
        $this->host[] = $line;
68
        return true;
69
    }
70
71
    /**
72
     * Client
73
     *
74
     * @param string $line
75
     * @return string|false
76
     */
77
    private function parse($line)
78
    {
79
        if (
80
            !($parts = $this->getParts($line)) ||
81
            !$this->urlValidateHost($parts['host']) ||
82
            (
83
                !empty($parts['scheme']) &&
84
                !$this->urlValidateScheme($parts['scheme'])
85
            )
86
        ) {
87
            return false;
88
        }
89
        return $parts['scheme'] . $parts['host'] . $parts['port'];
90
    }
91
92
    /**
93
     * Get URL parts
94
     *
95
     * @param string $url
96
     * @return string[]|false
97
     */
98
    private function getParts($url)
99
    {
100
        return ($parsed = parse_url($this->urlEncode(mb_strtolower($url)))) === false ? false : [
101
            'scheme' => isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '',
102
            'host' => isset($parsed['host']) ? $parsed['host'] : $parsed['path'],
103
            'port' => isset($parsed['port']) ? ':' . $parsed['port'] : '',
104
        ];
105
    }
106
107
    /**
108
     * Render
109
     *
110
     * @return string[]
111
     */
112 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...
113
    {
114
        $result = [];
115
        foreach ($this->host as $host) {
116
            $result[] = self::DIRECTIVE_HOST . ':' . $host;
117
        }
118
        sort($result);
119
        return $result;
120
    }
121
122
    /**
123
     * Client
124
     *
125
     * @return HostClient
126
     */
127
    public function client()
128
    {
129
        return new HostClient($this->base, $this->host, $this->parent);
130
    }
131
}
132