Completed
Push — master ( 8c64c3...017009 )
by Song
02:56
created

Resizable   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A thumbnail() 0 14 3
A getThumbnail() 0 11 1
1
<?php
2
3
namespace Encore\Admin\Traits;
4
5
trait Resizable
6
{
7
    /**
8
     * Method for returning specific thumbnail for model.
9
     *
10
     * @param string $type
11
     * @param string $attribute
12
     *
13
     * @return string
14
     */
15
    public function thumbnail($type, $attribute = 'image')
16
    {
17
        // Return empty string if the field not found
18
        if (!isset($this->attributes[$attribute])) {
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
            return '';
20
        }
21
22
        // We take image from posts field
23
        $image = $this->attributes[$attribute];
24
25
        $thumbnail = $this->getThumbnail($image, $type);
26
27
        return \Illuminate\Support\Facades\Storage::disk(config('admin.upload.disk'))->exists($thumbnail) ? $thumbnail : $image;
28
    }
29
30
    /**
31
     * Generate thumbnail URL.
32
     *
33
     * @param $image
34
     * @param $type
35
     *
36
     * @return string
37
     */
38
    public function getThumbnail($image, $type)
39
    {
40
        // We need to get extension type ( .jpeg , .png ...)
41
        $ext = pathinfo($image, PATHINFO_EXTENSION);
42
43
        // We remove extension from file name so we can append thumbnail type
44
        $name = str_replace_last('.'.$ext, '', $image);
0 ignored issues
show
Deprecated Code introduced by
The function str_replace_last() has been deprecated with message: Str::replaceLast() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
45
46
        // We merge original name + type + extension
47
        return $name.'-'.$type.'.'.$ext;
48
    }
49
}
50