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

Image   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 63
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDisk() 0 8 2
A setDisk() 0 6 1
A getImagePathAttribute() 0 4 1
A setImagePathAttribute() 0 6 1
A parseRows() 0 15 3
A getUrl() 0 10 2
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