Passed
Push — master ( 4f53f1...616a79 )
by Peter
02:36
created

Editor::sendJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 8
c 1
b 1
f 0
dl 0
loc 16
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Controllers\Api;
6
7
use AbterPhp\Admin\Config\Routes;
8
use AbterPhp\Admin\Constant\Env;
9
use AbterPhp\Framework\Constant\Env as FrameworkEnv;
10
use Opulence\Environments\Environment;
11
use Opulence\Http\Responses\JsonResponse;
12
use Opulence\Http\Responses\Response;
13
use Opulence\Http\Responses\ResponseHeaders;
14
use Opulence\Routing\Controller;
15
16
class Editor extends Controller
17
{
18
    /**
19
     * @return Response
20
     */
21
    public function fileUpload(): Response
22
    {
23
        $actualClientId = explode(' ', $this->request->getHeaders()->get('authorization'))[1];
24
25
        $expectedClientId = Environment::getVar(FrameworkEnv::CRYPTO_CLIENT_ID);
26
        if ($actualClientId != $expectedClientId) {
27
            return $this->sendJson(ResponseHeaders::HTTP_FORBIDDEN);
28
        }
29
30
        $image = $this->request->getFiles()->get('image');
31
        if ($image->hasErrors()) {
32
            return $this->sendJson(ResponseHeaders::HTTP_INTERNAL_SERVER_ERROR);
33
        }
34
35
        $pathinfo  = pathinfo($image->getTempFilename());
36
        $basePath  = mb_substr(basename($image->getPathname()), 3);
37
        $extension = $pathinfo['extension'] ?? '';
38
        $filename  = sprintf('%s.%s', $basePath, $extension);
39
40
        $path = Environment::getVar(FrameworkEnv::DIR_UPLOAD);
0 ignored issues
show
Bug introduced by
The constant AbterPhp\Framework\Constant\Env::DIR_UPLOAD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
41
        $url  = Environment::getVar(Env::UPLOAD_BASE_URL) . '/' . $filename;
42
43
        $image->move($path, $filename);
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type null; however, parameter $targetDirectory of Opulence\Http\Requests\UploadedFile::move() 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

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