Completed
Push — master ( 0489e6...065a8d )
by Jan-Petter
04:09
created

AllowClient   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 90
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isListed() 0 9 3
B getPath() 0 15 5
A export() 0 8 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
4
use vipnytt\RobotsTxtParser\Exceptions\ClientException;
5
use vipnytt\RobotsTxtParser\Parser\UriParser;
6
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
7
8
/**
9
 * Class AllowClient
10
 *
11
 * @package vipnytt\RobotsTxtParser\Client\Directives
12
 */
13
class AllowClient implements ClientInterface, RobotsTxtInterface
14
{
15
    use DirectiveClientCommons;
16
    use UriParser;
17
18
    /**
19
     * Paths
20
     * @var array
21
     */
22
    private $paths;
23
24
    /**
25
     * Host
26
     * @var HostClient
27
     */
28
    private $host;
29
30
    /**
31
     * Clean-param
32
     * @var CleanParamClient
33
     */
34
    private $cleanParam;
35
36
    /**
37
     * AllowClient constructor.
38
     *
39
     * @param array $paths
40
     * @param HostClient $host
41
     * @param CleanParamClient $cleanParam
42
     */
43
    public function __construct(array $paths, HostClient $host, CleanParamClient $cleanParam)
44
    {
45
        $this->host = $host;
46
        $this->paths = $paths;
47
        $this->cleanParam = $cleanParam;
48
    }
49
50
    /**
51
     * Check
52
     *
53
     * @param  string $url
54
     * @return bool
55
     */
56
    public function isListed($url)
57
    {
58
        $path = $this->getPath($url);
59
        return (
60
            $this->checkPaths($path, $this->paths) ||
61
            $this->host->isListed($url) ||
62
            $this->cleanParam->isListed($path)
63
        );
64
    }
65
66
    /**
67
     * Get path and query
68
     *
69
     * @param string $url
70
     * @return string
71
     * @throws ClientException
72
     */
73
    private function getPath($url)
74
    {
75
        // Encode
76
        $url = mb_split('#', $this->urlEncode($url))[0];
77
        if (mb_stripos($url, '/') === 0) {
78
            // URL is already an path
79
            return $url;
80
        }
81
        if (!$this->urlValidate($url)) {
82
            throw new ClientException('Invalid URL');
83
        }
84
        $path = (($path = parse_url($url, PHP_URL_PATH)) === null) ? '/' : $path;
85
        $query = (($query = parse_url($url, PHP_URL_QUERY)) === null) ? '' : '?' . $query;
86
        return $path . $query;
87
    }
88
89
    /**
90
     * Export
91
     *
92
     * @return array
93
     */
94
    public function export()
95
    {
96
        return [
97
            'host' => $this->host->export(),
98
            'path' => $this->paths,
99
            'clean-param' => $this->cleanParam->export(),
100
        ];
101
    }
102
}
103