Completed
Pull Request — master (#60)
by Matthijs
01:12
created

LogHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 47
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 10 1
A logEvent() 0 6 2
A logQueued() 0 4 1
A logPersisted() 0 4 1
A logFiltered() 0 4 1
A logFailed() 0 4 1
1
<?php
2
/**
3
 * @author Matthijs van den Bos <[email protected]>
4
 * @copyright 2013 Matthijs van den Bos
5
 */
6
7
namespace Example;
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 LogHandler implements EventSubscriberInterface
15
{
16
    private $debug = false;
17
18
    public function __construct($debug = false)
19
    {
20
        $this->debug = $debug;
21
    }
22
23
    public static function getSubscribedEvents()
24
    {
25
        return array(
26
            SpiderEvents::SPIDER_CRAWL_FILTER_POSTFETCH => 'logFiltered',
27
            SpiderEvents::SPIDER_CRAWL_FILTER_PREFETCH => 'logFiltered',
28
            SpiderEvents::SPIDER_CRAWL_POST_ENQUEUE => 'logQueued',
29
            SpiderEvents::SPIDER_CRAWL_RESOURCE_PERSISTED => 'logPersisted',
30
            SpiderEvents::SPIDER_CRAWL_ERROR_REQUEST => 'logFailed'
31
        );
32
    }
33
34
    protected function logEvent($name, GenericEvent $event)
35
    {
36
        if ($this->debug === true) {
37
            echo "\n[$name]\t:" . $event->getArgument('uri')->toString();
38
        }
39
    }
40
41
    public function logQueued(GenericEvent $event)
42
    {
43
        $this->logEvent('queued', $event);
44
    }
45
46
    public function logPersisted(GenericEvent $event)
47
    {
48
        $this->logEvent('persisted', $event);
49
    }
50
51
    public function logFiltered(GenericEvent $event)
52
    {
53
        $this->logEvent('filtered', $event);
54
    }
55
56
    public function logFailed(GenericEvent $event)
57
    {
58
        $this->logEvent('failed', $event);
59
    }
60
}
61