Passed
Pull Request — master (#941)
by
unknown
05:54 queued 02:44
created

RenameController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 72
rs 10
wmc 17

1 Method

Rating   Name   Duplication   Size   Complexity  
C getRename() 0 70 17
1
<?php
2
3
namespace UniSharp\LaravelFilemanager\Controllers;
4
5
use Illuminate\Support\Str;
6
use UniSharp\LaravelFilemanager\Events\ImageIsRenaming;
7
use UniSharp\LaravelFilemanager\Events\ImageWasRenamed;
8
use UniSharp\LaravelFilemanager\Events\FolderIsRenaming;
9
use UniSharp\LaravelFilemanager\Events\FolderWasRenamed;
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
        $old_file = $this->lfm->pretty($old_name);
0 ignored issues
show
Bug introduced by
The method pretty() 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
        $old_file = $this->lfm->pretty($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
        $is_directory = $old_file->isDirectory();
21
22
        if (empty($new_name)) {
23
            if ($is_directory) {
24
                return parent::error('folder-name');
25
            } else {
26
                return parent::error('file-name');
27
            }
28
        }
29
30
        if ($is_directory && config('lfm.alphanumeric_directory')) {
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

30
        if ($is_directory && /** @scrutinizer ignore-call */ config('lfm.alphanumeric_directory')) {
Loading history...
31
            if (config('lfm.convert_to_alphanumeric')) {
32
                $new_name = Str::slug($new_name);
33
            }
34
35
            if (preg_match('/[^\w\-_]/i', $new_name)) {
36
                return parent::error('folder-alnum');
37
            }
38
        } elseif(!$is_directory && config('lfm.alphanumeric_filename')) {
39
            // Remove extension for checks to alphanum characters
40
            $extension = $old_file->extension();
41
            if ($extension) {
42
                $new_name = str_replace('.' . $extension, '', $new_name);
43
            }
44
45
            if (config('lfm.convert_to_alphanumeric')) {
46
                $new_name = Str::slug($new_name);
47
            }
48
49
            if (preg_match('/[^\w\-_]/i', $new_name)) {
50
                return parent::error('file-alnum');
51
            }
52
53
            $new_name .= ($extension) ? '.' . $extension : null;
54
        }
55
56
        if ($this->lfm->setName($new_name)->exists()) {
57
            return parent::error('rename');
58
        }
59
60
        $new_file = $this->lfm->setName($new_name)->path('absolute');
61
62
        if ($is_directory) {
63
            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

63
            /** @scrutinizer ignore-call */ 
64
            event(new FolderIsRenaming($old_file->path(), $new_file));
Loading history...
64
        } else {
65
            event(new ImageIsRenaming($old_file->path(), $new_file));
66
        }
67
68
        if ($old_file->hasThumb()) {
69
            $this->lfm->setName($old_name)->thumb()
70
                ->move($this->lfm->setName($new_name)->thumb());
71
        }
72
73
        $this->lfm->setName($old_name)
74
            ->move($this->lfm->setName($new_name));
75
76
        if ($is_directory) {
77
            event(new FolderWasRenamed($old_file->path(), $new_file));
78
        } else {
79
            event(new ImageWasRenamed($old_file->path(), $new_file));
80
        }
81
82
        return parent::$success_response;
83
    }
84
}
85