-
Notifications
You must be signed in to change notification settings - Fork 56
/
OnEventStrategy.php
58 lines (50 loc) · 1.43 KB
/
OnEventStrategy.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
<?php
/*
* This file is part of the prooph/service-bus.
* (c) 2014 - 2015 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 03/11/14 - 21:40
*/
namespace Prooph\ServiceBus\Plugin\InvokeStrategy;
use Prooph\Common\Messaging\HasMessageName;
/**
* Class OnEventStrategy
*
* @package Prooph\ServiceBus\InvokeStrategy
* @author Alexander Miertsch <contact@prooph.de>
*/
class OnEventStrategy extends AbstractInvokeStrategy
{
protected $foo = 'bar';
/**
* @param mixed $handler
* @param mixed $message
* @return bool
*/
public function canInvoke($handler, $message)
{
$handleMethod = 'on' . $this->determineEventName($message);
return method_exists($handler, $handleMethod);
}
/**
* @param mixed $handler
* @param mixed $message
*/
public function invoke($handler, $message)
{
$handleMethod = 'on' . $this->determineEventName($message);
$handler->{$handleMethod}($message);
}
/**
* @param mixed $event
* @return string
*/
protected function determineEventName($event)
{
$eventName = ($event instanceof HasMessageName)? $event->messageName() : (is_object($event)? get_class($event) : gettype($event));
return implode('', array_slice(explode('\\', $eventName), -1));
}
}