1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Client\Directives; |
3
|
|
|
|
4
|
|
|
use vipnytt\RobotsTxtParser\Exceptions\ClientException; |
5
|
|
|
use vipnytt\RobotsTxtParser\Parser\Directives\DirectiveParserCommons; |
6
|
|
|
use vipnytt\RobotsTxtParser\Parser\UrlParser; |
7
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
8
|
|
|
|
9
|
|
|
class DisAllowClient implements RobotsTxtInterface |
10
|
|
|
{ |
11
|
|
|
use DirectiveParserCommons; |
12
|
|
|
use UrlParser; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Paths |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $array; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Clean-param |
22
|
|
|
* @var CleanParamClient |
23
|
|
|
*/ |
24
|
|
|
private $cleanParam; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Host |
28
|
|
|
* @var HostClient |
29
|
|
|
*/ |
30
|
|
|
private $host; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* DisAllowClient constructor. |
34
|
|
|
* |
35
|
|
|
* @param array $paths |
36
|
|
|
* @param CleanParamClient $cleanParam |
37
|
|
|
* @param HostClient $host |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $paths, CleanParamClient $cleanParam, HostClient $host) |
40
|
|
|
{ |
41
|
|
|
$this->array = $paths; |
42
|
|
|
$this->cleanParam = $cleanParam; |
43
|
|
|
$this->host = $host; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check |
48
|
|
|
* |
49
|
|
|
* @param string $url |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function affected($url) |
53
|
|
|
{ |
54
|
|
|
$path = $this->getPath($url); |
55
|
|
|
return ( |
56
|
|
|
$this->checkPath($path, isset($this->array['path']) ? $this->array['path'] : []) || |
57
|
|
|
$this->cleanParam->check($path) || |
58
|
|
|
$this->host->check($url) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get path and query |
64
|
|
|
* |
65
|
|
|
* @param string $url |
66
|
|
|
* @return string |
67
|
|
|
* @throws ClientException |
68
|
|
|
*/ |
69
|
|
|
private function getPath($url) |
70
|
|
|
{ |
71
|
|
|
// Encode |
72
|
|
|
$url = $this->urlEncode($url); |
73
|
|
View Code Duplication |
if (mb_stripos($url, '/') === 0) { |
|
|
|
|
74
|
|
|
// Strip fragments |
75
|
|
|
$url = mb_split('#', $url)[0]; |
76
|
|
|
return $url; |
77
|
|
|
} |
78
|
|
|
if (!$this->urlValidate($url)) { |
79
|
|
|
throw new ClientException('Invalid URL'); |
80
|
|
|
} |
81
|
|
|
$path = (($path = parse_url($url, PHP_URL_PATH)) === null) ? '/' : $path; |
82
|
|
|
$query = (($query = parse_url($url, PHP_URL_QUERY)) === null) ? '' : '?' . $query; |
83
|
|
|
return $path . $query; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.