-
Notifications
You must be signed in to change notification settings - Fork 2
/
ResourceEventDispatcher.php
73 lines (64 loc) · 1.98 KB
/
ResourceEventDispatcher.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
<?php
namespace ProgrammingAreHard\ResourceBundle\Domain\EventDispatcher;
use ProgrammingAreHard\ResourceBundle\Domain\Event\ResourceEvent;
use ProgrammingAreHard\ResourceBundle\Domain\ResourceInterface;
use ProgrammingAreHard\ResourceBundle\Domain\Transformer\TransformerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ResourceEventDispatcher implements ResourceEventDispatcherInterface
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var TransformerInterface
*/
private $classNameTransformer;
/**
* Constructor.
*
* @param EventDispatcherInterface $dispatcher
* @param TransformerInterface $classNameExtractor
*/
public function __construct(EventDispatcherInterface $dispatcher, TransformerInterface $classNameTransformer)
{
$this->dispatcher = $dispatcher;
$this->classNameTransformer = $classNameTransformer;
}
/**
* Dispatch a resource event.
*
* @param string $name
* @param ResourceInterface $resource
* @return ResourceEvent
*/
public function dispatch($name, ResourceInterface $resource)
{
$eventName = $this->createEventName($name, $resource);
$event = $this->createEvent($resource);
$this->dispatcher->dispatch($eventName, $event);
return $event;
}
/**
* Create the full event name.
*
* @param string $name
* @param ResourceInterface $resource
* @return string
*/
protected function createEventName($name, ResourceInterface $resource)
{
$resourceName = $this->classNameTransformer->transform(get_class($resource));
return $resourceName . '.' . $name;
}
/**
* Create a resource event object.
*
* @param ResourceInterface $resource
* @return ResourceEvent
*/
protected function createEvent(ResourceInterface $resource)
{
return new ResourceEvent($resource);
}
}