| Total Complexity | 5 |
| Total Lines | 32 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | final class AccessChecker implements MiddlewareInterface |
||
| 16 | { |
||
| 17 | private ResponseFactoryInterface $responseFactory; |
||
| 18 | private UserService $userService; |
||
| 19 | private ?string $permission = null; |
||
| 20 | |||
| 21 | public function __construct( |
||
| 22 | ResponseFactoryInterface $responseFactory, |
||
| 23 | UserService $userService |
||
| 24 | ) { |
||
| 25 | $this->responseFactory = $responseFactory; |
||
| 26 | $this->userService = $userService; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 30 | { |
||
| 31 | if ($this->permission === null) { |
||
| 32 | throw new \InvalidArgumentException('Permission not set.'); |
||
| 33 | } |
||
| 34 | |||
| 35 | if (!$this->userService->hasPermission($this->permission)) { |
||
| 36 | return $this->responseFactory->createResponse(Status::FORBIDDEN); |
||
| 37 | } |
||
| 38 | |||
| 39 | return $handler->handle($request); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function withPermission(string $permission): self |
||
| 47 | } |
||
| 48 | } |
||
| 49 |