Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HttpMethodValidator.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
16 use Psr\Log\LoggerInterface;
17 
22 {
26  private $map;
27 
31  private $log;
32 
37  public function __construct(
38  HttpMethodMap $map,
39  LoggerInterface $logger
40  ) {
41  $this->map = $map;
42  $this->log = $logger;
43  }
44 
54  private function throwException(
55  Http $request,
56  ActionInterface $action
57  ): void {
58  $uri = $request->getRequestUri();
59  $method = $request->getMethod();
60  if ($action instanceof InterceptorInterface) {
61  $actionClass = get_parent_class($action);
62  } else {
63  $actionClass = get_class($action);
64  }
65  $this->log->debug(
66  "URI '$uri'' cannot be accessed with $method method ($actionClass)"
67  );
68 
69  throw new InvalidRequestException(
70  new NotFoundException(new Phrase('Page not found.'))
71  );
72  }
73 
77  public function validate(
79  ActionInterface $action
80  ): void {
81  if ($request instanceof Http) {
82  $method = $request->getMethod();
83  $map = $this->map->getMap();
84  //If we don't have an interface for the HTTP method or
85  //the action has HTTP method limitations and doesn't allow the
86  //received one then the request is invalid.
87  if (!array_key_exists($method, $map)
88  || (array_intersect($map, class_implements($action, true))
89  && !$action instanceof $map[$method]
90  )
91  ) {
92  $this->throwException($request, $action);
93  }
94  }
95  }
96 }
validate(RequestInterface $request, ActionInterface $action)
$logger
__construct(HttpMethodMap $map, LoggerInterface $logger)
$method
Definition: info.phtml:13