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

ItemsController::getItems()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 4
b 0
f 0
nc 2
nop 1
dl 0
loc 31
rs 9.584
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();
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

24
        /** @scrutinizer ignore-call */ 
25
        $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...
25
        $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

25
        $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...
26
27
        $items = array_map(function ($item) {
28
            return $item->fill()->attributes;
29
        }, array_slice($items, ($currentPage - 1) * $perPage, $perPage));
30
31
        if($keyword != null) {
32
            $items = array_values(array_filter($items, function($item) use ($keyword) {
33
                if($this->like_match("%".$keyword."%", $item['name'])) {
34
                    return true;
35
                } else {
36
                    return false;
37
                }
38
            }));
39
        }
40
41
        return [
42
            'items' => $items,
43
            'paginator' => [
44
                'current_page' => $currentPage,
45
                'total' => count($items),
46
                'per_page' => $perPage,
47
            ],
48
            'display' => $this->helper->getDisplayMode(),
49
            'working_dir' => $this->lfm->path('working_dir'),
50
        ];
51
    }
52
53
54
    public function like_match($pattern, $subject)
55
    {
56
        $pattern = str_replace('%', '.*', preg_quote($pattern, '/'));
57
        return (bool) preg_match("/^{$pattern}$/i", $subject);
58
    }
59
60
    public function move()
61
    {
62
        $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

62
        $items = /** @scrutinizer ignore-call */ request('items');
Loading history...
63
        $folder_types = array_filter(['user', 'share'], function ($type) {
64
            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...
65
        });
66
        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

66
        return /** @scrutinizer ignore-call */ view('laravel-filemanager::move')
Loading history...
67
            ->with([
68
                'root_folders' => array_map(function ($type) use ($folder_types) {
69
                    $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...
70
71
                    return (object) [
72
                        '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

72
                        'name' => /** @scrutinizer ignore-call */ trans('laravel-filemanager::lfm.title-' . $type),
Loading history...
73
                        'url' => $path->path('working_dir'),
74
                        'children' => $path->folders(),
75
                        'has_next' => ! ($type == end($folder_types)),
76
                    ];
77
                }, $folder_types),
78
            ])
79
            ->with('items', $items);
80
    }
81
82
    public function domove()
83
    {
84
        $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...
85
        $items = $this->helper->input('items');
86
87
        foreach ($items as $item) {
88
            $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...
89
            $is_directory = $old_file->isDirectory();
90
91
            if ($old_file->hasThumb()) {
92
                $new_file = $this->lfm->setName($item)->thumb()->dir($target);
93
                if ($is_directory) {
94
                    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

94
                    /** @scrutinizer ignore-call */ 
95
                    event(new FolderIsMoving($old_file->path(), $new_file->path()));
Loading history...
95
                } else {
96
                    event(new FileIsMoving($old_file->path(), $new_file->path()));
97
                }
98
                $this->lfm->setName($item)->thumb()->move($new_file);
99
            }
100
            $new_file = $this->lfm->setName($item)->dir($target);
101
            $this->lfm->setName($item)->move($new_file);
102
            if ($is_directory) {
103
                event(new FolderWasMoving($old_file->path(), $new_file->path()));
104
            } else {
105
                event(new FileWasMoving($old_file->path(), $new_file->path()));
106
            }
107
        };
108
109
        return parent::$success_response;
110
    }
111
112
    private static function getCurrentPageFromRequest()
113
    {
114
        $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

114
        $currentPage = (int) /** @scrutinizer ignore-call */ request()->get('page', 1);
Loading history...
115
        $currentPage = $currentPage < 1 ? 1 : $currentPage;
116
117
        return $currentPage;
118
    }
119
120
    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...
121
    {
122
        $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

122
        $keyword = /** @scrutinizer ignore-call */ request()->get('keyword', "");
Loading history...
123
        return $keyword;
124
    }
125
}
126