Completed
Push — master ( 0c9838...f80bb7 )
by wen
12:48
created

Image   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 6.52 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 3
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getImagePathAttribute() 0 4 1
A setImagePathAttribute() 0 6 1
A parseRows() 0 16 3
A getImageUrl() 3 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Sco\Admin\Display;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Sco\Admin\Traits\UploadStorageTrait;
8
9
class Image extends Table
10
{
11
    use UploadStorageTrait;
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->getImageUrl($row->$pathKey));
41
42
            return $row;
43
        });
44
    }
45
46
    protected function getImageUrl($value)
47
    {
48 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...
49
            $value = $this->getFileUrl($value);
50
        }
51
52
        return $value;
53
    }
54
}
55