Completed
Push — master ( 1b6743...e69112 )
by Jan-Petter
9s
created

UserAgentTools   B

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 1
cbo 16
dl 0
loc 133
rs 8.4614

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __destruct() 0 4 1
A isAllowed() 0 4 1
A check() 0 16 4
A checkPath() 0 15 3
A isDisallowed() 0 4 1
A export() 0 13 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Client\Directives;
3
4
use vipnytt\RobotsTxtParser\Exceptions\ClientException;
5
use vipnytt\RobotsTxtParser\Parser\Directives\SubDirectiveHandler;
6
use vipnytt\RobotsTxtParser\Parser\StatusCodeParser;
7
use vipnytt\RobotsTxtParser\Parser\UrlParser;
8
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
9
10
/**
11
 * Class UserAgentTools
12
 *
13
 * @package vipnytt\RobotsTxtParser\Client\Directives
14
 */
15
class UserAgentTools implements RobotsTxtInterface
16
{
17
    use UrlParser;
18
19
    /**
20
     * Rules
21
     * @var SubDirectiveHandler
22
     */
23
    protected $handler;
24
25
    /**
26
     * Base Uri
27
     * @var string
28
     */
29
    private $base;
30
31
    /**
32
     * Status code
33
     * @var int|null
34
     */
35
    private $statusCode;
36
37
    /**
38
     * DisAllowClient constructor.
39
     *
40
     * @param string $base
41
     * @param int|null $statusCode
42
     * @param SubDirectiveHandler $handler
43
     */
44
    public function __construct(SubDirectiveHandler $handler, $base, $statusCode)
45
    {
46
        $this->handler = $handler;
47
        $this->base = $base;
48
        $this->statusCode = $statusCode;
49
    }
50
51
    /**
52
     * UserAgentClient destructor.
53
     */
54
    public function __destruct()
55
    {
56
        $this->handler->comment()->client();
57
    }
58
59
    /**
60
     * Check if URL is allowed to crawl
61
     *
62
     * @param string $url
63
     * @return bool
64
     */
65
    public function isAllowed($url)
66
    {
67
        return $this->check(self::DIRECTIVE_ALLOW, $url);
68
    }
69
70
    /**
71
     * Check
72
     *
73
     * @param string $directive
74
     * @param string $url
75
     * @return bool
76
     * @throws ClientException
77
     */
78
    private function check($directive, $url)
79
    {
80
        $url = $this->urlConvertToFull($url, $this->base);
81
        if ($this->base !== $this->urlBase($url)) {
82
            throw new ClientException('URL belongs to a different robots.txt');
83
        }
84
        $statusCodeParser = new StatusCodeParser($this->statusCode, parse_url($this->base, PHP_URL_SCHEME));
85
        $statusCodeParser->codeOverride();
86
        if (($result = $statusCodeParser->accessOverrideCheck()) !== null) {
87
            return $directive === $result;
88
        }
89
        if ($this->handler->visitTime()->client()->isVisitTime() === false) {
90
            return $result === self::DIRECTIVE_DISALLOW;
91
        }
92
        return $this->checkPath($directive, $url);
93
    }
94
95
    /**
96
     * Check path
97
     *
98
     * @param string $directive
99
     * @param string $url
100
     * @return bool
101
     */
102
    private function checkPath($directive, $url)
103
    {
104
        $result = self::DIRECTIVE_ALLOW;
105
        foreach (
106
            [
107
                self::DIRECTIVE_DISALLOW => $this->handler->disallow(),
108
                self::DIRECTIVE_ALLOW => $this->handler->allow(),
109
            ] as $currentDirective => $ruleClient
110
        ) {
111
            if ($ruleClient->client()->isListed($url)) {
112
                $result = $currentDirective;
113
            }
114
        }
115
        return $directive === $result;
116
    }
117
118
    /**
119
     * Check if URL is disallowed to crawl
120
     *
121
     * @param string $url
122
     * @return bool
123
     */
124
    public function isDisallowed($url)
125
    {
126
        return $this->check(self::DIRECTIVE_DISALLOW, $url);
127
    }
128
129
    /**
130
     * Rule export
131
     *
132
     * @return array
133
     */
134
    public function export()
135
    {
136
        return [
137
            self::DIRECTIVE_ROBOT_VERSION => $this->handler->robotVersion()->client()->export(),
138
            self::DIRECTIVE_VISIT_TIME => $this->handler->visitTime()->client()->export(),
139
            self::DIRECTIVE_DISALLOW => $this->handler->disallow()->client()->export(),
140
            self::DIRECTIVE_ALLOW => $this->handler->allow()->client()->export(),
141
            self::DIRECTIVE_CRAWL_DELAY => $this->handler->crawlDelay()->client()->export(),
142
            self::DIRECTIVE_CACHE_DELAY => $this->handler->cacheDelay()->client()->export(),
143
            self::DIRECTIVE_REQUEST_RATE => $this->handler->requestRate()->client()->export(),
144
            self::DIRECTIVE_COMMENT => $this->handler->comment()->client()->export(),
145
        ];
146
    }
147
}
148