HandleFiles   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 0
cts 43
cp 0
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
C handleField() 0 64 12
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