DigiComp.FlowTranslationEnd.../Classes/Http/TransformTranslationRequestMiddleware.php
Ferdinand Kuhl ef297fa8e1
All checks were successful
ci/woodpecker/push/code-style Pipeline was successful
ci/woodpecker/push/functional-tests Pipeline was successful
review: consistent naming and methodology
2023-08-08 12:11:46 +02:00

46 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace DigiComp\FlowTranslationEndpoint\Http;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class TransformTranslationRequestMiddleware implements MiddlewareInterface
{
/**
* @var string
*/
protected string $headerName;
/**
* @var string|null
*/
protected ?string $reactOnPath;
/**
* @var string
*/
protected string $translateGetParam;
public function __construct(string $headerName, ?string $reactOnPath, string $translateGetParam)
{
$this->headerName = $headerName;
$this->reactOnPath = $reactOnPath;
$this->translateGetParam = $translateGetParam;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->reactOnPath !== null && $request->getUri()->getPath() === $this->reactOnPath) {
$request = $request->withAddedHeader(
$this->headerName,
$request->getQueryParams()[$this->translateGetParam] ?? ''
);
}
return $handler->handle($request);
}
}