Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
StartUploadEndpoint
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace Olz\Api\Endpoints;
4
5use Olz\Api\OlzTypedEndpoint;
6
7/**
8 * @extends OlzTypedEndpoint<
9 *   array{
10 *     suffix?: ?non-empty-string,
11 *   },
12 *   array{
13 *     status: 'OK'|'ERROR',
14 *     id?: ?non-empty-string
15 *   }
16 * >
17 */
18class StartUploadEndpoint extends OlzTypedEndpoint {
19    public const MAX_LOOP = 100;
20
21    protected function handle(mixed $input): mixed {
22        $this->checkPermission('any');
23
24        $data_path = $this->envUtils()->getDataPath();
25        $temp_path = "{$data_path}temp/";
26        if (!is_dir($temp_path)) {
27            mkdir($temp_path, 0o777, true);
28        }
29        $suffix = $input['suffix'] ?? '';
30        $upload_id = '';
31        $continue = true;
32        for ($i = 0; $i < self::MAX_LOOP && $continue; $i++) {
33            $upload_id = $this->uploadUtils()->getRandomUploadId($suffix);
34            $upload_path = "{$temp_path}{$upload_id}";
35            if (!is_file($upload_path)) {
36                file_put_contents($upload_path, '');
37                $continue = false;
38            }
39        }
40        if ($continue) {
41            $this->log()->error("Could not start upload. Finding unique ID failed. Maximum number of loops exceeded.");
42            return ['status' => 'ERROR', 'id' => null];
43        }
44        return [
45            'status' => 'OK',
46            'id' => $upload_id ?: '-',
47        ];
48    }
49}