Completed
Push — master ( 49e9a0...fa7091 )
by Jan-Petter
02:11
created

Host::add()   D

Complexity

Conditions 9
Paths 19

Size

Total Lines 25
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 25
rs 4.909
cc 9
eloc 16
nc 19
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Modules\Directives;
3
4
use vipnytt\RobotsTxtParser\Modules\UrlTools;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
7
/**
8
 * Class Host
9
 *
10
 * @package vipnytt\RobotsTxtParser\Modules\Directives
11
 */
12
class Host implements DirectiveInterface, RobotsTxtInterface
13
{
14
    use UrlTools;
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 (in_array($host, $this->array)) {
60
            return false;
61
        }
62
        $this->array[] = $host;
63
        return true;
64
    }
65
66
    /**
67
     * Check
68
     *
69
     * @param string $url
70
     * @return bool
71
     */
72
    public function check($url)
73
    {
74
        if (empty($this->array)) {
75
            return false;
76
        }
77
        $url = mb_strtolower($this->urlEncode($url));
78
        $parts = [
79
            'scheme' => parse_url($url, PHP_URL_SCHEME),
80
            'host' => parse_url($url, PHP_URL_HOST),
81
        ];
82
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
83
        $cases = [
84
            $parts['host'],
85
            $parts['host'] . ':' . $parts['port'],
86
            $parts['scheme'] . '://' . $parts['host'],
87
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
88
        ];
89
        if (in_array($this->array[0], $cases)) {
90
            return true;
91
        }
92
        return false;
93
    }
94
95
    /**
96
     * Export
97
     *
98
     * @return array
99
     */
100
    public function export()
101
    {
102
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
103
    }
104
}
105