-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lifecycle.php
74 lines (65 loc) · 1.7 KB
/
Lifecycle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Havvg\Component\Lifecycle;
use Havvg\Component\Lifecycle\Event\EventInterface;
use Havvg\Component\Lifecycle\Event\EventProcessorInterface;
use Havvg\Component\Lifecycle\Event\EventProviderInterface;
use Havvg\Component\Lifecycle\Event\Runtime\EventTriggeredEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Lifecycle implements EventSubscriberInterface
{
/**
* @var EventProviderInterface
*/
private $provider;
/**
* @var EventProcessorInterface
*/
private $processor;
/**
* Constructor.
*
* @param EventProviderInterface $provider
* @param EventProcessorInterface $processor
*/
public function __construct(EventProviderInterface $provider, EventProcessorInterface $processor)
{
$this->provider = $provider;
$this->processor = $processor;
}
/**
* Handles the lifecycle resulting by the provided data.
*/
public function handle()
{
foreach ($this->provider->getEvents() as $eachEvent) {
$this->process($eachEvent);
}
}
/**
* A lifecycle event has been triggered.
*
* @param EventTriggeredEvent $event
*/
public function onEventTriggered(EventTriggeredEvent $event)
{
$this->process($event->getEvent());
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
LifecycleEvents::EVENT_TRIGGERED => 'onEventTriggered',
];
}
/**
* Processes the given Event.
*
* @param EventInterface $event
*/
private function process(EventInterface $event)
{
$this->processor->process($event);
}
}