HandleFiles::handleField()   C
last analyzed

Complexity

Conditions 12
Paths 20

Size

Total Lines 64
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 39
nc 20
nop 6
dl 0
loc 64
ccs 0
cts 43
cp 0
crap 156
rs 6.0561
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
namespace Anavel\Crud\Abstractor\Eloquent\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\Request;
7
use League\Flysystem\Adapter\Local;
8
use League\Flysystem\Filesystem;
9
10
trait HandleFiles
11
{
12
    /**
13
     * @param Request $request
14
     * @param Model   $item
15
     * @param array   $fields
16
     * @param $groupName
17
     * @param $fieldName
18
     * @param bool $mustDeleteFilesInFilesystem
19
     *
20
     * @throws \Exception
21
     *
22
     * @return array
23
     */
24
    protected function handleField(Request $request, $item, array $fields, $groupName, $fieldName, $mustDeleteFilesInFilesystem = false)
25
    {
26
        $normalizeGroupName = str_replace('.', '#', $groupName);
27
        $contentFromUploadedField = "uploaded-content.{$normalizeGroupName}#{$fieldName}#";
28
        $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...
29
        $basePath = base_path(DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.config('anavel-crud.uploads_path'));
30
        $modelPath = $basePath.$modelFolder;
31
        $skip = null;
32
        $requestValue = null;
33
34
        if (!empty($fields["{$fieldName}__delete"])) {
35
            //We never want to save this field, it doesn't exist in the DB
36
            $skip = "{$fieldName}__delete";
37
38
            //If user wants to delete the existing file
39
            if (!empty($request->input("{$groupName}.{$fieldName}__delete"))) {
40
                $adapter = new Local($basePath);
41
                $filesystem = new Filesystem($adapter);
42
                if ($filesystem->has($item->$fieldName) && $mustDeleteFilesInFilesystem) {
43
                    $filesystem->delete($item->$fieldName);
44
                }
45
46
                $item->setAttribute(
47
                    $fieldName,
48
                    null
49
                );
50
51
                return [
52
                    'skip' => $skip,
53
                ];
54
            }
55
        }
56
57
        if ($request->has($contentFromUploadedField)) {
58
            $requestValue = $request->input($contentFromUploadedField);
59
        } elseif ($request->hasFile($groupName.'.'.$fieldName)) {
60
            $fileName = pathinfo($request->file($groupName.'.'.$fieldName)->getClientOriginalName(), PATHINFO_FILENAME);
61
            $extension = pathinfo($request->file($groupName.'.'.$fieldName)->getClientOriginalName(), PATHINFO_EXTENSION);
62
63
            $fileName = uniqid().'_'.slugify($fileName);
64
            if (!empty($extension)) {
65
                $fileName .= '.'.$extension;
66
            }
67
68
            $request->file($groupName.'.'.$fieldName)->move(
69
                $modelPath,
70
                $fileName
71
            );
72
73
            $requestValue = $modelFolder.$fileName;
74
        } elseif (!empty($request->file($groupName.'.'.$fieldName)) && !$request->file($groupName.'.'.$fieldName)->isValid()) {
75
            throw new \Exception($request->file($groupName.'.'.$fieldName)->getErrorMessage());
76
        }
77
78
        //Avoid losing the existing filename if the user doesn't change it:
79
        if (empty($requestValue) && (!empty($item->$fieldName))) {
80
            $requestValue = $item->$fieldName;
81
        }
82
83
        return [
84
            'requestValue' => $requestValue,
85
            'skip'         => $skip,
86
        ];
87
    }
88
}
89