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

HostClient::export()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\UrlParser;
5
6
/**
7
 * Class HostClient
8
 *
9
 * @package vipnytt\RobotsTxtParser\Client\Directives
10
 */
11
class HostClient implements ClientInterface
12
{
13
    use UrlParser;
14
15
    /**
16
     * Base Uri
17
     * @var string
18
     */
19
    private $base;
20
21
    /**
22
     * Host
23
     * @var string[]
24
     */
25
    private $host;
26
27
    /**
28
     * Parent directive
29
     * @var string|null
30
     */
31
    private $parent;
32
33
    /**
34
     * HostClient constructor.
35
     *
36
     * @param string $base
37
     * @param string[] $host
38
     * @param string|null $parentDirective
39
     */
40
    public function __construct($base, array $host, $parentDirective = null)
41
    {
42
        $this->base = $base;
43
        $this->host = $host;
44
        $this->parent = $parentDirective;
45
    }
46
47
    /**
48
     * Preferred host
49
     *
50
     * @return bool
51
     */
52
    public function isPreferred()
53
    {
54
        if (($host = $this->get()) === null) {
55
            return true;
56
        }
57
        $parsed = parse_url($host);
58
        $new = [
59
            'scheme' => isset($parsed['scheme']) ? $parsed['scheme'] : parse_url($this->base, PHP_URL_SCHEME),
60
            'host' => isset($parsed['host']) ? $parsed['host'] : $parsed['path'],
61
        ];
62
        $new['port'] = isset($parsed['port']) ? $parsed['port'] : getservbyname($new['scheme'], 'tcp');
63
        return $this->base == $this->urlBase($new['scheme'] . '://' . $new['host'] . ':' . $new['port']);
64
    }
65
66
    /**
67
     * Get
68
     *
69
     * @return string|null
70
     */
71
    public function get()
72
    {
73
        return isset($this->host[0]) ? $this->host[0] : null;
74
    }
75
76
    /**
77
     * Preferred host
78
     *
79
     * @param string $url
80
     * @return bool
81
     */
82
    public function isListed($url)
83
    {
84
        $url = mb_strtolower($this->urlEncode($url));
85
        $parts = [
86
            'scheme' => parse_url($url, PHP_URL_SCHEME),
87
            'host' => parse_url($url, PHP_URL_HOST),
88
        ];
89
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
90
        $cases = [
91
            $parts['host'],
92
            $parts['host'] . ':' . $parts['port'],
93
            $parts['scheme'] . '://' . $parts['host'],
94
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
95
        ];
96
        foreach ($this->host as $host) {
97
            if (in_array($host, $cases)) {
98
                return true;
99
            }
100
        }
101
        return false;
102
    }
103
104
    /**
105
     * Export
106
     *
107
     * @return string[]|string|null
108
     */
109
    public function export()
110
    {
111
        if ($this->parent === null) {
112
            return isset($this->host[0]) ? $this->host[0] : null;
113
        }
114
        return $this->host;
115
    }
116
}
117