HostClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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