Completed
Push — master ( a6d8dc...680f8e )
by Jan-Petter
03:57
created

Host::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 8
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\RobotsTxtInterface;
5
use vipnytt\RobotsTxtParser\Parser\UrlParser;
6
7
/**
8
 * Class Host
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser\Directives
11
 */
12
class Host implements DirectiveInterface, 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
    protected $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
        if (($parsed = parse_url(($line = $this->urlEncode(mb_strtolower($line))))) === false) {
43
            return false;
44
        }
45
        $line = isset($parsed['host']) ? $parsed['host'] : $parsed['path'];
46
        if (
47
            !$this->urlValidateHost($line) ||
48
            (
49
                isset($parsed['scheme']) &&
50
                !$this->urlValidateScheme($parsed['scheme'])
51
            )
52
        ) {
53
            return false;
54
        }
55
        $scheme = isset($parsed['scheme']) ? $parsed['scheme'] . '://' : '';
56
        $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
57
58
        $host = $scheme . $line . $port;
59
        if (
60
            $line !== $host ||
61
            in_array($host, $this->array)
62
        ) {
63
            return false;
64
        }
65
        $this->array[] = $line;
66
        return true;
67
    }
68
69
    /**
70
     * Check
71
     *
72
     * @param string $url
73
     * @return bool
74
     */
75
    public function check($url)
76
    {
77
        if (empty($this->array)) {
78
            return false;
79
        }
80
        $url = mb_strtolower($this->urlEncode($url));
81
        $parts = [
82
            'scheme' => parse_url($url, PHP_URL_SCHEME),
83
            'host' => parse_url($url, PHP_URL_HOST),
84
        ];
85
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
86
        $cases = [
87
            $parts['host'],
88
            $parts['host'] . ':' . $parts['port'],
89
            $parts['scheme'] . '://' . $parts['host'],
90
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
91
        ];
92
        if (in_array($this->array[0], $cases)) {
93
            return true;
94
        }
95
        return false;
96
    }
97
98
    /**
99
     * Export rules
100
     *
101
     * @return string[][]
102
     */
103
    public function export()
104
    {
105
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
106
    }
107
108
    /**
109
     * Render
110
     *
111
     * @return string[]
112
     */
113 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...
114
    {
115
        $result = [];
116
        foreach ($this->array as $value) {
117
            $result[] = self::DIRECTIVE . ': ' . $value;
118
        }
119
        return $result;
120
    }
121
}
122