Completed
Push — master ( 8cc915...bfc41a )
by wen
13:38
created

Image::setImagePathAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\View;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
8
class Image extends Table
9
{
10
    protected $type = 'image';
11
12
    protected $disk;
13
14
    protected $imagePathAttribute;
15
16
    public function getDisk()
17
    {
18
        if ($this->disk) {
19
            return $this->disk;
20
        }
21
22
        return config('admin.upload.disk', 'public');
23
    }
24
25
    public function setDisk($value)
26
    {
27
        $this->disk = $value;
28
29
        return $this;
30
    }
31
32
    public function getImagePathAttribute()
33
    {
34
        return $this->imagePathAttribute;
35
    }
36
37
    public function setImagePathAttribute($value)
38
    {
39
        $this->imagePathAttribute = $value;
40
41
        return $this;
42
    }
43
44
    protected function parseRows(Collection $rows)
45
    {
46
        if (is_null($pathKey = $this->getImagePathAttribute())) {
47
            throw new \InvalidArgumentException('Must set image attribute');
48
        }
49
50
        return $rows->map(function (Model $row) use ($pathKey) {
51
            if (!isset($row->$pathKey)) {
52
                throw new \InvalidArgumentException("Not Found '{$pathKey}' attribute");
53
            }
54
55
            $path = $row->$pathKey;
56 View Code Duplication
            if (($disk = $this->getDisk())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
                $url = \Storage::disk($disk)->url($path);
58
            } else {
59
                $url = asset($path);
60
            }
61
62
            return [
63
                'id' => $row->getKey(),
64
                'url' => $url,
65
            ];
66
        });
67
    }
68
}
69