-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
Signed-off-by: alexmerlin <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Api\App\Handler; | ||
|
||
use Api\App\Exception\BadRequestException; | ||
use Api\App\Exception\ConflictException; | ||
use Api\App\Exception\ExpiredException; | ||
use Api\App\Exception\ForbiddenException; | ||
use Api\App\Exception\MethodNotAllowedException; | ||
use Api\App\Exception\NotFoundException; | ||
use Api\App\Exception\UnauthorizedException; | ||
use Dot\Mail\Exception\MailException; | ||
Check warning on line 14 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
use Exception; | ||
use Fig\Http\Message\RequestMethodInterface; | ||
Check warning on line 16 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
use Fig\Http\Message\StatusCodeInterface; | ||
Check warning on line 17 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
use Mezzio\Hal\Metadata\MetadataMap; | ||
Check warning on line 18 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
use Mezzio\Hal\Metadata\RouteBasedCollectionMetadata; | ||
Check warning on line 19 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
use Mezzio\Hal\ResourceGenerator\Exception\OutOfBoundsException; | ||
Check warning on line 20 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
use Mezzio\Router\RouteResult; | ||
Check warning on line 21 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
use Psr\Http\Message\ResponseInterface; | ||
Check warning on line 22 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
Check warning on line 23 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
use RuntimeException; | ||
|
||
use function array_key_exists; | ||
use function is_array; | ||
use function method_exists; | ||
use function sprintf; | ||
use function strtolower; | ||
|
||
trait HandlerTrait | ||
{ | ||
use ResponseTrait; | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
Check warning on line 39 in src/App/src/Handler/HandlerTrait.php GitHub Actions / Qodana for PHPUndefined class
|
||
{ | ||
try { | ||
$method = strtolower($request->getMethod()); | ||
if ($this->isGetCollectionRequest($request, $this->config)) { | ||
$method = 'getCollection'; | ||
} | ||
|
||
if (! method_exists($this, $method)) { | ||
throw new MethodNotAllowedException( | ||
sprintf('Method %s is not implemented for the requested resource.', $method) | ||
); | ||
} | ||
|
||
return $this->$method($request); | ||
} catch (ConflictException $exception) { | ||
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_CONFLICT); | ||
} catch (ForbiddenException $exception) { | ||
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_FORBIDDEN); | ||
} catch (ExpiredException $exception) { | ||
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_GONE); | ||
} catch (OutOfBoundsException | NotFoundException $exception) { | ||
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_NOT_FOUND); | ||
} catch (UnauthorizedException $exception) { | ||
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_UNAUTHORIZED); | ||
} catch (MethodNotAllowedException $exception) { | ||
return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED); | ||
} catch (BadRequestException $exception) { | ||
return $this->errorResponse($exception->getMessages(), StatusCodeInterface::STATUS_BAD_REQUEST); | ||
} catch (MailException | RuntimeException | Exception $exception) { | ||
return $this->errorResponse($exception->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* @throws RuntimeException | ||
*/ | ||
private function isGetCollectionRequest(ServerRequestInterface $request, array $config): bool | ||
{ | ||
if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) { | ||
return false; | ||
} | ||
|
||
if (! array_key_exists(MetadataMap::class, $config)) { | ||
throw new RuntimeException( | ||
sprintf('Unable to load %s from config.', MetadataMap::class) | ||
); | ||
} | ||
|
||
/** @var RouteResult $routeResult */ | ||
$routeResult = $request->getAttribute(RouteResult::class); | ||
$routeName = $routeResult->getMatchedRouteName(); | ||
|
||
$halConfig = null; | ||
foreach ($config[MetadataMap::class] as $cfg) { | ||
if ($cfg['route'] === $routeName) { | ||
$halConfig = $cfg; | ||
break; | ||
} | ||
} | ||
|
||
return is_array($halConfig) | ||
&& array_key_exists('__class__', $halConfig) | ||
&& $halConfig['__class__'] === RouteBasedCollectionMetadata::class; | ||
} | ||
} |