Completed
Branch 2.0-dev (131e57)
by Jan-Petter
02:08
created

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