Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
IsWorkspaceBlocked | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 1 |
__invoke | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | declare(strict_types=1); |
4 | // TODO unit test |
5 | |
6 | use Psr\Http\Message\ResponseInterface; |
7 | use Slim\Exception\HttpException; |
8 | use Slim\Exception\HttpForbiddenException; |
9 | use Slim\Exception\HttpNotFoundException; |
10 | use Slim\Http\ServerRequest as Request; |
11 | use Psr\Http\Server\RequestHandlerInterface as RequestHandler; |
12 | use Slim\Routing\RouteContext; |
13 | |
14 | class IsWorkspaceBlocked { |
15 | function __invoke(Request $request, RequestHandler $handler): ResponseInterface { |
16 | $routeContext = RouteContext::fromRequest($request); |
17 | $route = $routeContext->getRoute(); |
18 | $params = $route->getArguments(); |
19 | |
20 | if (!isset($params['ws_id']) or ((int) $params['ws_id'] < 1)) { |
21 | throw new HttpNotFoundException($request, "No valid workspace: `{$params['ws_id']}`"); |
22 | } |
23 | |
24 | $workspace = new Workspace((int) $params['ws_id']); |
25 | |
26 | if ( |
27 | file_exists($workspace->getWorkspacePath() . '/.lock') |
28 | and (time() - filemtime($workspace->getWorkspacePath() . '/.lock') < 60 * 12) |
29 | ) { |
30 | throw new HttpException($request, 'Workspace blocked by another upload or deletion action.', 409); |
31 | } |
32 | |
33 | file_put_contents($workspace->getWorkspacePath() . '/.lock', '.'); |
34 | register_shutdown_function(function() use ($workspace) { |
35 | unlink($workspace->getWorkspacePath() . '/.lock'); |
36 | }); |
37 | |
38 | return $handler->handle($request); |
39 | } |
40 | } |