Completed
Push — develop ( e7bb49...81a8f0 )
by Stéphane
02:04
created

Rules::getSitemaps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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