Passed
Push — master ( 3fc28d...5a5320 )
by Stream
04:22
created

ItemsController::doMove()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 23
c 0
b 0
f 0
nc 13
nop 0
dl 0
loc 36
rs 8.9297
1
<?php
2
3
namespace UniSharp\LaravelFilemanager\Controllers;
4
5
use Illuminate\Support\Facades\Storage;
6
use UniSharp\LaravelFilemanager\Events\FileIsMoving;
7
use UniSharp\LaravelFilemanager\Events\FileWasMoving;
8
use UniSharp\LaravelFilemanager\Events\FolderIsMoving;
9
use UniSharp\LaravelFilemanager\Events\FolderWasMoving;
10
11
class ItemsController extends LfmController
12
{
13
    /**
14
     * Get the images to load for a selected folder.
15
     *
16
     * @return mixed
17
     */
18
    public function getItems()
19
    {
20
        $currentPage = self::getCurrentPageFromRequest();
21
22
        $perPage = $this->helper->getPaginationPerPage();
0 ignored issues
show
Bug introduced by
The method getPaginationPerPage() 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

22
        /** @scrutinizer ignore-call */ 
23
        $perPage = $this->helper->getPaginationPerPage();

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 helper does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
23
        $items = array_merge($this->lfm->folders(), $this->lfm->files());
0 ignored issues
show
Bug Best Practice introduced by
The property lfm does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method folders() 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

23
        $items = array_merge($this->lfm->/** @scrutinizer ignore-call */ folders(), $this->lfm->files());

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...
24
25
        return [
26
            'items' => array_map(function ($item) {
27
                return $item->fill()->attributes;
28
            }, array_slice($items, ($currentPage - 1) * $perPage, $perPage)),
29
            'paginator' => [
30
                'current_page' => $currentPage,
31
                'total' => count($items),
32
                'per_page' => $perPage,
33
            ],
34
            'display' => $this->helper->getDisplayMode(),
35
            'working_dir' => $this->lfm->path('working_dir'),
36
        ];
37
    }
38
39
    public function move()
40
    {
41
        $items = 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

41
        $items = /** @scrutinizer ignore-call */ request('items');
Loading history...
42
        $folder_types = array_filter(['user', 'share'], function ($type) {
43
            return $this->helper->allowFolderType($type);
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
44
        });
45
        return view('laravel-filemanager::move')
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

45
        return /** @scrutinizer ignore-call */ view('laravel-filemanager::move')
Loading history...
46
            ->with([
47
                'root_folders' => array_map(function ($type) use ($folder_types) {
48
                    $path = $this->lfm->dir($this->helper->getRootFolder($type));
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property lfm does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
49
50
                    return (object) [
51
                        'name' => trans('laravel-filemanager::lfm.title-' . $type),
0 ignored issues
show
Bug introduced by
Are you sure the usage of trans('laravel-filemanager::lfm.title-' . $type) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The call to trans() has too many arguments starting with 'laravel-filemanager::lfm.title-' . $type. ( Ignorable by Annotation )

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

51
                        'name' => /** @scrutinizer ignore-call */ trans('laravel-filemanager::lfm.title-' . $type),

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
52
                        'url' => $path->path('working_dir'),
53
                        'children' => $path->folders(),
54
                        'has_next' => ! ($type == end($folder_types)),
55
                    ];
56
                }, $folder_types),
57
            ])
58
            ->with('items', $items);
59
    }
60
61
    public function doMove()
62
    {
63
        $target = $this->helper->input('goToFolder');
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
64
        $items = $this->helper->input('items');
65
66
        foreach ($items as $item) {
67
            $old_file = $this->lfm->pretty($item);
0 ignored issues
show
Bug Best Practice introduced by
The property lfm does not exist on UniSharp\LaravelFilemana...rollers\ItemsController. Since you implemented __get, consider adding a @property annotation.
Loading history...
68
            $is_directory = $old_file->isDirectory();
69
70
            $file = $this->lfm->setName($item);
71
72
            if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) {
73
                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

73
                /** @scrutinizer ignore-call */ 
74
                abort(404);
Loading history...
74
            }
75
76
            $old_path = $old_file->path();
77
78
            if ($old_file->hasThumb()) {
79
                $new_file = $this->lfm->setName($item)->thumb()->dir($target);
80
                if ($is_directory) {
81
                    event(new FolderIsMoving($old_file->path(), $new_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

81
                    /** @scrutinizer ignore-call */ 
82
                    event(new FolderIsMoving($old_file->path(), $new_file->path()));
Loading history...
82
                } else {
83
                    event(new FileIsMoving($old_file->path(), $new_file->path()));
84
                }
85
                $this->lfm->setName($item)->thumb()->move($new_file);
86
            }
87
            $new_file = $this->lfm->setName($item)->dir($target);
88
            $this->lfm->setName($item)->move($new_file);
89
            if ($is_directory) {
90
                event(new FolderWasMoving($old_path, $new_file->path()));
91
            } else {
92
                event(new FileWasMoving($old_path, $new_file->path()));
93
            }
94
        };
95
96
        return parent::$success_response;
97
    }
98
99
    private static function getCurrentPageFromRequest()
100
    {
101
        $currentPage = (int) request()->get('page', 1);
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

101
        $currentPage = (int) /** @scrutinizer ignore-call */ request()->get('page', 1);
Loading history...
102
        $currentPage = $currentPage < 1 ? 1 : $currentPage;
103
104
        return $currentPage;
105
    }
106
}
107