Completed
Branch 2.0-dev (2c252e)
by Jan-Petter
02:43
created

HostClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 9
c 4
b 1
f 0
lcom 1
cbo 1
dl 0
loc 84
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A check() 0 19 3
A get() 0 4 2
A isMainHost() 0 4 2
A export() 0 4 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
use vipnytt\RobotsTxtParser\Parser\UrlParser;
4
5
/**
6
 * Class HostClient
7
 *
8
 * @package vipnytt\RobotsTxtParser\Client\Directives
9
 */
10
class HostClient
11
{
12
    use UrlParser;
13
14
    /**
15
     * Base Uri
16
     * @var string
17
     */
18
    private $base;
19
20
    /**
21
     * Host
22
     * @var array
23
     */
24
    private $host;
25
26
    /**
27
     * HostClient constructor.
28
     *
29
     * @param string $base
30
     * @param array $host
31
     */
32
    public function __construct($base, array $host)
33
    {
34
        $this->base = $base;
35
        $this->host = $host;
36
    }
37
38
    /**
39
     * Check
40
     *
41
     * @param string $url
42
     * @return bool
43
     */
44
    public function check($url)
45
    {
46
        $url = mb_strtolower($this->urlEncode($url));
47
        $parts = [
48
            'scheme' => parse_url($url, PHP_URL_SCHEME),
49
            'host' => parse_url($url, PHP_URL_HOST),
50
        ];
51
        $parts['port'] = is_int($port = parse_url($url, PHP_URL_PORT)) ? $port : getservbyname($parts['scheme'], 'tcp');
52
        $cases = [
53
            $parts['host'],
54
            $parts['host'] . ':' . $parts['port'],
55
            $parts['scheme'] . '://' . $parts['host'],
56
            $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']
57
        ];
58
        if (in_array($this->get(), $cases)) {
59
            return true;
60
        }
61
        return false;
62
    }
63
64
    /**
65
     * Get
66
     *
67
     * @return string|null
68
     */
69
    public function get()
70
    {
71
        return isset($this->host[0]) ? $this->host[0] : null;
72
    }
73
74
    /**
75
     * Preferred host
76
     *
77
     * @return bool
78
     */
79
    public function isMainHost()
80
    {
81
        return empty($this->host) ? true : mb_stripos($this->urlBase($this->urlEncode($this->base)), $this->get()) !== false;
82
    }
83
84
    /**
85
     * Export
86
     *
87
     * @return array
88
     */
89
    public function export()
90
    {
91
        return $this->host;
92
    }
93
}
94