YeelightImageBase   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getYeelightImageId() 0 3 1
A getImageUrl() 0 3 1
A getDefaultImageUrl() 0 2 1
A isValid() 0 3 1
A getImageUrlAttribute() 0 3 1
A getImageName() 0 3 1
A transform() 0 5 1
A toSimpleArray() 0 8 1
A getTypeImageUrl() 0 8 3
A getImagePath() 0 5 1
1
<?php
2
3
namespace Yeelight\Services\Image\Models;
4
5
use Prettus\Repository\Contracts\Presentable;
6
use Prettus\Repository\Contracts\Transformable;
7
use Prettus\Repository\Traits\PresentableTrait;
8
use Yeelight\Base\Models\BaseModel;
9
use Yeelight\Services\Image\Models\Traits\YeelightPivotTrait;
10
11
/**
12
 * Yeelight\Services\Image\Models\YeelightImageBase.
13
 *
14
 * @property int $yeelight_image_id
15
 * @property int|null $user_id
16
 * @property string $image_name
17
 * @property string|null $exif
18
 * @property int $is_gif
19
 * @property int $file_size
20
 * @property int $width
21
 * @property int $height
22
 * @property int|null $created_by
23
 * @property \Carbon\Carbon|null $created_at
24
 * @property string|null $created_ip
25
 * @property int|null $updated_by
26
 * @property \Carbon\Carbon|null $updated_at
27
 * @property string|null $updated_ip
28
 * @property-read mixed $id
29
 * @property-read null|string $image_url
30
 *
31
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereCreatedAt($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereCreatedBy($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereCreatedIp($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereExif($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereFileSize($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereHeight($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereImageName($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereIsGif($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereUpdatedAt($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereUpdatedBy($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereUpdatedIp($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereUserId($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereWidth($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Yeelight\Services\Image\Models\YeelightImageBase whereYeelightImageId($value)
45
 * @mixin \Eloquent
46
 */
47
class YeelightImageBase extends BaseModel implements Transformable, Presentable
48
{
49
    use YeelightPivotTrait;
0 ignored issues
show
introduced by
The trait Yeelight\Services\Image\...aits\YeelightPivotTrait requires some properties which are not provided by Yeelight\Services\Image\Models\YeelightImageBase: $pivot, $parent
Loading history...
50
    use PresentableTrait;
51
52
    const MORPH_NAME = 'YeelightImage';
53
54
    protected $table = 'yeelight_images';
55
56
    protected $primaryKey = 'yeelight_image_id';
57
58
    protected $fillable = [
59
        'user_id', 'image_name', 'exif', 'is_gif', 'file_size', 'width', 'height',
60
    ];
61
62
    protected $hidden = [
63
        'created_at', 'created_by', 'created_ip',
64
        'updated_at', 'updated_by', 'updated_ip',
65
    ];
66
67
    /**
68
     * @return int
69
     */
70
    public function getYeelightImageId()
71
    {
72
        return $this->yeelight_image_id;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getImageName()
79
    {
80
        return $this->image_name;
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    public function isValid()
87
    {
88
        return $this->file_size > 0;
89
    }
90
91
    /**
92
     * @return null|string
93
     */
94
    public function getImageUrl()
95
    {
96
        return $this->getTypeImageUrl();
97
    }
98
99
    /**
100
     * @return null|string
101
     */
102
    public function getImageUrlAttribute()
103
    {
104
        return $this->getImageUrl();
105
    }
106
107
    /**
108
     * @param $type
109
     *
110
     * @return null|string
111
     */
112
    public function getTypeImageUrl($type = null)
113
    {
114
        $type = $type ? $type.'/' : null;
115
116
        if ($this->isValid()) {
117
            return url('/image/'.$type.$this->getImageName());
0 ignored issues
show
Bug Best Practice introduced by
The expression return url('/image/' . $... $this->getImageName()) also could return the type Illuminate\Contracts\Routing\UrlGenerator which is incompatible with the documented return type null|string.
Loading history...
118
        } else {
119
            return $this->getDefaultImageUrl($type = null);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getDefaultImageUrl($type = null) targeting Yeelight\Services\Image\...e::getDefaultImageUrl() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
120
        }
121
    }
122
123
    /**
124
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
125
     *
126
     * @return null|string
127
     */
128
    public function getDefaultImageUrl($type = null)
129
    {
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getImagePath()
136
    {
137
        $storage_path = config('yeelight-image.storage_path');
138
139
        return $storage_path.$this->getImageName();
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function transform()
146
    {
147
        return [
148
            'yeelight_image_id' => $this->getYeelightImageId(),
149
            'image_name'        => $this->getImageName(),
150
        ];
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function toSimpleArray()
157
    {
158
        $yeelightImage = $this;
159
160
        return [
161
            'yeelight_image_id'   => $yeelightImage->getYeelightImageId(),
162
            'yeelight_image_url'  => $yeelightImage->getImageUrl(),
163
            'thumbnail_image_url' => $yeelightImage->getTypeImageUrl('thumbnail'),
164
        ];
165
    }
166
}
167