Completed
Push — master ( 2c56a4...01668e )
by wen
13:19
created

Image::setDisk()   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
            $row->setAttribute('_primary', $row->getKey());
55
            $row->setAttribute('_url', $this->getUrl($row->$pathKey));
56
            return $row;
57
        });
58
    }
59
60
    protected function getUrl($path)
61
    {
62
        if (($disk = $this->getDisk())) {
63
            $url = \Storage::disk($disk)->url($path);
64
        } else {
65
            $url = asset($path);
66
        }
67
68
        return $url;
69
    }
70
}
71