Passed
Push — master ( e56b5f...1604a1 )
by Peter
03:14
created

Editor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 33
c 1
b 0
f 0
dl 0
loc 70
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fileUpload() 0 52 3
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\Framework\Constant\Env as FrameworkEnv;
9
use AbterPhp\Website\Constant\Env as WebsiteEnv;
0 ignored issues
show
Bug introduced by
The type AbterPhp\Website\Constant\Env was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    /** @var Routes */
19
    protected $routes;
20
21
    /**
22
     * Editor constructor.
23
     *
24
     * @param Routes $routes
25
     */
26
    public function __construct(Routes $routes)
27
    {
28
        $this->routes = $routes;
29
    }
30
31
    /**
32
     * @return Response
33
     */
34
    public function fileUpload(): Response
35
    {
36
        $actualClientId = explode(' ', $this->request->getHeaders()->get('authorization'))[1];
37
38
        $expectedClientId = Environment::getVar(FrameworkEnv::CRYPTO_CLIENT_ID);
0 ignored issues
show
Bug introduced by
The constant AbterPhp\Framework\Constant\Env::CRYPTO_CLIENT_ID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
        if ($actualClientId != $expectedClientId) {
40
            $response = new JsonResponse(
41
                [
42
                    'success' => false,
43
                    'status'  => ResponseHeaders::HTTP_FORBIDDEN,
44
                ],
45
                ResponseHeaders::HTTP_FORBIDDEN
46
            );
47
            $response->send();
48
49
            return $response;
50
        }
51
52
        $image = $this->request->getFiles()->get('image');
53
        if ($image->hasErrors()) {
54
            $response = new JsonResponse(
55
                [
56
                    'success' => false,
57
                    'status'  => ResponseHeaders::HTTP_INTERNAL_SERVER_ERROR,
58
                ],
59
                ResponseHeaders::HTTP_INTERNAL_SERVER_ERROR
60
            );
61
            $response->send();
62
63
            return $response;
64
        }
65
66
        $basePath  = mb_substr(basename($image->getPathname()), 3);
67
        $extension = pathinfo($image->getTempFilename())['extension'];
68
        $filename  = sprintf('%s.%s', $basePath, $extension);
69
70
        $path = sprintf('%s/editor-file-upload', Environment::getVar(FrameworkEnv::DIR_PUBLIC));
71
        $url  = Environment::getVar(WebsiteEnv::WEBSITE_BASE_URL) . 'editor-file-upload/' . $filename;
72
73
        $image->move($path, $filename);
74
75
        $response = new JsonResponse(
76
            [
77
                'data'    => ['url' => $url],
78
                'success' => true,
79
                'status'  => ResponseHeaders::HTTP_OK,
80
            ],
81
            ResponseHeaders::HTTP_OK
82
        );
83
        $response->send();
84
85
        return $response;
86
    }
87
}
88