-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathServiceSubscription.php
49 lines (41 loc) · 1.21 KB
/
ServiceSubscription.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
<?php
/**
* @LICENSE_TEXT
*/
namespace EventBand\Bundle;
use EventBand\AbstractSubscription;
use EventBand\BandDispatcher;
use EventBand\Event;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class ServiceSubscription
*
* @author Kirill chEbba Chebunin <iam@chebba.org>
*/
class ServiceSubscription extends AbstractSubscription
{
private $container;
private $serviceId;
private $method;
public function __construct($eventName, ContainerInterface $container, $serviceId, $method = null, $band = '')
{
$this->container = $container;
$this->serviceId = $serviceId;
$this->method = $method;
parent::__construct($eventName, $band);
}
/**
* {@inheritDoc}
*/
public function dispatch(Event $event, BandDispatcher $dispatcher)
{
$service = $this->container->get($this->serviceId);
$callback = $this->method ? [$service, $this->method] : $service;
if (!is_callable($callback)) {
throw new \RuntimeException(
sprintf('Wrong service callback [id: "%s", method: "%s"]', $this->serviceId, $this->method)
);
}
return $callback($event, $dispatcher);
}
}