Passed
Push — master ( 3b189c...342025 )
by Stream
03:51
created

RenameController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 36
c 2
b 1
f 0
dl 0
loc 62
rs 10
wmc 12

1 Method

Rating   Name   Duplication   Size   Complexity  
C getRename() 0 60 12
1
<?php
2
3
namespace UniSharp\LaravelFilemanager\Controllers;
4
5
use Illuminate\Support\Facades\Storage;
6
use UniSharp\LaravelFilemanager\Events\FolderIsRenaming;
7
use UniSharp\LaravelFilemanager\Events\FolderWasRenamed;
8
use UniSharp\LaravelFilemanager\Events\ImageIsRenaming;
9
use UniSharp\LaravelFilemanager\Events\ImageWasRenamed;
10
11
class RenameController extends LfmController
12
{
13
    public function getRename()
14
    {
15
        $old_name = $this->helper->input('file');
0 ignored issues
show
Bug Best Practice introduced by
The property helper does not exist on UniSharp\LaravelFilemana...ollers\RenameController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method input() 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

15
        /** @scrutinizer ignore-call */ 
16
        $old_name = $this->helper->input('file');

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...
16
        $new_name = $this->helper->input('new_name');
17
18
        $file = $this->lfm->setName($old_name);
0 ignored issues
show
Bug introduced by
The method setName() 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

18
        /** @scrutinizer ignore-call */ 
19
        $file = $this->lfm->setName($old_name);

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...ollers\RenameController. Since you implemented __get, consider adding a @property annotation.
Loading history...
19
20
        if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) {
21
            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

21
            /** @scrutinizer ignore-call */ 
22
            abort(404);
Loading history...
22
        }
23
24
        $old_file = $this->lfm->pretty($old_name);
25
26
        $is_directory = $old_file->isDirectory();
27
28
        if (empty($new_name)) {
29
            if ($is_directory) {
30
                return parent::error('folder-name');
31
            } else {
32
                return parent::error('file-name');
33
            }
34
        }
35
36
        if (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) {
0 ignored issues
show
Bug introduced by
The function config 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

36
        if (/** @scrutinizer ignore-call */ config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) {
Loading history...
37
            return parent::error('folder-alnum');
38
        // return parent::error('file-alnum');
39
        } elseif ($this->lfm->setName($new_name)->exists()) {
40
            return parent::error('rename');
41
        }
42
43
        if (! $is_directory) {
44
            $extension = $old_file->extension();
45
            if ($extension) {
46
                $new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension;
47
            }
48
        }
49
50
        $new_file = $this->lfm->setName($new_name)->path('absolute');
51
52
        if ($is_directory) {
53
            event(new FolderIsRenaming($old_file->path(), $new_file));
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

53
            /** @scrutinizer ignore-call */ 
54
            event(new FolderIsRenaming($old_file->path(), $new_file));
Loading history...
54
        } else {
55
            event(new ImageIsRenaming($old_file->path(), $new_file));
56
        }
57
58
        if ($old_file->hasThumb()) {
59
            $this->lfm->setName($old_name)->thumb()
60
                ->move($this->lfm->setName($new_name)->thumb());
61
        }
62
63
        $this->lfm->setName($old_name)
64
            ->move($this->lfm->setName($new_name));
65
66
        if ($is_directory) {
67
            event(new FolderWasRenamed($old_file->path(), $new_file));
68
        } else {
69
            event(new ImageWasRenamed($old_file->path(), $new_file));
70
        }
71
72
        return parent::$success_response;
73
    }
74
}
75