Completed
Push — master ( c2f76d...03586b )
by Freek
11:35
created

addConversionsFromRelatedModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Spatie\MediaLibrary\Conversion;
4
5
use Illuminate\Support\Arr;
6
use Spatie\MediaLibrary\HasMedia\HasMedia;
7
use Spatie\MediaLibrary\Models\Media;
8
use Spatie\Image\Manipulations;
9
use Illuminate\Support\Collection;
10
use Illuminate\Database\Eloquent\Relations\Relation;
11
use Spatie\MediaLibrary\Exceptions\InvalidConversion;
12
13
class ConversionCollection extends Collection
14
{
15
    /** @var \Spatie\MediaLibrary\Media  */
16
    protected $media;
17
18
    /**
19
     * @param \Spatie\MediaLibrary\Models\Media $media
20
     *
21
     * @return static
22
     */
23
    public static function createForMedia(Media $media)
24
    {
25
        return (new static())->setMedia($media);
26
    }
27
28
    /**
29
     * @param \Spatie\MediaLibrary\Models\Media $media
30
     *
31
     * @return $this
32
     */
33
    public function setMedia(Media $media)
34
    {
35
        $this->media = $media;
36
37
        $this->items = [];
38
39
        $this->addConversionsFromRelatedModel($media);
40
41
        $this->addManipulationsFromDb($media);
42
43
        return $this;
44
    }
45
46
    /**
47
     *  Get a conversion by it's name.
48
     *
49
     * @param string $name
50
     *
51
     * @return mixed
52
     *
53
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
54
     */
55
    public function getByName(string $name)
56
    {
57
        $conversion = $this->first(function (Conversion $conversion) use ($name) {
58
            return $conversion->getName() === $name;
59
        });
60
61
        if (! $conversion) {
62
            throw InvalidConversion::unknownName($name);
63
        }
64
65
        return $conversion;
66
    }
67
68
    /**
69
     * Add the conversion that are defined on the related model of
70
     * the given media.
71
     *
72
     * @param \Spatie\MediaLibrary\Models\Media $media
73
     */
74
    protected function addConversionsFromRelatedModel(Media $media)
75
    {
76
        $modelName = Arr::get(Relation::morphMap(), $media->model_type, $media->model_type);
77
78
        /** @var \Spatie\MediaLibrary\HasMedia\HasMedia $model */
79
        $model = new $modelName();
80
81
        /*
82
         * In some cases the user might want to get the actual model
83
         * instance so conversion parameters can depend on model
84
         * properties. This will causes extra queries.
85
         */
86
        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...
87
            $model = $media->model;
88
89
            $model->mediaConversion = [];
90
        }
91
92
        $model->registerAllMediaConversions($media);
93
94
        $this->items = $model->mediaConversions;
95
    }
96
97
    /**
98
     * Add the extra manipulations that are defined on the given media.
99
     *
100
     * @param \Spatie\MediaLibrary\Models\Media $media
101
     */
102
    protected function addManipulationsFromDb(Media $media)
103
    {
104
        collect($media->manipulations)->each(function ($manipulations, $conversionName) {
105
            $this->addManipulationToConversion(new Manipulations([$manipulations]), $conversionName);
106
        });
107
    }
108
109
    public function getConversions(string $collectionName = ''): self
110
    {
111
        if ($collectionName === '') {
112
            return $this;
113
        }
114
115
        return $this->filter->shouldBePerformedOn($collectionName);
116
    }
117
118
    /*
119
     * Get all the conversions in the collection that should be queued.
120
     */
121
    public function getQueuedConversions(string $collectionName = ''): self
122
    {
123
        return $this->getConversions($collectionName)->filter->shouldBeQueued();
124
    }
125
126
    /*
127
     * Add the given manipulation to the conversion with the given name.
128
     */
129
    protected function addManipulationToConversion(Manipulations $manipulations, string $conversionName)
130
    {
131
        optional($this->first(function (Conversion $conversion) use ($conversionName) {
132
            return $conversion->getName() === $conversionName;
133
        }))->addAsFirstManipulations($manipulations);
134
135
        if ($conversionName === '*') {
136
            $this->each->addAsFirstManipulations(clone $manipulations);
137
        }
138
    }
139
140
    /*
141
     * Get all the conversions in the collection that should not be queued.
142
     */
143
    public function getNonQueuedConversions(string $collectionName = ''): self
144
    {
145
        return $this->getConversions($collectionName)->reject->shouldBeQueued();
146
    }
147
148
    /*
149
     * Return the list of conversion files.
150
     */
151
    public function getConversionsFiles(string $collectionName = ''): self
152
    {
153
        $fileName = pathinfo($this->media->file_name, PATHINFO_FILENAME);
154
155
        return $this->getConversions($collectionName)->map(function (Conversion $conversion) use ($fileName) {
156
            return "{$fileName}-{$conversion->getName()}.{$conversion->getResultExtension()}";
157
        });
158
    }
159
}
160