Total Complexity | 4 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
14 | class CallbackMiddleware implements MiddlewareInterface |
||
15 | { |
||
16 | private $callable; |
||
17 | |||
18 | /** |
||
19 | * The callback should expect either one or two arguments, depending on |
||
20 | * whether it is receiving a pre or post send notification. |
||
21 | * |
||
22 | * $middleware = new CallbackMiddleware(function($request, $response = null) { |
||
23 | * if ($response) { |
||
24 | * // postSend |
||
25 | * } else { |
||
26 | * // preSend |
||
27 | * } |
||
28 | * }); |
||
29 | * |
||
30 | * @param mixed $callable A PHP callable |
||
31 | * |
||
32 | * @throws InvalidArgumentException If the argument is not callable |
||
33 | */ |
||
34 | 2 | public function __construct($callable) |
|
35 | { |
||
36 | 2 | if (!\is_callable($callable)) { |
|
37 | 1 | throw new InvalidArgumentException('The argument is not callable.'); |
|
38 | } |
||
39 | |||
40 | 1 | $this->callable = $callable; |
|
41 | 1 | } |
|
42 | |||
43 | 1 | public function handleRequest(RequestInterface $request, callable $next) |
|
44 | { |
||
45 | 1 | $request = \call_user_func($this->callable, $request); |
|
46 | |||
47 | 1 | return $next($request); |
|
48 | } |
||
49 | |||
50 | 1 | public function handleResponse(RequestInterface $request, ResponseInterface $response, callable $next) |
|
55 | } |
||
56 | } |
||
57 |