Completed
Push — master ( d32456...8cc915 )
by wen
10:57
created

Image::getDisk()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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 $imageAttributeValue;
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 getImageAttributeValue()
33
    {
34
        return $this->imageAttributeValue;
35
    }
36
37
    public function setImageAttributeValue($value)
38
    {
39
        $this->imageAttributeValue = $value;
40
41
        return $this;
42
    }
43
44
    protected function parseRows(Collection $rows)
45
    {
46
        if (is_null($key = $this->getImageAttributeValue())) {
47
            throw new \InvalidArgumentException('must set image attribute');
48
        }
49
50
        return $rows->map(function (Model $row) use ($key) {
51
            $path = $row->$key;
52 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...
53
                $url = \Storage::disk($disk)->url($path);
54
            } else {
55
                $url = asset($path);
56
            }
57
58
            return [
59
                'id' => $row->id,
60
                'url' => $url,
61
            ];
62
        });
63
    }
64
}
65