Passed
Pull Request — master (#995)
by
unknown
06:59
created

ItemsController::getItems()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 69
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 10
b 0
f 0
nc 1
nop 1
dl 0
loc 69
rs 9.584

How to fix   Long Method   

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\Http\Request;
6
7
use UniSharp\LaravelFilemanager\Events\FileIsMoving;
8
use UniSharp\LaravelFilemanager\Events\FileWasMoving;
9
use UniSharp\LaravelFilemanager\Events\FolderIsMoving;
10
use UniSharp\LaravelFilemanager\Events\FolderWasMoving;
11
12
class ItemsController extends LfmController
13
{
14
    /**
15
     * Get the images to load for a selected folder.
16
     *
17
     * @return mixed
18
     */
19
    public function getItems(Request $request)
20
    {
21
        /*$currentPage = self::getCurrentPageFromRequest();
22
        $keyword = $request->keyword;
23
24
        $perPage = $this->helper->getPaginationPerPage();
25
        $items = array_merge($this->lfm->folders(), $this->lfm->files());
26
27
        $items = array_map(function ($item) {
28
            return $item->fill()->attributes;
29
        }, array_slice($items, ($currentPage - 1) * $perPage, $perPage));
30
31
        $c = count($items);
32
33
        if (!empty($keyword)) {
34
            $items = array_values(array_filter($items, function ($item) use ($keyword) {
35
                if ($this->like_match("%".$keyword."%", $item['name'])) {
36
                    return true;
37
                } else {
38
                    return false;
39
                }
40
            }));
41
        }
42
43
        return [
44
            'items' => array_map(function ($item) {
45
                return $item->fill()->attributes;
46
            }, array_slice($items, ($currentPage - 1) * $perPage, $perPage)),
47
            'items' => $items,
48
            'paginator' => [
49
                'current_page' => $currentPage,
50
                'total' => $c,
51
                'per_page' => $perPage,
52
            ],
53
            'display' => $this->helper->getDisplayMode(),
54
            'working_dir' => $this->lfm->path('working_dir'),
55
        ];*/
56
57
58
        $currentPage = self::getCurrentPageFromRequest();
0 ignored issues
show
Unused Code introduced by
The assignment to $currentPage is dead and can be removed.
Loading history...
59
60
        $perPage = $this->helper->getPaginationPerPage();
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 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

60
        /** @scrutinizer ignore-call */ 
61
        $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...
Unused Code introduced by
The assignment to $perPage is dead and can be removed.
Loading history...
61
        $items = array_merge($this->lfm->folders(), $this->lfm->files());
0 ignored issues
show
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

61
        $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...
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...
62
63
        var_dump($items);exit();
0 ignored issues
show
Security Debugging Code introduced by
var_dump($items) looks like debug code. Are you sure you do not want to remove it?
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
64
        
65
        $keyword = $request->keyword;
0 ignored issues
show
Unused Code introduced by
$keyword = $request->keyword is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
66
67
        if (!empty($keyword)) {
68
            $items = array_values(array_filter($items, function ($item) use ($keyword) {
69
                if ($this->like_match("%".$keyword."%", $item['name'])) {
70
                    return true;
71
                } else {
72
                    return false;
73
                }
74
            }));
75
        }
76
77
        return [
78
            'items' => array_map(function ($item) {
79
                return $item->fill()->attributes;
80
            }, array_slice($items, ($currentPage - 1) * $perPage, $perPage)),
81
            'paginator' => [
82
                'current_page' => $currentPage,
83
                'total' => count($items),
84
                'per_page' => $perPage,
85
            ],
86
            'display' => $this->helper->getDisplayMode(),
87
            'working_dir' => $this->lfm->path('working_dir'),
88
        ];
89
    }
90
91
92
    public function like_match($pattern, $subject)
93
    {
94
        $pattern = str_replace('%', '.*', preg_quote($pattern, '/'));
95
        return (bool) preg_match("/^{$pattern}$/i", $subject);
96
    }
97
98
    public function move()
99
    {
100
        $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

100
        $items = /** @scrutinizer ignore-call */ request('items');
Loading history...
101
        $folder_types = array_filter(['user', 'share'], function ($type) {
102
            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...
103
        });
104
        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

104
        return /** @scrutinizer ignore-call */ view('laravel-filemanager::move')
Loading history...
105
            ->with([
106
                'root_folders' => array_map(function ($type) use ($folder_types) {
107
                    $path = $this->lfm->dir($this->helper->getRootFolder($type));
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 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...
108
109
                    return (object) [
110
                        'name' => trans('laravel-filemanager::lfm.title-' . $type),
0 ignored issues
show
Bug introduced by
The function trans 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

110
                        'name' => /** @scrutinizer ignore-call */ trans('laravel-filemanager::lfm.title-' . $type),
Loading history...
111
                        'url' => $path->path('working_dir'),
112
                        'children' => $path->folders(),
113
                        'has_next' => ! ($type == end($folder_types)),
114
                    ];
115
                }, $folder_types),
116
            ])
117
            ->with('items', $items);
118
    }
119
120
    public function domove()
121
    {
122
        $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...
123
        $items = $this->helper->input('items');
124
125
        foreach ($items as $item) {
126
            $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...
127
            $is_directory = $old_file->isDirectory();
128
129
            if ($old_file->hasThumb()) {
130
                $new_file = $this->lfm->setName($item)->thumb()->dir($target);
131
                if ($is_directory) {
132
                    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

132
                    /** @scrutinizer ignore-call */ 
133
                    event(new FolderIsMoving($old_file->path(), $new_file->path()));
Loading history...
133
                } else {
134
                    event(new FileIsMoving($old_file->path(), $new_file->path()));
135
                }
136
                $this->lfm->setName($item)->thumb()->move($new_file);
137
            }
138
            $new_file = $this->lfm->setName($item)->dir($target);
139
            $this->lfm->setName($item)->move($new_file);
140
            if ($is_directory) {
141
                event(new FolderWasMoving($old_file->path(), $new_file->path()));
142
            } else {
143
                event(new FileWasMoving($old_file->path(), $new_file->path()));
144
            }
145
        };
146
147
        return parent::$success_response;
148
    }
149
150
    private static function getCurrentPageFromRequest()
151
    {
152
        $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

152
        $currentPage = (int) /** @scrutinizer ignore-call */ request()->get('page', 1);
Loading history...
153
        $currentPage = $currentPage < 1 ? 1 : $currentPage;
154
155
        return $currentPage;
156
    }
157
158
    private static function getKeywordFromRequest()
0 ignored issues
show
Unused Code introduced by
The method getKeywordFromRequest() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
159
    {
160
        $keyword = request()->get('keyword', "");
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

160
        $keyword = /** @scrutinizer ignore-call */ request()->get('keyword', "");
Loading history...
161
        return $keyword;
162
    }
163
}
164