|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Api\App\Handler; |
| 6 | + |
| 7 | +use Api\App\Exception\BadRequestException; |
| 8 | +use Api\App\Exception\ConflictException; |
| 9 | +use Api\App\Exception\ExpiredException; |
| 10 | +use Api\App\Exception\ForbiddenException; |
| 11 | +use Api\App\Exception\MethodNotAllowedException; |
| 12 | +use Api\App\Exception\NotFoundException; |
| 13 | +use Api\App\Exception\UnauthorizedException; |
| 14 | +use Dot\Mail\Exception\MailException; |
| 15 | +use Exception; |
| 16 | +use Fig\Http\Message\RequestMethodInterface; |
| 17 | +use Fig\Http\Message\StatusCodeInterface; |
| 18 | +use Mezzio\Hal\Metadata\MetadataMap; |
| 19 | +use Mezzio\Hal\Metadata\RouteBasedCollectionMetadata; |
| 20 | +use Mezzio\Hal\ResourceGenerator\Exception\OutOfBoundsException; |
| 21 | +use Mezzio\Router\RouteResult; |
| 22 | +use Psr\Http\Message\ResponseInterface; |
| 23 | +use Psr\Http\Message\ServerRequestInterface; |
| 24 | +use RuntimeException; |
| 25 | + |
| 26 | +use function array_key_exists; |
| 27 | +use function is_array; |
| 28 | +use function method_exists; |
| 29 | +use function sprintf; |
| 30 | +use function strtolower; |
| 31 | + |
| 32 | +trait HandlerTrait |
| 33 | +{ |
| 34 | + use ResponseTrait; |
| 35 | + |
| 36 | + /** |
| 37 | + * @throws Exception |
| 38 | + */ |
| 39 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 40 | + { |
| 41 | + try { |
| 42 | + $method = strtolower($request->getMethod()); |
| 43 | + if ($this->isGetCollectionRequest($request, $this->config)) { |
| 44 | + $method = 'getCollection'; |
| 45 | + } |
| 46 | + |
| 47 | + if (! method_exists($this, $method)) { |
| 48 | + throw new MethodNotAllowedException( |
| 49 | + sprintf('Method %s is not implemented for the requested resource.', $method) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + return $this->$method($request); |
| 54 | + } catch (ConflictException $exception) { |
| 55 | + return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_CONFLICT); |
| 56 | + } catch (ForbiddenException $exception) { |
| 57 | + return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_FORBIDDEN); |
| 58 | + } catch (ExpiredException $exception) { |
| 59 | + return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_GONE); |
| 60 | + } catch (OutOfBoundsException | NotFoundException $exception) { |
| 61 | + return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_NOT_FOUND); |
| 62 | + } catch (UnauthorizedException $exception) { |
| 63 | + return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_UNAUTHORIZED); |
| 64 | + } catch (MethodNotAllowedException $exception) { |
| 65 | + return $this->errorResponse($exception->getMessage(), StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED); |
| 66 | + } catch (BadRequestException $exception) { |
| 67 | + return $this->errorResponse($exception->getMessages(), StatusCodeInterface::STATUS_BAD_REQUEST); |
| 68 | + } catch (MailException | RuntimeException | Exception $exception) { |
| 69 | + return $this->errorResponse($exception->getMessage()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @throws RuntimeException |
| 75 | + */ |
| 76 | + private function isGetCollectionRequest(ServerRequestInterface $request, array $config): bool |
| 77 | + { |
| 78 | + if ($request->getMethod() !== RequestMethodInterface::METHOD_GET) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + if (! array_key_exists(MetadataMap::class, $config)) { |
| 83 | + throw new RuntimeException( |
| 84 | + sprintf('Unable to load %s from config.', MetadataMap::class) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + /** @var RouteResult $routeResult */ |
| 89 | + $routeResult = $request->getAttribute(RouteResult::class); |
| 90 | + $routeName = $routeResult->getMatchedRouteName(); |
| 91 | + |
| 92 | + $halConfig = null; |
| 93 | + foreach ($config[MetadataMap::class] as $cfg) { |
| 94 | + if ($cfg['route'] === $routeName) { |
| 95 | + $halConfig = $cfg; |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return is_array($halConfig) |
| 101 | + && array_key_exists('__class__', $halConfig) |
| 102 | + && $halConfig['__class__'] === RouteBasedCollectionMetadata::class; |
| 103 | + } |
| 104 | +} |
0 commit comments