Completed
Push — master ( 705095...78cb27 )
by Jan-Petter
04:57
created

Host::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
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\UrlParser;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
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 array
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
100
     *
101
     * @return array
102
     */
103
    public function export()
104
    {
105
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
106
    }
107
}
108