-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventDispatcherProcessor.php
57 lines (44 loc) · 1.52 KB
/
EventDispatcherProcessor.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
<?php
declare(strict_types=1);
namespace JeanBeru\PipelineBundle\Processor;
use JeanBeru\PipelineBundle\Event\AfterProcessorEvent;
use JeanBeru\PipelineBundle\Event\AfterStageEvent;
use JeanBeru\PipelineBundle\Event\BeforeProcessorEvent;
use JeanBeru\PipelineBundle\Event\BeforeStageEvent;
use League\Pipeline\ProcessorInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
final class EventDispatcherProcessor implements ProcessorInterface
{
private EventDispatcherInterface $eventDispatcher;
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @param mixed $payload
*
* @return mixed
*/
public function process($payload, callable ...$stages)
{
$this->eventDispatcher->dispatch(new BeforeProcessorEvent($payload));
foreach ($stages as $stage) {
$stageName = $this->getStageName($stage);
$this->eventDispatcher->dispatch(new BeforeStageEvent($stageName, $payload));
$payload = $stage($payload);
$this->eventDispatcher->dispatch(new AfterStageEvent($stageName, $payload));
}
$this->eventDispatcher->dispatch(new AfterProcessorEvent($payload));
return $payload;
}
private function getStageName(callable $stage): string
{
if (is_string($stage)) {
return $stage;
}
if (is_object($stage)) {
return \get_class($stage);
}
return 'callable';
}
}