Completed
Pull Request — master (#831)
by
unknown
03:34
created

UploadController::upload()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 24
nop 0
dl 0
loc 31
rs 8.6506
c 0
b 0
f 0
1
<?php
2
3
namespace Xuandung38\LaravelFilemanager\Controllers;
4
5
use Illuminate\Support\Facades\Log;
6
use Xuandung38\LaravelFilemanager\Events\ImageIsUploading;
7
use Xuandung38\LaravelFilemanager\Events\ImageWasUploaded;
8
use Xuandung38\LaravelFilemanager\Lfm;
9
10
class UploadController extends LfmController
11
{
12
    protected $errors;
13
14
    public function __construct()
15
    {
16
        parent::__construct();
17
        $this->errors = [];
18
    }
19
20
    /**
21
     * Upload files
22
     *
23
     * @param void
24
     * @return string
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
                $new_filename = $this->lfm->upload($file);
0 ignored issues
show
Bug introduced by
The method upload() 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
                /** @scrutinizer ignore-call */ 
35
                $new_filename = $this->lfm->upload($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...
Bug Best Practice introduced by
The property lfm does not exist on Xuandung38\LaravelFilema...ollers\UploadController. Since you implemented __get, consider adding a @property annotation.
Loading history...
35
            } catch (\Exception $e) {
36
                Log::error($e->getMessage(), [
37
                    'file' => $e->getFile(),
38
                    'line' => $e->getLine(),
39
                    'trace' => $e->getTraceAsString()
40
                ]);
41
                array_push($error_bag, $e->getMessage());
42
            }
43
        }
44
45
        if (is_array($uploaded_files)) {
46
            $response = count($error_bag) > 0 ? $error_bag : parent::$success_response;
47
        } else { // upload via ckeditor 'Upload' tab
48
            if (is_null($new_filename)) {
49
                $response = $error_bag[0];
50
            } else {
51
                $response = view(Lfm::PACKAGE_NAME . '::use')
0 ignored issues
show
Bug introduced by
The function view 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

51
                $response = /** @scrutinizer ignore-call */ view(Lfm::PACKAGE_NAME . '::use')
Loading history...
52
                    ->withFile($this->lfm->setName($new_filename)->url());
53
            }
54
        }
55
56
        return $response;
57
    }
58
}
59