|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Equip\Responder; |
|
4
|
|
|
|
|
5
|
|
|
use Destrukt\Set; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Equip\Adr\PayloadInterface; |
|
10
|
|
|
use Equip\Adr\ResponderInterface; |
|
11
|
|
|
use Equip\Resolver\ResolverTrait; |
|
12
|
|
|
use Relay\ResolverInterface; |
|
13
|
|
|
|
|
14
|
|
|
class ChainedResponder extends Set implements ResponderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use ResolverTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param ResolverInterface $resolver |
|
20
|
|
|
* @param array $responders |
|
21
|
|
|
*/ |
|
22
|
5 |
|
public function __construct( |
|
23
|
|
|
ResolverInterface $resolver, |
|
24
|
|
|
array $responders = [ |
|
25
|
|
|
FormattedResponder::class, |
|
26
|
|
|
RedirectResponder::class, |
|
27
|
|
|
] |
|
28
|
|
|
) { |
|
29
|
5 |
|
$this->resolver = $resolver; |
|
30
|
|
|
|
|
31
|
5 |
|
return parent::__construct($responders); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritDoc |
|
36
|
|
|
*/ |
|
37
|
5 |
|
public function validate(array $data) |
|
38
|
|
|
{ |
|
39
|
5 |
|
parent::validate($data); |
|
40
|
|
|
|
|
41
|
5 |
|
foreach ($data as $responder) { |
|
42
|
5 |
|
if (!is_subclass_of($responder, ResponderInterface::class)) { |
|
43
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
44
|
1 |
|
'All responders in `%s` must implement `%s`', |
|
45
|
1 |
|
static::class, |
|
46
|
5 |
|
ResponderInterface::class |
|
47
|
|
|
)); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
5 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
3 |
|
public function __invoke( |
|
56
|
|
|
ServerRequestInterface $request, |
|
57
|
|
|
ResponseInterface $response, |
|
58
|
|
|
PayloadInterface $payload |
|
59
|
|
|
) { |
|
60
|
3 |
|
foreach ($this as $responder) { |
|
61
|
3 |
|
$responder = $this->resolve($responder); |
|
62
|
3 |
|
$response = $responder($request, $response, $payload); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
return $response; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
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.