Completed
Push — master ( a6d5b0...1121c7 )
by wen
12:56
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
use Sco\Admin\Traits\StorageTrait;
8
9
class Image extends Table
10
{
11
    use StorageTrait;
12
13
    protected $type = 'image';
14
15
    protected $imagePathAttribute;
16
17
    public function getImagePathAttribute()
18
    {
19
        return $this->imagePathAttribute;
20
    }
21
22
    public function setImagePathAttribute($value)
23
    {
24
        $this->imagePathAttribute = $value;
25
26
        return $this;
27
    }
28
29
    protected function parseRows(Collection $rows)
30
    {
31
        if (is_null($pathKey = $this->getImagePathAttribute())) {
32
            throw new \InvalidArgumentException('Must set image attribute');
33
        }
34
35
        return $rows->map(function (Model $row) use ($pathKey) {
36
            if (!isset($row->$pathKey)) {
37
                throw new \InvalidArgumentException("Not Found '{$pathKey}' attribute");
38
            }
39
            $row->setAttribute('_primary', $row->getKey());
40
            $row->setAttribute('_url', $this->getFileUrl($row->$pathKey));
41
            return $row;
42
        });
43
    }
44
}
45