Image::imageTransformations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace HustleWorks\ChuteLaravel\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Ramsey\Uuid\Uuid;
8
9
/**
10
 * \HustleWorks\ChuteLaravel\Models\Image
11
 *
12
 * @property int                                                                                           $id
13
 * @property int|null                                                                                      $imageable_id
14
 * @property string|null                                                                                   $imageable_type
15
 * @property string                                                                                        $name
16
 * @property string                                                                                        $filename
17
 * @property string                                                                                        $disk
18
 * @property string                                                                                        $directory
19
 * @property string                                                                                        $uuid
20
 * @property string                                                                                        $path
21
 * @property string                                                                                        $status
22
 * @property string|null                                                                                   $width
23
 * @property string|null                                                                                   $height
24
 * @property int|null                                                                                      $size_in_kilobytes
25
 * @property string|null                                                                                   $mime_type
26
 * @property string|null                                                                                   $caption
27
 * @property \Carbon\Carbon|null                                                                           $created_at
28
 * @property \Carbon\Carbon|null                                                                           $updated_at
29
 * @property-read \Illuminate\Database\Eloquent\Collection|\HustleWorks\ChuteLaravel\Models\ImageTransformation[] $imageTransformations
30
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent                                            $imageable
31
 * @method static Builder|Image whereCaption($value)
32
 * @method static Builder|Image whereCreatedAt($value)
33
 * @method static Builder|Image whereDirectory($value)
34
 * @method static Builder|Image whereDisk($value)
35
 * @method static Builder|Image whereHeight($value)
36
 * @method static Builder|Image whereId($value)
37
 * @method static Builder|Image whereImageableId($value)
38
 * @method static Builder|Image whereImageableType($value)
39
 * @method static Builder|Image whereMimeType($value)
40
 * @method static Builder|Image whereName($value)
41
 * @method static Builder|Image wherePath($value)
42
 * @method static Builder|Image whereSizeInKilobytes($value)
43
 * @method static Builder|Image whereStatus($value)
44
 * @method static Builder|Image whereUpdatedAt($value)
45
 * @method static Builder|Image whereUuid($value)
46
 * @method static Builder|Image whereWidth($value)
47
 * @mixin \Eloquent
48
 */
49
class Image extends Model
50
{
51
    protected $fillable = [
52
        'disk',
53
        'name',
54
        'filename',
55
        'path',
56
        'size',
57
        'status',
58
        'width',
59
        'height',
60
        'mime_type',
61
        'extension',
62
        'alt',
63
        'title',
64
        'description',
65
    ];
66
67
    public function getTransformationUrl($name)
68
    {
69
        /** @var ImageTransformation $transformation */
70
        $transformation = $this->imageTransformations()->where('name', $name)->first();
71
72
        return $transformation->imageUrl();
73
    }
74
75
    public function getOriginalUrl()
76
    {
77
        return \Storage::disk($this->disk)->url("$this->path/$this->uuid/$this->filename");
78
    }
79
80
    /**
81
     * Image Transformations
82
     *
83
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
84
     */
85
    public function imageTransformations()
86
    {
87
        return $this->hasMany(ImageTransformation::class);
88
    }
89
90
    /**
91
     * Imageable
92
     *
93
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
94
     */
95
    public function imageable()
96
    {
97
        return $this->morphTo();
98
    }
99
100
    /**
101
     * The "booting" method of the model.
102
     *
103
     * @return void
104
     */
105
    protected static function boot()
106
    {
107
        parent::boot();
108
109
        static::creating(function (Image $model) {
110
            // Don't let people provide their own UUIDs, we will generate a proper one.
111
            $model->uuid = Uuid::uuid4()->toString();
112
        });
113
114
        static::saving(function (Image $model) {
115
            // What's that, trying to change the UUID huh?  Nope, not gonna happen.
116
            $original_uuid = $model->getOriginal('uuid');
117
118
            if ($original_uuid !== $model->uuid) {
119
                $model->uuid = $original_uuid;
120
            }
121
        });
122
123 View Code Duplication
        static::deleting(function (Image $image_original) {
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...
124
125
            /* if the original is deleted all transformations must be deleted */
126
            foreach ($image_original->imageTransformations as $transformation) {
127
                $transformation->delete();
128
            }
129
130
            /* get the path */
131
            $path = "$image_original->directory/$image_original->uuid/$image_original->path";
132
133
            /* delete the saved file */
134
            if (\Storage::disk($image_original->disk)->exists($path)) {
135
                \Storage::disk($image_original->disk)->delete($path);
136
            }
137
138
            /* delete the directory */
139
            if (!\Storage::disk($image_original->disk)->files("$image_original->directory/$image_original->uuid")) {
140
                \Storage::disk($image_original->disk)->deleteDirectory("$image_original->directory/$image_original->uuid");
141
            }
142
        });
143
    }
144
}
145