Completed
Push — master ( 3138ff...d9a894 )
by
unknown
03:46
created

UrlCollection::pop()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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|false
28
     * @throws EmptyCollectionException
29
     */
30 10
    public function pop()
31
    {
32 10
        $url = array_shift($this->urls);
33 10
        if (empty($url)) {
34 7
            return false;
35
        }
36
37 10
        return $url;
38
    }
39
40
    /**
41
     * @param Url $url
42
     *
43
     * @return bool
44
     */
45 11
    public function contains(Url $url)
46
    {
47 11
        return isset($this->urls[$url->__toString()]);
48
    }
49
50
    /**
51
     * @return int
52
     */
53 1
    public function count()
54
    {
55 1
        return count($this->urls);
56
    }
57
58
    /**
59
     * @return void
60
     */
61 11
    public function reset()
62
    {
63 11
        $this->urls = [];
64 11
    }
65
66
    /**
67
     * @return array
68
     */
69 9
    public function toArray()
70
    {
71 9
        $stringified = [];
72 9
        foreach ($this->urls as $url) {
73 8
            $stringified[] = $url->__toString();
74 9
        }
75
76 9
        return $stringified;
77
    }
78
}
79