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
|
|
|
$exception = null; |
49
|
|
|
try { |
50
|
|
|
$this->eventDispatcher->dispatch($event->getOriginalName(), $event); |
51
|
|
|
$this->eventDispatcher->dispatch(ProcessedEvents::SUCCESS, new SuccessProcessedEvent($event)); |
52
|
|
|
} catch (\Exception $e) { |
53
|
|
|
$maxRetryCount = $this->config['retry_count'][$this->eventsConfig[$event->getOriginalName()]['type']]; |
54
|
|
|
$this->eventDispatcher->dispatch(ProcessedEvents::FAIL, new FailProcessedEvent($event, $maxRetryCount)); |
55
|
|
|
$exception = $e; |
56
|
|
|
} |
57
|
|
|
$this->eventDispatcher->dispatch(ProcessedEvents::FINISH, new Event()); |
58
|
|
|
if ($event->isFailed() && $exception) { |
59
|
|
|
throw $exception; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|