vendor/can/rest/src/Can/RestBundle/EventListener/RequestMethodListener.php line 51

Open in your IDE?
  1. <?php
  2. namespace Can\RestBundle\EventListener;
  3. use Can\RestBundle\CanRestBundle;
  4. use Can\RestBundle\Http\Exception\NotImplementedHttpException;
  5. use Can\RestBundle\Http\Method;
  6. use Can\RestBundle\Mapping\MappingException;
  7. use Can\RestBundle\Mapping\MetadataStore;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  10. use Exception;
  11. /**
  12.  * When a request method is received that is unrecognized or not implemented
  13.  * by an origin server, the origin server SHOULD respond with the 501 (Not
  14.  * Implemented) status code.  When a request method is received that is known
  15.  * by an origin server but not allowed for the target resource, the origin
  16.  * server SHOULD respond with the 405 (Method Not Allowed) status code.
  17.  *
  18.  * @see https://tools.ietf.org/html/rfc7231#section-4.1
  19.  *
  20.  * @group core
  21.  *
  22.  * @package can/rest-bundle
  23.  * @author lechecacharro <lechecacharro@gmail.com>
  24.  */
  25. class RequestMethodListener
  26. {
  27.     /**
  28.      * @var MetadataStore
  29.      */
  30.     protected $store;
  31.     /**
  32.      * RequiredBodyListener constructor.
  33.      *
  34.      * @param MetadataStore $store
  35.      */
  36.     public function __construct(MetadataStore $store)
  37.     {
  38.         $this->store $store;
  39.     }
  40.     /**
  41.      * @param RequestEvent $event
  42.      */
  43.     public function onRequest(RequestEvent $event): void
  44.     {
  45.         if (! $event->isMasterRequest()) {
  46.             return;
  47.         }
  48.         $request $event->getRequest();
  49.         if (! $request->attributes->get(CanRestBundle::ATTR_SERVICE_ZONE) ||
  50.             ! $request->attributes->has(CanRestBundle::ATTR_SERVICE_NODE)) {
  51.             return;
  52.         }
  53.         try {
  54.             Method::getMethod($request->getMethod());
  55.         } catch (Exception $e) {
  56.             // When a request method is received that is unrecognized
  57.             // or not implemented by an origin server, the origin server
  58.             // SHOULD respond with the 501 (Not Implemented) status code
  59.             throw new NotImplementedHttpException(sprintf('Method %s is not implemented'$request->getMethod()));
  60.         }
  61.         $serviceNode $request->attributes->get(CanRestBundle::ATTR_SERVICE_NODE);
  62.         try {
  63.             $metadata $this->store->getMetadataFor($serviceNode);
  64.         } catch (MappingException $f) {
  65.             // This case is handled by the router component
  66.             return;
  67.         }
  68.         if (! $metadata->supportsOperation($request->getMethod())) {
  69.             // When a request method is received that is known by an origin
  70.             // server but not allowed for the target resource, the origin
  71.             // server SHOULD respond with the 405 (Method Not Allowed)
  72.             // status code
  73.             throw new MethodNotAllowedHttpException($metadata->getSupportedMethodNames());
  74.         }
  75.     }
  76. }