Passed
Branch feature/cleanup (2bd333)
by Matthijs
06:03
created

InMemoryQueueManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 89
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setTraversalAlgorithm() 0 4 1
A getTraversalAlgorithm() 0 4 1
A setDispatcher() 0 6 1
A getDispatcher() 0 7 2
A addUri() 0 14 3
A next() 0 10 3
1
<?php
2
/**
3
 * @author Matthijs van den Bos <[email protected]>
4
 * @copyright 2013 Matthijs van den Bos
5
 */
6
7
namespace VDB\Spider\QueueManager;
8
9
use VDB\Uri\UriInterface;
10
use VDB\Spider\Exception\QueueException;
11
use Symfony\Component\EventDispatcher\Event;
12
use Symfony\Component\EventDispatcher\EventDispatcher;
13
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14
use Symfony\Component\EventDispatcher\GenericEvent;
15
use VDB\Spider\Event\SpiderEvents;
16
17
/**
18
 * TODO: make this an Iterable
19
 */
20
class InMemoryQueueManager implements QueueManager
21
{
22
    /** @var int The maximum depth for the crawl */
23
    public $maxDepth = 3;
24
25
    /** @var int The maximum size of the process queue for this spider. 0 means infinite */
26
    public $maxQueueSize = 0;
27
28
    /** @var int the amount of times a Resource was enqueued */
29
    private $currentQueueSize = 0;
30
31
    /** @var UriInterface[] the list of URIs to process */
32
    private $traversalQueue = array();
33
34
    /** @var int The traversal algorithm to use. Choose from the class constants
35
     */
36
    private $traversalAlgorithm = self::ALGORITHM_DEPTH_FIRST;
37
38
    /** @var EventDispatcherInterface */
39
    private $dispatcher;
40
41
    /**
42
     * @param int $traversalAlgorithm Choose from the class constants
43
     * TODO: This should be extracted to a Strategy pattern
44
     */
45
    public function setTraversalAlgorithm($traversalAlgorithm)
46
    {
47
        $this->traversalAlgorithm = $traversalAlgorithm;
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getTraversalAlgorithm()
54
    {
55
        return $this->traversalAlgorithm;
56
    }
57
58
    /**
59
     * @param EventDispatcherInterface $eventDispatcher
60
     * @return $this
61
     */
62
    public function setDispatcher(EventDispatcherInterface $eventDispatcher)
63
    {
64
        $this->dispatcher = $eventDispatcher;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @return EventDispatcherInterface
71
     */
72
    public function getDispatcher()
73
    {
74
        if (!$this->dispatcher) {
75
            $this->dispatcher = new EventDispatcher();
76
        }
77
        return $this->dispatcher;
78
    }
79
80
    /**
81
     * @param UriInterface
82
     */
83
    public function addUri(UriInterface $uri)
84
    {
85
        if ($this->maxQueueSize != 0 && $this->currentQueueSize >= $this->maxQueueSize) {
86
            throw new QueueException('Maximum Queue Size of ' . $this->maxQueueSize . ' reached');
87
        }
88
89
        $this->currentQueueSize++;
90
        array_push($this->traversalQueue, $uri);
91
92
        $this->getDispatcher()->dispatch(
93
            SpiderEvents::SPIDER_CRAWL_POST_ENQUEUE,
94
            new GenericEvent($this, array('uri' => $uri))
95
        );
96
    }
97
98
    public function next()
99
    {
100
        if ($this->traversalAlgorithm === static::ALGORITHM_DEPTH_FIRST) {
101
            return array_pop($this->traversalQueue);
102
        } elseif ($this->traversalAlgorithm === static::ALGORITHM_BREADTH_FIRST) {
103
            return array_shift($this->traversalQueue);
104
        } else {
105
            throw new \LogicException('No search algorithm set');
106
        }
107
    }
108
}
109