ConversionCollection::addManipulationsFromDb()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\MediaLibrary\Conversion;
4
5
use Illuminate\Database\Eloquent\Relations\Relation;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
use Spatie\Image\Manipulations;
9
use Spatie\MediaLibrary\Exceptions\InvalidConversion;
10
use Spatie\MediaLibrary\Models\Media;
11
12
class ConversionCollection extends Collection
13
{
14
    /** @var \Spatie\MediaLibrary\Models\Media */
15
    protected $media;
16
17
    /**
18
     * @param \Spatie\MediaLibrary\Models\Media $media
19
     *
20
     * @return static
21
     */
22
    public static function createForMedia(Media $media)
23
    {
24
        return (new static())->setMedia($media);
25
    }
26
27
    /**
28
     * @param \Spatie\MediaLibrary\Models\Media $media
29
     *
30
     * @return $this
31
     */
32
    public function setMedia(Media $media)
33
    {
34
        $this->media = $media;
35
36
        $this->items = [];
37
38
        $this->addConversionsFromRelatedModel($media);
39
40
        $this->addManipulationsFromDb($media);
41
42
        return $this;
43
    }
44
45
    /**
46
     *  Get a conversion by it's name.
47
     *
48
     * @param string $name
49
     *
50
     * @return mixed
51
     *
52
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
53
     */
54
    public function getByName(string $name): Conversion
55
    {
56
        $conversion = $this->first(function (Conversion $conversion) use ($name) {
57
            return $conversion->getName() === $name;
58
        });
59
60
        if (! $conversion) {
61
            throw InvalidConversion::unknownName($name);
62
        }
63
64
        return $conversion;
65
    }
66
67
    /**
68
     * Add the conversion that are defined on the related model of
69
     * the given media.
70
     *
71
     * @param \Spatie\MediaLibrary\Models\Media $media
72
     */
73
    protected function addConversionsFromRelatedModel(Media $media)
74
    {
75
        $modelName = Arr::get(Relation::morphMap(), $media->model_type, $media->model_type);
76
77
        /** @var \Spatie\MediaLibrary\HasMedia\HasMedia $model */
78
        $model = new $modelName();
79
80
        /*
81
         * In some cases the user might want to get the actual model
82
         * instance so conversion parameters can depend on model
83
         * properties. This will causes extra queries.
84
         */
85
        if ($model->registerMediaConversionsUsingModelInstance) {
0 ignored issues
show
Bug introduced by
Accessing registerMediaConversionsUsingModelInstance on the interface Spatie\MediaLibrary\HasMedia\HasMedia suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
86
            $model = $media->model;
87
88
            $model->mediaConversion = [];
89
        }
90
91
        $model->registerAllMediaConversions($media);
92
93
        $this->items = $model->mediaConversions;
94
    }
95
96
    /**
97
     * Add the extra manipulations that are defined on the given media.
98
     *
99
     * @param \Spatie\MediaLibrary\Models\Media $media
100
     */
101
    protected function addManipulationsFromDb(Media $media)
102
    {
103
        collect($media->manipulations)->each(function ($manipulations, $conversionName) {
104
            $this->addManipulationToConversion(new Manipulations([$manipulations]), $conversionName);
105
        });
106
    }
107
108
    public function getConversions(string $collectionName = ''): self
109
    {
110
        if ($collectionName === '') {
111
            return $this;
112
        }
113
114
        return $this->filter->shouldBePerformedOn($collectionName);
115
    }
116
117
    /*
118
     * Get all the conversions in the collection that should be queued.
119
     */
120
    public function getQueuedConversions(string $collectionName = ''): self
121
    {
122
        return $this->getConversions($collectionName)->filter->shouldBeQueued();
123
    }
124
125
    /*
126
     * Add the given manipulation to the conversion with the given name.
127
     */
128
    protected function addManipulationToConversion(Manipulations $manipulations, string $conversionName)
129
    {
130
        /** @var \Spatie\MediaLibrary\Conversion\Conversion|null $conversion */
131
        $conversion = $this->first(function (Conversion $conversion) use ($conversionName) {
132
            if (! in_array($this->media->collection_name, $conversion->getPerformOnCollections())) {
133
                return false;
134
            }
135
136
            if ($conversion->getName() !== $conversionName) {
137
                return false;
138
            }
139
140
            return true;
141
        });
142
143
        if ($conversion) {
144
            $conversion->addAsFirstManipulations($manipulations);
145
        }
146
147
        if ($conversionName === '*') {
148
            $this->each->addAsFirstManipulations(clone $manipulations);
149
        }
150
    }
151
152
    /*
153
     * Get all the conversions in the collection that should not be queued.
154
     */
155
    public function getNonQueuedConversions(string $collectionName = ''): self
156
    {
157
        return $this->getConversions($collectionName)->reject->shouldBeQueued();
158
    }
159
160
    /*
161
     * Return the list of conversion files.
162
     */
163
    public function getConversionsFiles(string $collectionName = ''): self
164
    {
165
        $fileName = pathinfo($this->media->file_name, PATHINFO_FILENAME);
166
167
        return $this->getConversions($collectionName)->map(function (Conversion $conversion) use ($fileName) {
168
            return $conversion->getConversionFile($fileName);
169
        });
170
    }
171
}
172