Completed
Push — develop ( 990136...183c36 )
by Stéphane
02:08
created

Rules::get()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 23
ccs 19
cts 19
cp 1
rs 8.5906
cc 5
eloc 17
nc 4
nop 1
crap 5
1
<?php
2
3
namespace Bee4\RobotsTxt;
4
5
use Bee4\RobotsTxt\Exception\DuplicateRuleException;
6
7
/**
8
 * Class Rules
9
 * Represent a collection of Rules
10
 *
11
 * @copyright Bee4 2015
12
 * @author    Stephane HULARD <[email protected]>
13
 */
14
class Rules
15
{
16
    const DEFAULT_UA = '*';
17
18
    /**
19
     * The collection of rules
20
     * @var array
21
     */
22
    protected $collection = [];
23
24
    /**
25
     * Default rule used if robots.txt is empty
26
     * @var Rule
27
     */
28
    private $defaultRule;
29
30 4
    public function __construct()
31
    {
32 4
        $this->defaultRule = new Rule();
33 4
        $this->add(self::DEFAULT_UA, $this->defaultRule);
34 4
    }
35
36
    /**
37
     * Add a new rule to the collection
38
     * @param string $userAgent
39
     * @param Rule $rule
40
     * @return Rules
41
     */
42 4
    public function add($userAgent, Rule $rule)
43
    {
44 4
        $userAgent = $this->handleUa($userAgent);
45 4
        if (isset($this->collection[$userAgent]) &&
46 4
                $this->collection[$userAgent] !== $this->defaultRule ) {
47 1
            throw (new DuplicateRuleException(
48
                'You can\'t add 2 rules for the same UserAgent'
49 1
            ))
50 1
                ->setRule($rule)
51 1
                ->setUserAgent($userAgent);
52
        }
53 4
        $this->collection[$userAgent] = $rule;
54
55 4
        return $this;
56
    }
57
58
    /**
59
     * Check if the URL match for the given UA or not
60
     * @param string $userAgent
61
     * @param string $url
62
     * @return boolean
63
     */
64 1
    public function match($userAgent, $url)
65
    {
66 1
        if (($rule = $this->get($userAgent)) === null) {
67
            return false;
68
        }
69 1
        return $rule->match($url);
70
    }
71
72
    /**
73
     * Retrieve rules for a given UA
74
     * @param string $userAgent
75
     * @return null|Rule
76
     */
77 3
    public function get($userAgent)
78
    {
79 3
        $item = null;
80 3
        $iterator = new \ArrayIterator($this->collection);
81 3
        iterator_apply(
82 3
            $iterator,
83 3
            function (\ArrayIterator $iterator, $userAgent) use (&$item) {
84 3
                if ($iterator->key() != Rules::DEFAULT_UA &&
85 3
                        preg_match($iterator->key(), $userAgent) === 1 ) {
86 1
                    $item = $iterator->current();
87 1
                    return false;
88
                }
89 3
                return true;
90 3
            },
91 3
            [$iterator, $userAgent]
92 3
        );
93
94 3
        return $item!==null?
95 3
            $item:
96 3
            (isset($this->collection[self::DEFAULT_UA])?
97 3
                $this->collection[self::DEFAULT_UA]:
98 3
                null);
99
    }
100
101
    /**
102
     * Update the UA to make a valid regexp
103
     * @param string $userAgent
104
     * @return string
105
     */
106 4
    private function handleUa($userAgent)
107
    {
108 4
        if ($userAgent == self::DEFAULT_UA) {
109 4
            return $userAgent;
110
        }
111 1
        return '/^'.preg_quote($userAgent).'.*/i';
112
    }
113
}
114