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

Rules   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 0 Features 3
Metric Value
wmc 12
c 11
b 0
f 3
lcom 1
cbo 2
dl 0
loc 103
ccs 32
cts 32
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 14 3
A match() 0 4 1
A get() 0 21 4
A handleUa() 0 7 2
A count() 0 4 1
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 implements \Countable
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
    public function __construct()
31
    {
32 1
        $this->defaultRule = new Rule(self::DEFAULT_UA);
33 1
        $this->add($this->defaultRule);
34 1
    }
35
36
    /**
37
     * Add a new rule to the collection
38
     * @param string $userAgent
0 ignored issues
show
Bug introduced by
There is no parameter named $userAgent. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     * @param Rule $rule
40
     * @return Rules
41
     */
42
    public function add(Rule $rule)
43
    {
44 1
        $userAgent = $this->handleUa($rule->getUserAgent());
45 1
        if (isset($this->collection[$userAgent]) &&
46 1
                $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
        }
52 1
        $this->collection[$userAgent] = $rule;
53
54 1
        return $this;
55
    }
56
57
    /**
58
     * Check if the URL match for the given UA or not
59
     * @param string $userAgent
60
     * @param string $url
61
     * @return boolean
62
     */
63
    public function match($userAgent, $url)
64
    {
65 1
        return $this->get($userAgent)->match($url);
66
    }
67
68
    /**
69
     * Retrieve rules for a given UA
70
     * @param string $userAgent
71
     * @return null|Rule
72
     */
73
    public function get($userAgent)
74
    {
75 1
        $item = null;
76 1
        $iterator = new \ArrayIterator($this->collection);
77 1
        iterator_apply(
78 1
            $iterator,
79 1
            function (\ArrayIterator $iterator, $userAgent) use (&$item) {
80 1
                if ($iterator->key() != Rules::DEFAULT_UA &&
81 1
                        preg_match($iterator->key(), $userAgent) === 1 ) {
82 1
                    $item = $iterator->current();
83 1
                    return false;
84
                }
85 1
                return true;
86 1
            },
87 1
            [$iterator, $userAgent]
88 1
        );
89
90 1
        return $item!==null?
91 1
            $item:
92 1
            $this->collection[self::DEFAULT_UA];
93
    }
94
95
    /**
96
     * Update the UA to make a valid regexp
97
     * @param string $userAgent
98
     * @return string
99
     */
100
    private function handleUa($userAgent)
101
    {
102 1
        if ($userAgent == self::DEFAULT_UA) {
103 1
            return $userAgent;
104
        }
105 1
        return '/^'.preg_quote($userAgent).'.*/i';
106
    }
107
108
    /**
109
     * Return the number of rules
110
     * @return integer
111
     */
112
    public function count()
113
    {
114 1
        return count($this->collection);
115
    }
116
}
117