Passed
Pull Request — master (#995)
by
unknown
07:25
created

ItemsController::domove()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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

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

105
        return /** @scrutinizer ignore-call */ view('laravel-filemanager::move')
Loading history...
106
            ->with([
107
                'root_folders' => array_map(function ($type) use ($folder_types) {
108
                    $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...
109
110
                    return (object) [
111
                        '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

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

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

153
        $currentPage = (int) /** @scrutinizer ignore-call */ request()->get('page', 1);
Loading history...
154
        $currentPage = $currentPage < 1 ? 1 : $currentPage;
155
156
        return $currentPage;
157
    }
158
159
    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...
160
    {
161
        $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

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