Passed
Push — master ( 1120e7...dcb672 )
by Jesús
02:28
created

Image   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 80
Duplicated Lines 21.25 %

Importance

Changes 0
Metric Value
dl 17
loc 80
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A url_to() 0 10 3
D thumbnails() 17 26 9
A getThumbnailsAttribute() 0 3 1
A url() 0 3 1

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 OkayBueno\Images\Models;
4
5
use Illuminate\Database\Eloquent\SoftDeletes;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * Class Image
10
 * @package OkayBueno\Images\Models
11
 */
12
class Image extends Model
13
{
14
    use SoftDeletes;
15
16
    protected $fillable = [
17
        'path', 'filename', 'type', 'thumbnails', 'metadata', 'processed'
18
    ];
19
20
21
    /*******************************************************************************************************************
22
     ************************************************** HELPERS / DATA *************************************************
23
     *******************************************************************************************************************/
24
    /**
25
     * @param null $size
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $size is correct as it would always require null to be passed?
Loading history...
26
     * @param bool|FALSE $onlyPath
27
     * @return array|null|string
28
     */
29
    public function thumbnails( $size = NULL, $onlyPath = FALSE )
30
    {
31
        $sizesArray = [];
32
33
        if ( is_null( $size ) )
34
        {
35 View Code Duplication
            if ( is_array( $this->thumbnails ) )
2 ignored issues
show
Bug introduced by
The property thumbnails does not seem to exist on OkayBueno\Images\Models\Image. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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...
36
            {
37
                foreach( $this->thumbnails as $sizeKey => $path )
38
                {
39
                    $sizesArray[ $sizeKey ] = $onlyPath ? $path : $this->url_to( $path );
40
                }
41
            }
42
43 View Code Duplication
        } else
1 ignored issue
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...
44
        {
45
            if ( is_array( $this->thumbnails) )
46
            {
47
                foreach( $this->thumbnails as $sizeKey => $path )
48
                {
49
                    if( $size == $sizeKey ) return $onlyPath ? $path : $this->url_to( $path );
50
                }
51
            }
52
        }
53
54
        return $sizesArray;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function url()
61
    {
62
        return $this->url_to( $this->path );
0 ignored issues
show
Bug introduced by
The property path does not seem to exist on OkayBueno\Images\Models\Image. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
63
    }
64
65
    /*******************************************************************************************************************
66
     **************************************************** INTERNAL *****************************************************
67
     *******************************************************************************************************************/
68
69
    /**
70
     * @param $value
71
     * @return mixed
72
     */
73
    public function getThumbnailsAttribute( $value )
74
    {
75
        return json_decode( $value, TRUE );
76
    }
77
78
    /**
79
     * @param $path
80
     * @return string
81
     */
82
    protected function url_to( $path )
83
    {
84
        $localUrl = config('images.local_disk_url');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        $localUrl = /** @scrutinizer ignore-call */ config('images.local_disk_url');
Loading history...
85
        $remoteUrl = config('images.cloud_disk_url');
86
        if ( $this->processed ) $cdnBaseUrl = $remoteUrl ? $remoteUrl : $localUrl;
0 ignored issues
show
Bug introduced by
The property processed does not seem to exist on OkayBueno\Images\Models\Image. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
87
        else $cdnBaseUrl = $localUrl;
88
89
        $cdnUrlToImage = rtrim( $cdnBaseUrl, '/') . '/' . trim( $path, '/');
90
91
        return $cdnUrlToImage;
92
    }
93
94
95
}
96