Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| MiddlewareRequestHandler | |
0.00% |
0 / 24 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| handle | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| build | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| withOriginalResponse | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getMatchedMiddlewareReferences | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021-2022 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Middleware; |
| 24 | |
| 25 | use GuzzleHttp\Psr7\Response; |
| 26 | use LogicException; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use Psr\Http\Message\RequestInterface; |
| 29 | use Psr\Http\Message\ResponseInterface; |
| 30 | use Psr\Http\Message\ServerRequestInterface; |
| 31 | use Psr\Http\Server\MiddlewareInterface; |
| 32 | use Psr\Http\Server\RequestHandlerInterface; |
| 33 | use Relay\Relay; |
| 34 | |
| 35 | /** |
| 36 | * @deprecated Refer to generis/core/Middleware/README.md |
| 37 | */ |
| 38 | class MiddlewareRequestHandler extends ConfigurableService implements RequestHandlerInterface |
| 39 | { |
| 40 | public const SERVICE_ID = 'tao/MiddlewareRequestHandler'; |
| 41 | public const OPTION_MAP = 'map'; |
| 42 | |
| 43 | /** @var ResponseInterface|null */ |
| 44 | private $originalResponse; |
| 45 | |
| 46 | /** |
| 47 | * @deprecated Refer to generis/core/Middleware/README.md |
| 48 | */ |
| 49 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 50 | { |
| 51 | $queue = $this->build($request); |
| 52 | return (new Relay($queue))->handle($request); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @return MiddlewareInterface[] |
| 57 | */ |
| 58 | private function build(RequestInterface $request): array |
| 59 | { |
| 60 | $mapping = []; |
| 61 | |
| 62 | foreach ($this->getMatchedMiddlewareReferences($request) as $middlewareClass) { |
| 63 | $middleware = $this->getServiceLocator()->get($middlewareClass); |
| 64 | if (!$middleware instanceof MiddlewareInterface) { |
| 65 | throw new LogicException(sprintf('Incorrect middleware configuration for %s', $middlewareClass)); |
| 66 | } |
| 67 | $mapping[] = $middleware; |
| 68 | } |
| 69 | |
| 70 | return array_merge( |
| 71 | [ |
| 72 | function ($request, $next) { |
| 73 | return $this->originalResponse ?? new Response(); |
| 74 | } |
| 75 | ], |
| 76 | $mapping, |
| 77 | [ |
| 78 | function ($request, $next) { |
| 79 | return new Response(); |
| 80 | } |
| 81 | ] |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | public function withOriginalResponse(ResponseInterface $response): self |
| 86 | { |
| 87 | $this->originalResponse = $response; |
| 88 | return $this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return string[] |
| 93 | */ |
| 94 | private function getMatchedMiddlewareReferences(RequestInterface $request): array |
| 95 | { |
| 96 | return $this->getOption(self::OPTION_MAP, [])[$request->getUri()->getPath()] ?? []; |
| 97 | } |
| 98 | } |