UploadController::upload()   B
last analyzed

Complexity

Conditions 8
Paths 48

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 8
eloc 31
c 4
b 1
f 0
nc 48
nop 0
dl 0
loc 46
rs 8.1795
1
<?php
2
3
namespace UniSharp\LaravelFilemanager\Controllers;
4
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Support\Facades\Log;
7
use UniSharp\LaravelFilemanager\Lfm;
8
9
class UploadController extends LfmController
10
{
11
    protected $errors;
12
13
    public function __construct()
14
    {
15
        parent::__construct();
16
        $this->errors = [];
17
    }
18
19
    /**
20
     * Upload files
21
     *
22
     * @param void
23
     *
24
     * @return JsonResponse
25
     */
26
    public function upload()
27
    {
28
        $uploaded_files = request()->file('upload');
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

28
        $uploaded_files = /** @scrutinizer ignore-call */ request()->file('upload');
Loading history...
29
        $error_bag = [];
30
        $new_filename = null;
31
32
        foreach (is_array($uploaded_files) ? $uploaded_files : [$uploaded_files] as $file) {
33
            try {
34
                $this->lfm->validateUploadedFile($file);
0 ignored issues
show
Bug Best Practice introduced by
The property lfm does not exist on UniSharp\LaravelFilemana...ollers\UploadController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method validateUploadedFile() does not exist on null. ( Ignorable by Annotation )

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

34
                $this->lfm->/** @scrutinizer ignore-call */ 
35
                            validateUploadedFile($file);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
36
                $new_filename = $this->lfm->upload($file);
37
            } catch (\Exception $e) {
38
                Log::error($e->getMessage(), [
39
                    'file' => $e->getFile(),
40
                    'line' => $e->getLine(),
41
                    'trace' => $e->getTraceAsString()
42
                ]);
43
                array_push($error_bag, $e->getMessage());
44
            } catch (\Error $e) {
45
                Log::error($e->getMessage(), [
46
                    'file' => $e->getFile(),
47
                    'line' => $e->getLine(),
48
                    'trace' => $e->getTraceAsString()
49
                ]);
50
                array_push($error_bag, 'Some error occured during uploading.');
51
            }
52
        }
53
54
        if (is_array($uploaded_files)) {
55
            $response = count($error_bag) > 0 ? $error_bag : parent::$success_response;
56
        } else { // upload via ckeditor5 expects json responses
57
            if (is_null($new_filename)) {
58
                $response = [
59
                    'error' => [ 'message' =>  $error_bag[0] ]
60
                ];
61
            } else {
62
                $url = $this->lfm->setName($new_filename)->url();
63
64
                $response = [
65
                    'url' => $url,
66
                    'uploaded' => $url
67
                ];
68
            }
69
        }
70
71
        return response()->json($response);
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

71
        return /** @scrutinizer ignore-call */ response()->json($response);
Loading history...
72
    }
73
}
74