ChuteImageModelTrait::images()   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\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(),
0 ignored issues
show
Bug introduced by
It seems like getMorphClass() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
83
    }
84
85
}