Completed
Pull Request — master (#16)
by Matthijs
05:44
created

LogHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

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 VDB\Spider;
8
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\EventDispatcher\GenericEvent;
11
use VDB\Uri\UriInterface;
12
13
use VDB\Spider\Event\SpiderEvents;
14
15
class LogHandler implements EventSubscriberInterface
16
{
17
    private $debug = false;
18
19
    public function __construct($debug = false)
20
    {
21
        $this->debug = $debug;
22
    }
23
24
    public static function getSubscribedEvents()
25
    {
26
        return array(
27
            SpiderEvents::SPIDER_CRAWL_FILTER_POSTFETCH => 'logFiltered',
28
            SpiderEvents::SPIDER_CRAWL_FILTER_PREFETCH => 'logFiltered',
29
            SpiderEvents::SPIDER_CRAWL_POST_ENQUEUE => 'logQueued',
30
            SpiderEvents::SPIDER_CRAWL_RESOURCE_PERSISTED => 'logPersisted',
31
            SpiderEvents::SPIDER_CRAWL_ERROR_REQUEST => 'logFailed'
32
        );
33
    }
34
35
    protected function logEvent($name, GenericEvent $event)
36
    {
37
        if ($this->debug === true) {
38
            echo "\n[$name]\t:" . $event->getArgument('uri')->toString();
39
        }
40
    }
41
42
    public function logQueued(GenericEvent $event)
43
    {
44
        $this->logEvent('queued', $event);
45
    }
46
47
    public function logPersisted(GenericEvent $event)
48
    {
49
        $this->logEvent('persisted', $event);
50
    }
51
52
    public function logFiltered(GenericEvent $event)
53
    {
54
        $this->logEvent('filtered', $event);
55
    }
56
57
    public function logFailed(GenericEvent $event)
58
    {
59
        $this->logEvent('failed', $event);
60
    }
61
}
62