Completed
Push — master ( dc3dad...b5f967 )
by Jan-Petter
01:56
created

UserAgent::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
namespace vipnytt\RobotsTxtParser\Directives;
3
4
use vipnytt\RobotsTxtParser\ObjectTools;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
7
/**
8
 * Class UserAgent
9
 *
10
 * @package vipnytt\RobotsTxtParser\Directives
11
 */
12
final class UserAgent implements DirectiveInterface, RobotsTxtInterface
13
{
14
    use ObjectTools;
15
16
    /**
17
     * Sub directives white list
18
     */
19
    const SUB_DIRECTIVES = [
20
        self::DIRECTIVE_ALLOW,
21
        self::DIRECTIVE_CACHE_DELAY,
22
        self::DIRECTIVE_CRAWL_DELAY,
23
        self::DIRECTIVE_DISALLOW,
24
    ];
25
26
    /**
27
     * Directive
28
     */
29
    const DIRECTIVE = self::DIRECTIVE_USER_AGENT;
30
    
31
    /**
32
     * All User-agents declared
33
     * @var array
34
     */
35
    public $userAgents = [];
36
    /**
37
     * Sub-directive Allow
38
     * @var array
39
     */
40
    public $allow = [];
41
    /**
42
     * Sub-directive Cache-delay
43
     * @var array
44
     */
45
    public $cacheDelay = [];
46
    /**
47
     * Sub-directive Crawl-delay
48
     * @var array
49
     */
50
    public $crawlDelay = [];
51
    /**
52
     * Sub-directive Disallow
53
     * @var array
54
     */
55
    public $disallow = [];
56
    /**
57
     * Current User-agent(s)
58
     * @var array
59
     */
60
    protected $userAgent = [];
61
62
    /**
63
     * UserAgent constructor.
64
     */
65
    public function __construct()
66
    {
67
        $this->set();
68
    }
69
70
    /**
71
     * Set new User-agent
72
     *
73
     * @param array $array
74
     * @return bool
75
     */
76
    public function set($array = [self::USER_AGENT])
77
    {
78
        $this->userAgent = $array;
79
        foreach ($this->userAgent as $userAgent) {
80
            if (!in_array($userAgent, $this->userAgents)) {
81
                $this->allow[$userAgent] = new DisAllow(self::DIRECTIVE_ALLOW);
82
                $this->cacheDelay[$userAgent] = new CrawlDelay(self::DIRECTIVE_CACHE_DELAY);
83
                $this->crawlDelay[$userAgent] = new CrawlDelay(self::DIRECTIVE_CRAWL_DELAY);
84
                $this->disallow[$userAgent] = new DisAllow(self::DIRECTIVE_DISALLOW);
85
                $this->userAgents[] = $userAgent;
86
            }
87
        }
88
        return true;
89
    }
90
91
    /**
92
     * Add
93
     *
94
     * @param string $line
95
     * @return bool
96
     */
97
    public function add($line)
98
    {
99
        $result = false;
100
        $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
101
        foreach ($this->userAgent as $userAgent) {
102
            switch ($pair['directive']) {
103
                case self::DIRECTIVE_ALLOW:
104
                    $result = $this->allow[$userAgent]->add($pair['value']);
105
                    break;
106
                case self::DIRECTIVE_CACHE_DELAY:
107
                    $result = $this->cacheDelay[$userAgent]->add($pair['value']);
108
                    break;
109
                case self::DIRECTIVE_CRAWL_DELAY:
110
                    $result = $this->crawlDelay[$userAgent]->add($pair['value']);
111
                    break;
112
                case self::DIRECTIVE_DISALLOW:
113
                    $result = $this->disallow[$userAgent]->add($pair['value']);
114
                    break;
115
            }
116
        }
117
        return isset($result) ? $result : false;
118
    }
119
120
    /**
121
     * Export
122
     *
123
     * @return array
124
     */
125
    public function export()
126
    {
127
        $result = [];
128
        foreach ($this->userAgents as $userAgent) {
129
            $current = $this->allow[$userAgent]->export()
130
                + $this->cacheDelay[$userAgent]->export()
131
                + $this->crawlDelay[$userAgent]->export()
132
                + $this->disallow[$userAgent]->export();
133
            if (!empty($current)) {
134
                $result[$userAgent] = $current;
135
            }
136
        }
137
        return empty($result) ? [] : [self::DIRECTIVE => $result];
138
    }
139
}
140