Completed
Push — master ( 065a8d...52e320 )
by Jan-Petter
02:19
created

HostClient::isListed()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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