2018-02-01 12:49:40 +01:00
|
|
|
<?php
|
2020-10-15 00:19:31 +02:00
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-05 00:05:14 +02:00
|
|
|
namespace App\Middleware;
|
2018-02-01 12:49:40 +01:00
|
|
|
|
2018-08-05 00:05:14 +02:00
|
|
|
use App\Entity;
|
|
|
|
use App\Entity\Repository\StationRepository;
|
2019-08-09 17:00:21 +02:00
|
|
|
use App\Http\ServerRequest;
|
2019-08-07 06:33:55 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Slim\Routing\RouteContext;
|
2018-02-01 12:49:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the station specified in the request parameters, and throw an error if none exists but one is required.
|
|
|
|
*/
|
2019-08-07 06:33:55 +02:00
|
|
|
class GetStation implements MiddlewareInterface
|
2018-02-01 12:49:40 +01:00
|
|
|
{
|
2019-08-07 06:33:55 +02:00
|
|
|
public function __construct(
|
2022-06-22 03:48:32 +02:00
|
|
|
protected StationRepository $station_repo
|
2019-08-07 06:33:55 +02:00
|
|
|
) {
|
2018-02-01 12:49:40 +01:00
|
|
|
}
|
2020-07-09 04:32:34 +02:00
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
2018-02-01 12:49:40 +01:00
|
|
|
{
|
2021-06-08 08:40:49 +02:00
|
|
|
$route_args = RouteContext::fromRequest($request)->getRoute()?->getArguments();
|
2018-02-01 12:49:40 +01:00
|
|
|
|
2019-09-10 06:02:08 +02:00
|
|
|
$id = $route_args['station_id'] ?? null;
|
2018-02-01 12:49:40 +01:00
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
if (!empty($id)) {
|
2019-09-19 04:31:13 +02:00
|
|
|
$record = $this->station_repo->findByIdentifier($id);
|
2019-02-02 05:22:52 +01:00
|
|
|
|
|
|
|
if ($record instanceof Entity\Station) {
|
2022-06-22 03:48:32 +02:00
|
|
|
$request = $request->withAttribute(ServerRequest::ATTR_STATION, $record);
|
2019-02-02 05:22:52 +01:00
|
|
|
}
|
2018-02-01 12:49:40 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
return $handler->handle($request);
|
2018-02-01 12:49:40 +01:00
|
|
|
}
|
2018-08-05 00:05:14 +02:00
|
|
|
}
|