Completed
Pull Request — master (#26)
by Julien
04:20
created

EventProcessor::process()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 9
nc 5
nop 1
crap 12
1
<?php
2
3
namespace Itkg\DelayEventBundle\Processor;
4
5
use Itkg\DelayEventBundle\Event\FailProcessedEvent;
6
use Itkg\DelayEventBundle\Event\ProcessedEvents;
7
use Itkg\DelayEventBundle\Event\SuccessProcessedEvent;
8
use Itkg\DelayEventBundle\Model\Event;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
11
/**
12
 * Class EventProcessor
13
 */
14
class EventProcessor implements EventProcessorInterface
15
{
16
    /**
17
     * @var EventDispatcherInterface
18
     */
19
    private $eventDispatcher;
20
21
    /**
22
     * @var array
23
     */
24
    private $config = array();
25
26
    /**
27
     * @var array
28
     */
29
    private $eventsConfig = array();
30
31
    /**
32
     * @param EventDispatcherInterface $eventDispatcher
33
     * @param array                    $config
34
     * @param array                    $eventsConfig
35
     */
36
    public function __construct(EventDispatcherInterface $eventDispatcher, array $config, array $eventsConfig)
37
    {
38
        $this->eventDispatcher = $eventDispatcher;
39
        $this->config = $config;
40
        $this->eventsConfig = $eventsConfig;
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function process(Event $event)
47
    {
48
        try {
49
            $this->eventDispatcher->dispatch($event->getOriginalName(), $event);
50
            $this->eventDispatcher->dispatch(ProcessedEvents::SUCCESS, new SuccessProcessedEvent($event));
51
        } catch (\Exception $e) {
52
            $maxRetryCount = $this->config['retry_count'][$this->eventsConfig[$event->getOriginalName()]['type']];
53
            $this->eventDispatcher->dispatch(ProcessedEvents::FAIL, new FailProcessedEvent($event, $maxRetryCount));
54
            if ($event->isFailed()) {
55
                throw $e;
56
            }
57
        }
58
    }
59
}
60