1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Joselfonseca\LaravelTactician\Handler; |
4
|
|
|
|
5
|
|
|
use League\Tactician\Handler\CommandHandlerMiddleware; |
6
|
|
|
use League\Tactician\Exception\CanNotInvokeHandlerException; |
7
|
|
|
use Joselfonseca\LaravelTactician\Middleware\MiddlewareUndoable; |
8
|
|
|
//use League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor; |
9
|
|
|
use Joselfonseca\LaravelTactician\Handler\CommandNameExtractorUndoable; |
10
|
|
|
use League\Tactician\Handler\Locator\HandlerLocator; |
11
|
|
|
//use League\Tactician\Handler\MethodNameInflector\MethodNameInflector; |
12
|
|
|
use Joselfonseca\LaravelTactician\Handler\MethodNameInflectorUndoable; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class CommandHandlerMiddlewareUndoable implements MiddlewareUndoable |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CommandNameExtractorUndoable |
19
|
|
|
*/ |
20
|
|
|
private $commandNameExtractor; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var HandlerLocator |
24
|
|
|
*/ |
25
|
|
|
private $handlerLocator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var MethodNameInflectorUndoable |
29
|
|
|
*/ |
30
|
|
|
private $methodNameInflector; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param CommandNameExtractorUndoable $commandNameExtractor |
34
|
|
|
* @param HandlerLocator $handlerLocator |
35
|
|
|
* @param MethodNameInflectorUndoable $methodNameInflector |
36
|
|
|
*/ |
37
|
|
|
// Liskov's Principle is L for SOLID, applies here |
38
|
|
|
public function __construct( |
39
|
|
|
CommandNameExtractorUndoable $commandNameExtractor, |
40
|
|
|
HandlerLocator $handlerLocator, |
41
|
|
|
MethodNameInflectorUndoable $methodNameInflector |
42
|
|
|
) { |
43
|
|
|
$this->commandNameExtractor = $commandNameExtractor; |
44
|
|
|
$this->handlerLocator = $handlerLocator; |
45
|
|
|
$this->methodNameInflector = $methodNameInflector; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function execute($command, callable $next) |
49
|
|
|
{ |
50
|
|
|
throw new \InvalidArgumentException(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function executeUndo($command, callable $next): void |
54
|
|
|
{ |
55
|
|
|
//return $this->execute($command, $next); |
56
|
|
|
$commandName = $this->commandNameExtractor->extractUndo($command); |
57
|
|
|
$handler = $this->handlerLocator->getHandlerForCommand($commandName); |
58
|
|
|
$methodName = $this->methodNameInflector->inflectUndo($command, $handler); |
59
|
|
|
//$methodName = 'executeUndo'; |
60
|
|
|
|
61
|
|
|
// is_callable is used here instead of method_exists, as method_exists |
62
|
|
|
// will fail when given a handler that relies on __call. |
63
|
|
|
if (!is_callable([$handler, $methodName])) { |
64
|
|
|
throw CanNotInvokeHandlerException::forCommand( |
65
|
|
|
$command, |
66
|
|
|
"Method '{$methodName}' does not exist on handler" |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$handler->{$methodName}($command); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function executeRedo($command, callable $next):void |
74
|
|
|
{ |
75
|
|
|
//return $this->execute($command, $next); |
76
|
|
|
$commandName = $this->commandNameExtractor->extractRedo($command); |
77
|
|
|
$handler = $this->handlerLocator->getHandlerForCommand($commandName); |
78
|
|
|
$methodName = $this->methodNameInflector->inflectRedo($command, $handler); |
79
|
|
|
//$methodName = 'executeRedo'; |
80
|
|
|
|
81
|
|
|
// is_callable is used here instead of method_exists, as method_exists |
82
|
|
|
// will fail when given a handler that relies on __call. |
83
|
|
|
if (!is_callable([$handler, $methodName])) { |
84
|
|
|
throw CanNotInvokeHandlerException::forCommand( |
85
|
|
|
$command, |
86
|
|
|
"Method '{$methodName}' does not exist on handler" |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$handler->{$methodName}($command); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|