Completed
Pull Request — master (#14)
by
unknown
15:13 queued 07:44
created

File   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 107
Duplicated Lines 12.15 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 38.3%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 9
dl 13
loc 107
ccs 18
cts 47
cp 0.383
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getListView() 0 7 1
A __construct() 0 4 1
A shouldSkip() 0 6 1
A value() 0 22 5
A getEditFormView() 0 9 2
A getCreateFormView() 0 6 1
A getPaths() 13 13 4
A formUrl() 0 6 1
A generateFilename() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Storage as IlluminateStorage;
9
use Yaro\Jarboe\Table\Fields\Adapters\RepeaterFile;
10
use Yaro\Jarboe\Table\Fields\Traits\Filename;
11
use Yaro\Jarboe\Table\Fields\Traits\Nullable;
12
use Yaro\Jarboe\Table\Fields\Traits\Placeholder;
13
use Yaro\Jarboe\Table\Fields\Traits\Storage;
14
15
class File extends AbstractField
16
{
17
    use Storage;
18
    use Nullable;
19
    use Placeholder;
20
    use Filename;
21
22
    public static $repeaterAdapterClass = RepeaterFile::class;
23
24 45
    public function __construct()
25
    {
26 45
        $this->disk = config('filesystems.default');
27 45
    }
28
29 2
    public function shouldSkip(Request $request)
30
    {
31 2
        $files = $request->file($this->name());
32
33 2
        return !$files;
34
    }
35
36
    public function value(Request $request)
37
    {
38
        $data = $request->get($this->name());
39
        $paths = Arr::get($data, 'paths', []);
40
        $files = Arr::get($data, 'files', []);
41
42
        if (!$this->isMultiple()) {
43
            $files = [$files];
44
        }
45
        $files = array_filter($files);
46
47
        /** @var UploadedFile $file */
48
        foreach ($files as $file) {
49
            $filename = $this->generateFilename($request, $file);
50
            $paths[] = $this->storeFile($file, $filename, $request);
51
        }
52
53
        if ($this->isMultiple()) {
54
            return $paths;
55
        }
56
        return array_filter($paths) ? array_pop($paths) : null;
57
    }
58
59 1
    public function getListView($model)
60
    {
61 1
        return view('jarboe::crud.fields.file.list', [
62 1
            'model' => $model,
63 1
            'field' => $this,
64
        ]);
65
    }
66
67 1
    public function getEditFormView($model)
68
    {
69 1
        $template = $this->isReadonly() ? 'readonly' : 'edit';
70
71 1
        return view('jarboe::crud.fields.file.'. $template, [
72 1
            'model' => $model,
73 1
            'field' => $this,
74
        ]);
75
    }
76
77 1
    public function getCreateFormView()
78
    {
79 1
        return view('jarboe::crud.fields.file.create', [
80 1
            'field' => $this,
81
        ]);
82
    }
83
84 View Code Duplication
    public function getPaths($model): array
85
    {
86
        if (is_null($model)) {
87
            return [];
88
        }
89
90
        $filepath = $this->getAttribute($model);
91
        if (!$filepath) {
92
            return [];
93
        }
94
95
        return is_array($filepath) ? $filepath : [$filepath];
96
    }
97
98
    public function formUrl(string $path)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
99
    {
100
        $disk = IlluminateStorage::disk($this->getDisk());
101
102
        return $disk->url($path);
103
    }
104
105
    /**
106
     * @param Request $request
107
     * @param UploadedFile $file
108
     * @return string
109
     */
110
    private function generateFilename(Request $request, UploadedFile $file): string
111
    {
112
        $filename = $file->hashName();
113
114
        $closure = $this->filenameClosure;
115
        if (is_callable($closure)) {
116
            $filename = $closure($file, $request);
117
        }
118
119
        return (string) $filename;
120
    }
121
}
122