Delayed   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 8 2
A getSubscribedEvents() 0 4 1
A dispatchDelayedDomainEvents() 0 7 2
A prepareEventNames() 0 6 1
1
<?php
2
3
namespace Knp\RadBundle\DomainEvent\Dispatcher;
4
5
use Doctrine\Common\EventSubscriber;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
8
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
9
10
class Delayed implements EventSubscriber
11
{
12
    private $container;
13
    private $delayedEventNames;
14
    private $events = array();
15
16
    public function __construct(ContainerInterface $container, array $delayedEventNames = [])
17
    {
18
        $this->container = $container;
19
        $this->delayedEventNames = $delayedEventNames;
20
    }
21
22
    public function __call($method, array $arguments)
23
    {
24
        if (!in_array(substr($method, 2), $this->delayedEventNames)) {
25
            throw new \BadMethodCallException;
26
        }
27
28
        $this->events[] = $arguments[0];
29
    }
30
31
    public function getSubscribedEvents()
32
    {
33
        return $this->prepareEventNames($this->delayedEventNames);
34
    }
35
36
    public function dispatchDelayedDomainEvents()
37
    {
38
        $eventManager = $this->container->get('doctrine.dbal.default_connection.event_manager');
39
        foreach ($this->events as $event) {
40
            $eventManager->dispatchEvent('onDelayed' . $event->getName(), $event);
41
        }
42
    }
43
44
    private function prepareEventNames(array $events)
45
    {
46
        return array_map(function ($element) {
47
            return sprintf('on%s', $element);
48
        }, $events);
49
    }
50
}
51