Completed
Push — master ( 7a439c...5a698e )
by Freek
02:18
created

addConversionsFromRelatedModel()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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