1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HustleWorks\ChuteLaravel\Traits; |
4
|
|
|
|
5
|
|
|
use HustleWorks\Chute\DTO\ImageConfiguration; |
6
|
|
|
use HustleWorks\ChuteLaravel\Jobs\ImageProcessingJob; |
7
|
|
|
use HustleWorks\ChuteLaravel\Models\Image; |
8
|
|
|
use HustleWorks\Chute\ImageConfigurationBuilder; |
9
|
|
|
use HustleWorks\ChuteLaravel\Services\ImageUploadService; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait ChuteImageModelTrait |
13
|
|
|
* |
14
|
|
|
* @property string[] $chute_image_sizes |
15
|
|
|
* @property array $chute_image_rule |
16
|
|
|
* @property int $id |
17
|
|
|
* @mixin \Eloquent |
18
|
|
|
*/ |
19
|
|
|
trait ChuteImageModelTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Upload Image |
23
|
|
|
* |
24
|
|
|
* Perform the image upload process |
25
|
|
|
* |
26
|
|
|
* @param $file |
27
|
|
|
* @param $name |
28
|
|
|
* @return \HustleWorks\Chute\ServiceResponse |
29
|
|
|
*/ |
30
|
|
|
public function uploadImage($file, $name) |
31
|
|
|
{ |
32
|
|
|
/* upload file */ |
33
|
|
|
$response = (new ImageUploadService)->handle($file, $this->imageUploadConfig($name)); |
34
|
|
|
|
35
|
|
|
/* queue processing */ |
36
|
|
|
dispatch(new ImageProcessingJob($response->data(), $this->imageUploadConfig($name))); |
37
|
|
|
|
38
|
|
|
return $response; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Image URl |
43
|
|
|
* |
44
|
|
|
* @param $name |
45
|
|
|
* @param null $size |
46
|
|
|
* @return \HustleWorks\ChuteLaravel\Models\ImageTransformation|string |
47
|
|
|
*/ |
48
|
|
|
public function imageUrl($name, $size = null) |
49
|
|
|
{ |
50
|
|
|
/** @var Image $image */ |
51
|
|
|
$image = $this->images()->where('name', $name)->first(); |
52
|
|
|
|
53
|
|
|
return $size ? $image->getTransformationUrl($size) : $image->getOriginalUrl(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Image Upload Configuration |
58
|
|
|
* |
59
|
|
|
* @param null $image_name |
60
|
|
|
* @return ImageConfiguration |
61
|
|
|
*/ |
62
|
|
|
public function imageUploadConfig($image_name = null) |
63
|
|
|
{ |
64
|
|
|
return (new ImageConfigurationBuilder())->getConfig( |
65
|
|
|
$image_name, |
66
|
|
|
$this->chute_image_rule[ $image_name ], |
67
|
|
|
$this->chute_image_sizes[ $image_name ], |
68
|
|
|
[ |
69
|
|
|
'imageable_type' => $this->getMorphClass(), |
|
|
|
|
70
|
|
|
'imageable_id' => $this->id, |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Images |
77
|
|
|
* |
78
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
79
|
|
|
*/ |
80
|
|
|
private function images() |
81
|
|
|
{ |
82
|
|
|
return $this->morphMany(Image::class, 'imageable'); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.