Completed
Push — master ( ee72f6...3eb46d )
by
unknown
06:00
created

HandleFiles::handleField()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 54
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 54
ccs 0
cts 32
cp 0
rs 7.255
cc 9
eloc 31
nc 12
nop 5
crap 90

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
4
namespace Anavel\Crud\Abstractor\Eloquent\Traits;
5
6
use League\Flysystem\Adapter\Local;
7
use League\Flysystem\Filesystem;
8
use Illuminate\Http\Request;
9
10
trait HandleFiles
11
{
12
    protected function handleField(Request $request, $item, array $fields, $groupName, $fieldName)
13
    {
14
        $modelFolder = $this->slug . DIRECTORY_SEPARATOR;
0 ignored issues
show
Bug introduced by
The property slug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
        $basePath = base_path(DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR .config('anavel-crud.uploads_path'));
16
        $modelPath = $basePath . $modelFolder;
17
        $skip = null;
18
        $requestValue = null;
19
        if (! empty($fields["{$fieldName}__delete"])) {
20
            //We never want to save this field, it doesn't exist in the DB
21
            $skip = "{$fieldName}__delete";
22
23
24
            //If user wants to delete the existing file
25
            if (! empty($request->input("{$groupName}.{$fieldName}__delete"))) {
26
                $adapter = new Local($basePath);
27
                $filesystem = new Filesystem($adapter);
28
                if ($filesystem->has($item->$fieldName)) {
29
                    $filesystem->delete($item->$fieldName);
30
                }
31
32
33
                $item->setAttribute(
34
                    $fieldName,
35
                    null
36
                );
37
                return [
38
                    'skip' => $skip
39
                ];
40
            }
41
        }
42
        if ($request->hasFile($groupName .'.'.$fieldName)) {
43
            $fileName = uniqid() . '.' . $request->file($groupName .'.'.$fieldName)->getClientOriginalExtension();
44
45
46
            $request->file($groupName .'.'.$fieldName)->move(
47
                $modelPath,
48
                $fileName
49
            );
50
51
            $requestValue = $modelFolder . $fileName;
52
        } elseif (! empty($request->file($groupName .'.'.$fieldName)) && ! $request->file($groupName .'.'.$fieldName)->isValid()) {
53
            throw new \Exception($request->file($groupName .'.'.$fieldName)->getErrorMessage());
54
        }
55
56
        //Avoid losing the existing filename if the user doesn't change it:
57
        if (empty($requestValue) && (! empty($item->$fieldName))) {
58
            $requestValue = $item->$fieldName;
59
        }
60
61
        return [
62
            'requestValue' => $requestValue,
63
            'skip' => $skip
64
        ];
65
    }
66
}