Completed
Push — master ( 706ec6...902e66 )
by Jan-Petter
04:48
created

HostClient::getWithUriFallback()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 8
nc 4
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
4
/**
5
 * Class HostClient
6
 *
7
 * @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/HostClient.md for documentation
8
 * @package vipnytt\RobotsTxtParser\Client\Directives
9
 */
10
class HostClient implements ClientInterface
11
{
12
    /**
13
     * Base uri
14
     * @var string
15
     */
16
    private $base;
17
18
    /**
19
     * Effective uri
20
     * @var string
21
     */
22
    private $effective;
23
24
    /**
25
     * Host values
26
     * @var string[]
27
     */
28
    private $host;
29
30
    /**
31
     * Parent directive
32
     * @var string|null
33
     */
34
    private $parent;
35
36
    /**
37
     * HostClient constructor.
38
     *
39
     * @param string $base
40
     * @param string $effective
41
     * @param string[] $host
42
     * @param string|null $parentDirective
43
     */
44
    public function __construct($base, $effective, array $host, $parentDirective = null)
45
    {
46
        $this->base = $base;
47
        $this->effective = $effective;
48
        $this->host = $host;
49
        $this->parent = $parentDirective;
50
    }
51
52
    /**
53
     * Is preferred host
54
     *
55
     * @return bool
56
     */
57
    public function isPreferred()
58
    {
59
        if (($host = $this->get()) === null) {
60
            return $this->base === $this->effective;
61
        }
62
        $parsed = parse_url($host);
63
        $new = [
64
            'scheme' => isset($parsed['scheme']) ? $parsed['scheme'] : parse_url($this->base, PHP_URL_SCHEME),
65
            'host' => isset($parsed['host']) ? $parsed['host'] : $parsed['path'],
66
        ];
67
        $new['port'] = isset($parsed['port']) ? $parsed['port'] : getservbyname($new['scheme'], 'tcp');
68
        return $this->base == $new['scheme'] . '://' . $new['host'] . ':' . $new['port'];
69
    }
70
71
    /**
72
     * Get Host
73
     *
74
     * @return string|null
75
     */
76
    public function get()
77
    {
78
        return isset($this->host[0]) ? $this->host[0] : null;
79
    }
80
81
    /**
82
     * Get Host, falls back to Effective Request URI if not found
83
     *
84
     * @return string
85
     */
86
    public function getWithUriFallback()
87
    {
88
        if (($get = $this->get()) !== null) {
89
            // Host defined by the Host directive
90
            return $get;
91
        } elseif (
92
            $this->base !== $this->effective &&
93
            parse_url($this->base, PHP_URL_HOST) === ($host = parse_url($this->effective, PHP_URL_HOST))
94
        ) {
95
            // Host is the same, but Scheme or Port is different
96
            return getservbyname($scheme = parse_url($this->effective, PHP_URL_SCHEME), 'tcp') === parse_url($this->effective, PHP_URL_PORT) ? $scheme . '://' . $host : $this->effective;
97
        }
98
        // Return Host name only
99
        return parse_url($this->effective, PHP_URL_HOST);
100
    }
101
102
    /**
103
     * Export
104
     *
105
     * @return string[]|string|null
106
     */
107
    public function export()
108
    {
109
        if ($this->parent === null) {
110
            return isset($this->host[0]) ? $this->host[0] : null;
111
        }
112
        return $this->host;
113
    }
114
}
115