Completed
Branch 2.0-dev (2c252e)
by Jan-Petter
02:43
created

HostParser::check()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 16
nc 5
nop 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A HostParser::client() 0 4 1
A HostParser::getRules() 0 4 2
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
     * Directive
19
     */
20
    const DIRECTIVE = self::DIRECTIVE_HOST;
21
22
    /**
23
     * Base Uri
24
     * @var string
25
     */
26
    private $base;
27
28
    /**
29
     * Host array
30
     * @var string[]
31
     */
32
    private $array = [];
33
34
    /**
35
     * Host constructor.
36
     *
37
     * @param string $base
38
     */
39
    public function __construct($base)
40
    {
41
        $this->base = $base;
42
    }
43
44
    /**
45
     * Add
46
     *
47
     * @param string $line
48
     * @return bool
49
     */
50
    public function add($line)
51
    {
52
        $host = $this->parse($line);
53
        if (
54
            $host === false ||
55
            $line !== $host ||
56
            in_array($host, $this->array)
57
        ) {
58
            return false;
59
        }
60
        $this->array[] = $line;
61
        return true;
62
    }
63
64
    /**
65
     * Client
66
     *
67
     * @param string $line
68
     * @return string|false
69
     */
70
    private function parse($line)
71
    {
72
        if (($parsed = parse_url(($line = $this->urlEncode(mb_strtolower($line))))) === false) {
73
            return false;
74
        }
75
        $line = isset($parsed['host']) ? $parsed['host'] : $parsed['path'];
76
        if (
77
            !$this->urlValidateHost($line) ||
78
            (
79
                isset($parsed['scheme']) &&
80
                !$this->urlValidateScheme($parsed['scheme'])
81
            )
82
        ) {
83
            return false;
84
        }
85
        $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '';
86
        $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
87
        return $scheme . $line . $port;
88
    }
89
90
    /**
91
     * Client
92
     *
93
     * @return HostClient
94
     */
95
    public function client()
96
    {
97
        return new HostClient($this->base, $this->array);
98
    }
99
100
    /**
101
     * Rule array
102
     *
103
     * @return string[][]
104
     */
105
    public function getRules()
106
    {
107
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
108
    }
109
110
    /**
111
     * Render
112
     *
113
     * @return string[]
114
     */
115 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...
116
    {
117
        $result = [];
118
        foreach ($this->array as $value) {
119
            $result[] = self::DIRECTIVE . ':' . $value;
120
        }
121
        return $result;
122
    }
123
}
124