Editor::fileUpload()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 21
c 4
b 1
f 0
dl 0
loc 32
rs 9.584
cc 4
nc 6
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Controllers\Api;
6
7
use AbterPhp\Admin\Constant\Env;
8
use Opulence\Environments\Environment;
9
use Opulence\Http\Requests\UploadException;
10
use Opulence\Http\Responses\JsonResponse;
11
use Opulence\Http\Responses\Response;
12
use Opulence\Http\Responses\ResponseHeaders;
13
use Opulence\Routing\Controller;
14
15
class Editor extends Controller
16
{
17
    /**
18
     * @return Response
19
     * @throws UploadException
20
     */
21
    public function fileUpload(): Response
22
    {
23
        $authHeader     = $this->request->getHeaders()->get('authorization');
24
        $actualClientId = strpos($authHeader, ' ') ? explode(' ', $authHeader)[1] : '';
0 ignored issues
show
Bug introduced by
It seems like $authHeader can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
        $actualClientId = strpos($authHeader, ' ') ? explode(' ', /** @scrutinizer ignore-type */ $authHeader)[1] : '';
Loading history...
Bug introduced by
It seems like $authHeader can also be of type null; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
        $actualClientId = strpos(/** @scrutinizer ignore-type */ $authHeader, ' ') ? explode(' ', $authHeader)[1] : '';
Loading history...
25
26
        $expectedClientId = Environment::getVar(Env::EDITOR_CLIENT_ID);
27
        if ($actualClientId != $expectedClientId) {
28
            return $this->sendJson(ResponseHeaders::HTTP_FORBIDDEN);
29
        }
30
31
        $image = $this->request->getFiles()->get('image');
32
        if ($image->hasErrors()) {
33
            return $this->sendJson(ResponseHeaders::HTTP_INTERNAL_SERVER_ERROR);
34
        }
35
36
        $pathInfo  = pathinfo($image->getTempFilename());
37
        $baseName  = mb_substr(basename($image->getPathname()), 3);
38
        $extension = $pathInfo['extension'] ?? '';
39
        $filename  = sprintf('%s.%s', $baseName, $extension);
40
41
        $basePath = Environment::getVar(Env::EDITOR_BASE_PATH);
42
        $path     = Environment::getVar(Env::DIR_MEDIA) . $basePath;
43
        $url      = sprintf(
44
            '%s/%s/%s',
45
            rtrim(Environment::getVar(Env::MEDIA_BASE_URL), DIRECTORY_SEPARATOR),
0 ignored issues
show
Bug introduced by
It seems like Opulence\Environments\En...nt\Env::MEDIA_BASE_URL) can also be of type null; however, parameter $string of rtrim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            rtrim(/** @scrutinizer ignore-type */ Environment::getVar(Env::MEDIA_BASE_URL), DIRECTORY_SEPARATOR),
Loading history...
46
            trim($basePath, DIRECTORY_SEPARATOR),
0 ignored issues
show
Bug introduced by
It seems like $basePath can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            trim(/** @scrutinizer ignore-type */ $basePath, DIRECTORY_SEPARATOR),
Loading history...
47
            $filename
48
        );
49
50
        $image->move($path, $filename);
51
52
        return $this->sendJson(ResponseHeaders::HTTP_OK, ['url' => $url]);
53
    }
54
55
    /**
56
     * @param int        $status
57
     * @param array|null $data
58
     *
59
     * @return Response
60
     */
61
    protected function sendJson(int $status = ResponseHeaders::HTTP_OK, array $data = null): Response
62
    {
63
        $body = [
64
            'success' => $status == ResponseHeaders::HTTP_OK,
65
            'status'  => $status,
66
        ];
67
68
        if ($data) {
69
            $body['data'] = $data;
70
        }
71
72
        $response = new JsonResponse($body, $status);
73
74
        $response->send();
75
76
        return $response;
77
    }
78
}
79