Completed
Branch 2.0-dev (d250b8)
by Jan-Petter
03:02
created

UserAgentParser::add()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 34
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 4.8196
cc 10
eloc 30
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
5
6
/**
7
 * Class UserAgentParser
8
 *
9
 * @package vipnytt\RobotsTxtParser\Parser\Directives
10
 */
11
class UserAgentParser implements ParserInterface, RobotsTxtInterface
12
{
13
    use DirectiveParserCommons;
14
15
    /**
16
     * Sub directives white list
17
     */
18
    const SUB_DIRECTIVES = [
19
        self::DIRECTIVE_ALLOW,
20
        self::DIRECTIVE_CACHE_DELAY,
21
        self::DIRECTIVE_COMMENT,
22
        self::DIRECTIVE_CRAWL_DELAY,
23
        self::DIRECTIVE_DISALLOW,
24
        self::DIRECTIVE_REQUEST_RATE,
25
        self::DIRECTIVE_ROBOT_VERSION,
26
        self::DIRECTIVE_VISIT_TIME,
27
    ];
28
29
    /**
30
     * Directive
31
     */
32
    const DIRECTIVE = self::DIRECTIVE_USER_AGENT;
33
34
    /**
35
     * All User-agents declared
36
     * @var array
37
     */
38
    public $userAgents = [];
39
40
    /**
41
     * Sub-directive Allow
42
     * @var DisAllowParser[]
43
     */
44
    public $allow = [];
45
46
    /**
47
     * Sub-directive Cache-delay
48
     * @var CrawlDelayParser[]
49
     */
50
    public $cacheDelay = [];
51
52
    /**
53
     * Sub-directive Comment
54
     * @var CommentParser[]
55
     */
56
    public $comment = [];
57
58
    /**
59
     * Sub-directive Crawl-delay
60
     * @var CrawlDelayParser[]
61
     */
62
    public $crawlDelay = [];
63
64
    /**
65
     * Sub-directive Disallow
66
     * @var DisAllowParser[]
67
     */
68
    public $disallow = [];
69
70
    /**
71
     * Sub-directive RequestClient-rate
72
     * @var RequestRateParser[]
73
     */
74
    public $requestRate = [];
75
76
    /**
77
     * Sub-directive Robot-version
78
     * @var RobotVersionParser[]
79
     */
80
    public $robotVersion = [];
81
82
    /**
83
     * Sub-directive Visit-time
84
     * @var VisitTimeParser[]
85
     */
86
    public $visitTime = [];
87
88
    /**
89
     * Current User-agent(s)
90
     * @var array
91
     */
92
    protected $userAgent = [];
93
94
    /**
95
     * UserAgent constructor.
96
     */
97
    public function __construct()
98
    {
99
        $this->set();
100
    }
101
102
    /**
103
     * Set new User-agent
104
     *
105
     * @param array $array
106
     * @return bool
107
     */
108
    public function set(array $array = [self::USER_AGENT])
109
    {
110
        $this->userAgent = array_map('mb_strtolower', $array);
111
        foreach ($this->userAgent as $userAgent) {
112
            if (!in_array($userAgent, $this->userAgents)) {
113
                $this->allow[$userAgent] = new DisAllowParser(self::DIRECTIVE_ALLOW);
114
                $this->cacheDelay[$userAgent] = new CrawlDelayParser(self::DIRECTIVE_CACHE_DELAY);
115
                $this->comment[$userAgent] = new CommentParser();
116
                $this->crawlDelay[$userAgent] = new CrawlDelayParser(self::DIRECTIVE_CRAWL_DELAY);
117
                $this->disallow[$userAgent] = new DisAllowParser(self::DIRECTIVE_DISALLOW);
118
                $this->requestRate[$userAgent] = new RequestRateParser();
119
                $this->robotVersion[$userAgent] = new RobotVersionParser();
120
                $this->visitTime[$userAgent] = new VisitTimeParser();
121
                $this->userAgents[] = $userAgent;
122
            }
123
        }
124
        return true;
125
    }
126
127
    /**
128
     * Add
129
     *
130
     * @param string $line
131
     * @return bool
132
     */
133
    public function add($line)
134
    {
135
        $result = [];
136
        $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
137
        foreach ($this->userAgent as $userAgent) {
138
            switch ($pair['directive']) {
139
                case self::DIRECTIVE_ALLOW:
140
                    $result[] = $this->allow[$userAgent]->add($pair['value']);
141
                    break;
142
                case self::DIRECTIVE_CACHE_DELAY:
143
                    $result[] = $this->cacheDelay[$userAgent]->add($pair['value']);
144
                    break;
145
                case self::DIRECTIVE_COMMENT:
146
                    $result[] = $this->comment[$userAgent]->add($pair['value']);
147
                    break;
148
                case self::DIRECTIVE_CRAWL_DELAY:
149
                    $result[] = $this->crawlDelay[$userAgent]->add($pair['value']);
150
                    break;
151
                case self::DIRECTIVE_DISALLOW:
152
                    $result[] = $this->disallow[$userAgent]->add($pair['value']);
153
                    break;
154
                case self::DIRECTIVE_REQUEST_RATE:
155
                    $result[] = $this->requestRate[$userAgent]->add($pair['value']);
156
                    break;
157
                case self::DIRECTIVE_ROBOT_VERSION:
158
                    $result[] = $this->robotVersion[$userAgent]->add($pair['value']);
159
                    break;
160
                case self::DIRECTIVE_VISIT_TIME:
161
                    $result[] = $this->visitTime[$userAgent]->add($pair['value']);
162
                    break;
163
            }
164
        }
165
        return in_array(true, $result, true);
166
    }
167
168
    /**
169
     * Export rules
170
     *
171
     * @return array
172
     */
173
    public function export()
174
    {
175
        $result = [];
176
        foreach ($this->userAgents as $userAgent) {
177
            $current = array_merge(
178
                $this->allow[$userAgent]->export(),
179
                $this->comment[$userAgent]->export(),
180
                $this->cacheDelay[$userAgent]->export(),
181
                $this->crawlDelay[$userAgent]->export(),
182
                $this->disallow[$userAgent]->export(),
183
                $this->requestRate[$userAgent]->export(),
184
                $this->robotVersion[$userAgent]->export(),
185
                $this->visitTime[$userAgent]->export()
186
            );
187
            if (!empty($current)) {
188
                $result[$userAgent] = $current;
189
            }
190
        }
191
        return empty($result) ? [] : [self::DIRECTIVE => $result];
192
    }
193
194
    /**
195
     * Render
196
     *
197
     * @return string[]
198
     */
199
    public function render()
200
    {
201
        $result = [];
202
        sort($this->userAgents);
203
        foreach ($this->userAgents as $userAgent) {
204
            $current = array_merge(
205
                $this->allow[$userAgent]->render(),
206
                $this->comment[$userAgent]->render(),
207
                $this->cacheDelay[$userAgent]->render(),
208
                $this->crawlDelay[$userAgent]->render(),
209
                $this->disallow[$userAgent]->render(),
210
                $this->requestRate[$userAgent]->render(),
211
                $this->robotVersion[$userAgent]->render(),
212
                $this->visitTime[$userAgent]->render()
213
            );
214
            if (!empty($current)) {
215
                $result = array_merge($result, [self::DIRECTIVE . ':' . $userAgent], $current);
216
            }
217
        }
218
        return $result;
219
    }
220
}
221