Image::setImagePathAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Sco\Admin\Display;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Sco\Admin\Display\Concerns\WithPagination;
8
use Sco\Admin\Traits\UploadStorageTrait;
9
10
class Image extends Display
11
{
12
    use UploadStorageTrait, WithPagination;
13
14
    protected $type = 'image';
15
16
    protected $imagePathAttribute;
17
18
    public function getImagePathAttribute()
19
    {
20
        return $this->imagePathAttribute;
21
    }
22
23
    public function setImagePathAttribute($value)
24
    {
25
        $this->imagePathAttribute = $value;
26
27
        return $this;
28
    }
29
30 View Code Duplication
    public function get()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
31
    {
32
        if ($this->isPagination()) {
33
            $data = $this->paginate();
34
35
            return $data->setCollection($this->parseRows($data->getCollection()));
36
        }
37
        return $this->parseRows($this->getQuery()->get());
0 ignored issues
show
Bug introduced by
It seems like $this->getQuery()->get() targeting Illuminate\Database\Eloquent\Builder::get() can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, Sco\Admin\Display\Image::parseRows() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
38
    }
39
40
    protected function parseRows(Collection $rows)
41
    {
42
        if (is_null($pathKey = $this->getImagePathAttribute())) {
43
            throw new \InvalidArgumentException('Must set image attribute');
44
        }
45
46
        return $rows->map(function (Model $row) use ($pathKey) {
47
            if (! isset($row->$pathKey)) {
48
                throw new \InvalidArgumentException("Not Found '{$pathKey}' attribute");
49
            }
50
            $row->setAttribute('_primary', $row->getKey());
51
            $row->setAttribute('_url', $this->getImageUrl($row->$pathKey));
52
53
            return $row;
54
        });
55
    }
56
57
    protected function getImageUrl($value)
58
    {
59 View Code Duplication
        if (! empty($value) && (strpos($value, '://') === false)) {
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...
60
            $value = $this->getFileUrl($value);
61
        }
62
63
        return $value;
64
    }
65
}
66