DeleteController::getDelete()   B
last analyzed

Complexity

Conditions 10
Paths 50

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
eloc 34
c 2
b 1
f 0
nc 50
nop 0
dl 0
loc 58
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace UniSharp\LaravelFilemanager\Controllers;
4
5
use Illuminate\Support\Facades\Storage;
6
use UniSharp\LaravelFilemanager\Events\FileIsDeleting;
7
use UniSharp\LaravelFilemanager\Events\FileWasDeleted;
8
use UniSharp\LaravelFilemanager\Events\FolderIsDeleting;
9
use UniSharp\LaravelFilemanager\Events\FolderWasDeleted;
10
use UniSharp\LaravelFilemanager\Events\ImageIsDeleting;
11
use UniSharp\LaravelFilemanager\Events\ImageWasDeleted;
12
13
class DeleteController extends LfmController
14
{
15
    /**
16
     * Delete image and associated thumbnail.
17
     *
18
     * @return mixed
19
     */
20
    public function getDelete()
21
    {
22
        $item_names = request('items');
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

22
        $item_names = /** @scrutinizer ignore-call */ request('items');
Loading history...
23
        $errors = [];
24
25
        foreach ($item_names as $name_to_delete) {
26
            $file = $this->lfm->setName($name_to_delete);
0 ignored issues
show
Bug introduced by
The method setName() 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

26
            /** @scrutinizer ignore-call */ 
27
            $file = $this->lfm->setName($name_to_delete);

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 UniSharp\LaravelFilemana...ollers\DeleteController. Since you implemented __get, consider adding a @property annotation.
Loading history...
27
28
            if ($file->isDirectory()) {
29
                event(new FolderIsDeleting($file->path('absolute')));
0 ignored issues
show
Bug introduced by
The function event 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

29
                /** @scrutinizer ignore-call */ 
30
                event(new FolderIsDeleting($file->path('absolute')));
Loading history...
30
            } else {
31
                event(new FileIsDeleting($file->path('absolute')));
32
                event(new ImageIsDeleting($file->path('absolute')));
33
            }
34
35
            if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) {
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...ollers\DeleteController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method config() 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

35
            if (!Storage::disk($this->helper->/** @scrutinizer ignore-call */ config('disk'))->exists($file->path('storage'))) {

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...
36
                abort(404);
0 ignored issues
show
Bug introduced by
The function abort 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

36
                /** @scrutinizer ignore-call */ 
37
                abort(404);
Loading history...
37
            }
38
39
            $file_to_delete = $this->lfm->pretty($name_to_delete);
40
            $file_path = $file_to_delete->path('absolute');
41
42
            if (is_null($name_to_delete)) {
43
                array_push($errors, parent::error('folder-name'));
44
                continue;
45
            }
46
47
            if (! $this->lfm->setName($name_to_delete)->exists()) {
48
                array_push($errors, parent::error('folder-not-found', ['folder' => $file_path]));
49
                continue;
50
            }
51
52
            if ($this->lfm->setName($name_to_delete)->isDirectory()) {
53
                if (! $this->lfm->setName($name_to_delete)->directoryIsEmpty()) {
54
                    array_push($errors, parent::error('delete-folder'));
55
                    continue;
56
                }
57
58
                $this->lfm->setName($name_to_delete)->delete();
59
60
                event(new FolderWasDeleted($file_path));
61
            } else {
62
                if ($file_to_delete->isImage()) {
63
                    $this->lfm->setName($name_to_delete)->thumb()->delete();
64
                }
65
66
                $this->lfm->setName($name_to_delete)->delete();
67
68
                event(new FileWasDeleted($file_path));
69
                event(new ImageWasDeleted($file_path));
70
            }
71
        }
72
73
        if (count($errors) > 0) {
74
            return response()->json($errors, 400);
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

74
            return /** @scrutinizer ignore-call */ response()->json($errors, 400);
Loading history...
75
        }
76
77
        return parent::$success_response;
78
    }
79
}
80