|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Equip\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Arbiter\Action; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Relay\ResolverInterface; |
|
9
|
|
|
use Equip\Adr\PayloadInterface; |
|
10
|
|
|
use Equip\Adr\DomainInterface; |
|
11
|
|
|
use Equip\Adr\InputInterface; |
|
12
|
|
|
use Equip\Adr\ResponderInterface; |
|
13
|
|
|
|
|
14
|
|
|
class ActionHandler |
|
15
|
|
|
{ |
|
16
|
|
|
const ACTION_ATTRIBUTE = 'equip/adr:action'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ResolverInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $resolver; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param ResolverInterface $resolver |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function __construct(ResolverInterface $resolver) |
|
27
|
|
|
{ |
|
28
|
1 |
|
$this->resolver = $resolver; |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param ServerRequestInterface $request |
|
33
|
|
|
* @param ResponseInterface $response |
|
34
|
|
|
* @param callable $next |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function __invoke( |
|
37
|
|
|
ServerRequestInterface $request, |
|
38
|
|
|
ResponseInterface $response, |
|
39
|
|
|
callable $next |
|
40
|
|
|
) { |
|
41
|
1 |
|
$action = $request->getAttribute(self::ACTION_ATTRIBUTE); |
|
|
|
|
|
|
42
|
1 |
|
$request = $request->withoutAttribute(self::ACTION_ATTRIBUTE); |
|
43
|
|
|
|
|
44
|
1 |
|
$response = $this->handle($action, $request, $response); |
|
45
|
|
|
|
|
46
|
1 |
|
return $next($request, $response); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Use the action collaborators to get a response. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Action $action |
|
53
|
|
|
* @param ServerRequestInterface $request |
|
54
|
|
|
* @param ResponseInterface $response |
|
55
|
|
|
* |
|
56
|
|
|
* @return ResponseInterface |
|
57
|
|
|
*/ |
|
58
|
1 |
|
private function handle( |
|
59
|
|
|
Action $action, |
|
60
|
|
|
ServerRequestInterface $request, |
|
61
|
|
|
ResponseInterface $response |
|
62
|
|
|
) { |
|
63
|
1 |
|
$domain = $this->resolve($action->getDomain()); |
|
|
|
|
|
|
64
|
1 |
|
$input = $this->resolve($action->getInput()); |
|
|
|
|
|
|
65
|
1 |
|
$responder = $this->resolve($action->getResponder()); |
|
66
|
|
|
|
|
67
|
1 |
|
$payload = $this->payload($domain, $input, $request); |
|
|
|
|
|
|
68
|
1 |
|
$response = $this->response($responder, $request, $response, $payload); |
|
69
|
|
|
|
|
70
|
1 |
|
return $response; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Resolve the class spec into an object. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $spec |
|
77
|
|
|
* |
|
78
|
|
|
* @return object |
|
79
|
|
|
*/ |
|
80
|
1 |
|
private function resolve($spec) |
|
81
|
|
|
{ |
|
82
|
1 |
|
return call_user_func($this->resolver, $spec); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Execute the domain to get a payload using input from the request. |
|
87
|
|
|
* |
|
88
|
|
|
* @param DomainInterface $domain |
|
89
|
|
|
* @param InputInterface $input |
|
90
|
|
|
* @param ServerRequestInterface $request |
|
91
|
|
|
* |
|
92
|
|
|
* @return PayloadInterface |
|
93
|
|
|
*/ |
|
94
|
1 |
|
private function payload( |
|
95
|
|
|
DomainInterface $domain, |
|
96
|
|
|
InputInterface $input, |
|
97
|
|
|
ServerRequestInterface $request |
|
98
|
|
|
) { |
|
99
|
1 |
|
return $domain($input($request)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Execute the responder to marshall the payload into the response. |
|
104
|
|
|
* |
|
105
|
|
|
* @param ResponderInterface $responder |
|
106
|
|
|
* @param ServerRequestInterface $request |
|
107
|
|
|
* @param ResponseInterface $response |
|
108
|
|
|
* @param PayloadInterface $payload |
|
109
|
|
|
* |
|
110
|
|
|
* @return ResponseInterface |
|
111
|
|
|
*/ |
|
112
|
1 |
|
private function response( |
|
113
|
|
|
ResponderInterface $responder, |
|
114
|
|
|
ServerRequestInterface $request, |
|
115
|
|
|
ResponseInterface $response, |
|
116
|
|
|
PayloadInterface $payload |
|
117
|
|
|
) { |
|
118
|
1 |
|
return $responder($request, $response, $payload); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.