Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
IsTestWritable
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/** @noinspection PhpUnhandledExceptionInspection */
3declare(strict_types=1);
4
5// TODO unit test
6
7use Psr\Http\Message\ResponseInterface;
8use Slim\Exception\HttpBadRequestException;
9use Slim\Exception\HttpForbiddenException;
10use Slim\Http\ServerRequest as Request;
11use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
12use Slim\Routing\RouteContext;
13
14class IsTestWritable {
15
16  function __invoke(Request $request, RequestHandler $handler): ResponseInterface {
17    $routeContext = RouteContext::fromRequest($request);
18    $route = $routeContext->getRoute();
19    $params = $route->getArguments();
20
21    if (!isset($params['test_id']) or ((int) $params['test_id'] < 1)) {
22      throw new HttpBadRequestException($request, "No valid test-Id: {$params['test_id']}");
23    }
24
25    /* @var $authToken AuthToken */
26    $authToken = $request->getAttribute('AuthToken');
27
28    $sessionDAO = new SessionDAO();
29
30    if (!$sessionDAO->ownsTest($authToken->getToken(), $params['test_id'])) {
31      throw new HttpForbiddenException($request, "Access to test {$params['test_id']} is not provided.");
32    }
33
34    return $handler->handle($request);
35  }
36}