Completed
Push — master ( b490b9...ed4a79 )
by Jan-Petter
02:49
created

AllowClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hasPath() 0 4 1
B getPathFromUri() 0 17 5
A export() 0 4 1
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
use vipnytt\RobotsTxtParser\Parser\UriParser;
12
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
13
14
/**
15
 * Class AllowClient
16
 *
17
 * @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/AllowClient.md for documentation
18
 * @package vipnytt\RobotsTxtParser\Client\Directives
19
 */
20
class AllowClient implements ClientInterface, RobotsTxtInterface
21
{
22
    use DirectiveClientTrait;
23
24
    /**
25
     * Paths
26
     * @var array
27
     */
28
    private $paths;
29
30
    /**
31
     * AllowClient constructor.
32
     *
33
     * @param string[] $paths
34
     */
35
    public function __construct(array $paths)
36
    {
37
        $this->paths = $paths;
38
    }
39
40
    /**
41
     * Check
42
     *
43
     * @param  string $uri
44
     * @return int|false
45
     */
46
    public function hasPath($uri)
47
    {
48
        return $this->checkPaths($this->getPathFromUri($uri), $this->paths);
49
    }
50
51
    /**
52
     * Get path and query
53
     *
54
     * @param string $uri
55
     * @return string
56
     * @throws \InvalidArgumentException
57
     */
58
    private function getPathFromUri($uri)
59
    {
60
        $uriParser = new UriParser($uri);
61
        // Prepare uri
62
        $uriParser->encode();
63
        $uri = $uriParser->stripFragment();
64
        if (strpos($uri, '/') === 0) {
65
            // URI is already an path
66
            return $uri;
67
        }
68
        if (!$uriParser->validate()) {
69
            throw new \InvalidArgumentException('Invalid URI');
70
        }
71
        $path = (($path = parse_url($uri, PHP_URL_PATH)) === null) ? '/' : $path;
72
        $query = (($query = parse_url($uri, PHP_URL_QUERY)) === null) ? '' : '?' . $query;
73
        return $path . $query;
74
    }
75
76
    /**
77
     * Export
78
     *
79
     * @return array
80
     */
81
    public function export()
82
    {
83
        return $this->paths;
84
    }
85
}
86