Completed
Push — master ( d70065...3138ff )
by
unknown
03:52
created

UrlCollection::push()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace MediaMonks\Crawler\Url;
4
5
use MediaMonks\Crawler\Exception\EmptyCollectionException;
6
use MediaMonks\Crawler\Url;
7
8
class UrlCollection implements \Countable
9
{
10
11
    /**
12
     * @var Url[]
13
     */
14
    private $urls = [];
15
16
    /**
17
     * @param Url $url
18
     */
19 11
    public function push(Url $url)
20
    {
21 11
        if (!$this->contains($url)) {
22 11
            $this->urls[$url->__toString()] = $url;
23 11
        }
24 11
    }
25
26
    /**
27
     * @return Url
28
     * @throws EmptyCollectionException
29
     */
30 10
    public function pop()
31
    {
32 10
        if ($this->count() === 0) {
33
            throw new EmptyCollectionException();
34
        }
35
36 10
        return array_shift($this->urls);
37
    }
38
39
    /**
40
     * @param Url $url
41
     *
42
     * @return bool
43
     */
44 11
    public function contains(Url $url)
45
    {
46 11
        return isset($this->urls[$url->__toString()]);
47
    }
48
49
    /**
50
     * @return int
51
     */
52 10
    public function count()
53
    {
54 10
        return count($this->urls);
55
    }
56
57
    /**
58
     * @return void
59
     */
60 11
    public function reset()
61
    {
62 11
        $this->urls = [];
63 11
    }
64
65
    /**
66
     * @return array
67
     */
68 9
    public function toArray()
69
    {
70 9
        $stringified = [];
71 9
        foreach ($this->urls as $url) {
72 8
            $stringified[] = $url->__toString();
73 9
        }
74
75 9
        return $stringified;
76
    }
77
}
78