StatsHandler   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A addToFailed() 0 3 1
A addToPersisted() 0 3 1
A addToFiltered() 0 3 1
A setSpiderId() 0 3 1
A addToQueued() 0 3 1
A getQueued() 0 3 1
A getFailed() 0 3 1
A getFiltered() 0 3 1
A toString() 0 15 1
A getPersisted() 0 3 1
A getSubscribedEvents() 0 8 1
A getSpiderId() 0 3 1
1
<?php
2
/**
3
 * @author Matthijs van den Bos <[email protected]>
4
 * @copyright 2013 Matthijs van den Bos
5
 */
6
7
namespace VDB\Spider;
8
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\EventDispatcher\GenericEvent;
11
use VDB\Uri\UriInterface;
12
use VDB\Spider\Event\SpiderEvents;
13
14
class StatsHandler implements EventSubscriberInterface
15
{
16
    /** @var string */
17
    protected $spiderId;
18
19
    protected $persisted = array();
20
21
    protected $queued = array();
22
23
    protected $filtered = array();
24
25
    protected $failed = array();
26
27
    public static function getSubscribedEvents()
28
    {
29
        return array(
30
            SpiderEvents::SPIDER_CRAWL_FILTER_POSTFETCH => 'addToFiltered',
31
            SpiderEvents::SPIDER_CRAWL_FILTER_PREFETCH => 'addToFiltered',
32
            SpiderEvents::SPIDER_CRAWL_POST_ENQUEUE => 'addToQueued',
33
            SpiderEvents::SPIDER_CRAWL_RESOURCE_PERSISTED => 'addToPersisted',
34
            SpiderEvents::SPIDER_CRAWL_ERROR_REQUEST => 'addToFailed'
35
        );
36
    }
37
38
    public function setSpiderId($spiderId)
39
    {
40
        $this->spiderId = $spiderId;
41
    }
42
43
    public function getSpiderId()
44
    {
45
        return $this->spiderId;
46
    }
47
48
    public function addToQueued(GenericEvent $event)
49
    {
50
        $this->queued[] = $event->getArgument('uri');
51
    }
52
53
    public function addToPersisted(GenericEvent $event)
54
    {
55
        $this->persisted[] = $event->getArgument('uri');
56
    }
57
58
    public function addToFiltered(GenericEvent $event)
59
    {
60
        $this->filtered[] = $event->getArgument('uri');
61
    }
62
63
    public function addToFailed(GenericEvent $event)
64
    {
65
        $this->failed[$event->getArgument('uri')->toString()] = $event->getArgument('message');
66
    }
67
68
    /**
69
     * @return UriInterface[]
70
     */
71
    public function getQueued()
72
    {
73
        return $this->queued;
74
    }
75
76
    /**
77
     * @return UriInterface[]
78
     */
79
    public function getPersisted()
80
    {
81
        return $this->persisted;
82
    }
83
84
    /**
85
     * @return FilterableInterface[]
86
     */
87
    public function getFiltered()
88
    {
89
        return $this->filtered;
90
    }
91
92
    /**
93
     * @return array of form array($uriString, $reason)
94
     */
95
    public function getFailed()
96
    {
97
        return $this->failed;
98
    }
99
100
    public function toString()
101
    {
102
        $spiderId = $this->getSpiderId();
103
        $queued = $this->getQueued();
104
        $filtered = $this->getFiltered();
105
        $failed = $this->getFailed();
106
107
        $string = '';
108
109
        $string .= "\n\nSPIDER ID: " . $spiderId;
110
        $string .= "\n  ENQUEUED:  " . count($queued);
111
        $string .= "\n  SKIPPED:   " . count($filtered);
112
        $string .= "\n  FAILED:    " . count($failed);
113
114
        return $string;
115
    }
116
}
117