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

DeleteController::getDelete()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 24
nc 14
nop 0
dl 0
loc 42
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Xuandung38\LaravelFilemanager\Controllers;
4
5
use Xuandung38\LaravelFilemanager\Events\ImageIsDeleting;
6
use Xuandung38\LaravelFilemanager\Events\ImageWasDeleted;
7
8
class DeleteController extends LfmController
9
{
10
    /**
11
     * Delete image and associated thumbnail.
12
     *
13
     * @return mixed
14
     */
15
    public function getDelete()
16
    {
17
        $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

17
        $item_names = /** @scrutinizer ignore-call */ request('items');
Loading history...
18
        $errors = [];
19
20
        foreach ($item_names as $name_to_delete) {
21
            $file_to_delete = $this->lfm->pretty($name_to_delete);
0 ignored issues
show
Bug introduced by
The method pretty() 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

21
            /** @scrutinizer ignore-call */ 
22
            $file_to_delete = $this->lfm->pretty($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 Xuandung38\LaravelFilema...ollers\DeleteController. Since you implemented __get, consider adding a @property annotation.
Loading history...
22
            $file_path = $file_to_delete->path();
23
24
            event(new ImageIsDeleting($file_path));
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

24
            /** @scrutinizer ignore-call */ 
25
            event(new ImageIsDeleting($file_path));
Loading history...
25
26
            if (is_null($name_to_delete)) {
27
                array_push($errors, parent::error('folder-name'));
28
                continue;
29
            }
30
31
            if (! $this->lfm->setName($name_to_delete)->exists()) {
32
                array_push($errors, parent::error('folder-not-found', ['folder' => $file_path]));
33
                continue;
34
            }
35
36
            if ($this->lfm->setName($name_to_delete)->isDirectory()) {
37
                if (! $this->lfm->setName($name_to_delete)->directoryIsEmpty()) {
38
                    array_push($errors, parent::error('delete-folder'));
39
                    continue;
40
                }
41
            } else {
42
                if ($file_to_delete->isImage()) {
43
                    $this->lfm->setName($name_to_delete)->thumb()->delete();
44
                }
45
            }
46
47
            $this->lfm->setName($name_to_delete)->delete();
48
49
            event(new ImageWasDeleted($file_path));
50
        }
51
52
        if (count($errors) > 0) {
53
            return $errors;
54
        }
55
56
        return parent::$success_response;
57
    }
58
}
59