Completed
Pull Request — master (#898)
by
unknown
01:43
created

Conversion::__call()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.4751
c 0
b 0
f 0
cc 6
eloc 25
nc 6
nop 2
1
<?php
2
3
namespace Spatie\MediaLibrary\Conversion;
4
5
use BadMethodCallException;
6
use Spatie\Image\Manipulations;
7
use Illuminate\Support\Collection;
8
use Spatie\MediaLibrary\ImageGenerators\ImageGenerator;
9
10
/** @mixin \Spatie\Image\Manipulations */
11
class Conversion
12
{
13
    /** @var string */
14
    protected $name = '';
15
16
    /** @var int */
17
    protected $extractVideoFrameAtSecond = 0;
18
19
    /** @var \Spatie\Image\Manipulations */
20
    protected $manipulations;
21
22
    /** @var Illuminate\Support\Collection */
23
    protected $generatorParams;
24
25
    /** @var array */
26
    protected $performOnCollections = [];
27
28
    /** @var bool */
29
    protected $performOnQueue = true;
30
31
    /** @var bool */
32
    protected $keepOriginalImageFormat = false;
33
34
    public function __construct(string $name)
35
    {
36
        $this->name = $name;
37
38
        $this->manipulations = (new Manipulations())
39
            ->optimize(config('medialibrary.image_optimizers'))
40
            ->format('jpg');
41
42
        $this->generatorParams = collect();
43
        $this->getImageGenerators()
44
            ->map(function (string $className) {
45
                $generator = app($className);
46
47
                $this->generatorParams[$className] = collect($generator->getParams());
48
            });
49
    }
50
51
    public static function create(string $name)
52
    {
53
        return new static($name);
54
    }
55
56
    public function getName(): string
57
    {
58
        return $this->name;
59
    }
60
61
    public function keepOriginalImageFormat(): Conversion
62
    {
63
        $this->keepOriginalImageFormat = true;
64
65
        return $this;
66
    }
67
68
    public function shouldKeepOriginalImageFormat(): Bool
69
    {
70
        return $this->keepOriginalImageFormat;
71
    }
72
73
    public function getManipulations(): Manipulations
74
    {
75
        return $this->manipulations;
76
    }
77
78
    public function removeManipulation(string $manipulationName)
79
    {
80
        $this->manipulations->removeManipulation($manipulationName);
81
82
        return $this;
83
    }
84
85
    public function __call($name, $arguments)
86
    {
87
        // Manipulation call
88
        if (method_exists($this->manipulations, $name)) {
89
            $this->manipulations->$name(...$arguments);
90
91
            return $this;
92
        }
93
94
        // Get ImageGenerator param call
95
        if (preg_match('/^get(.*)$/', $name, $getParamMatch)) {
96
            $paramName = strtolower($getParamMatch[1]);
97
            $validGeneratorParams = $this->generatorParams
98
                ->filter(function ($generatorParams) use ($paramName) {
99
                    return $generatorParams->has($paramName);
100
                });
101
102
            if (count($validGeneratorParams)) {
103
                $firstValidGeneratorParams = $validGeneratorParams->first();
104
                if ($firstValidGeneratorParams) {
105
                    return $firstValidGeneratorParams[$paramName];
106
                }
107
            }
108
109
            throw new BadMethodCallException("Generator Parameter `{$paramName}` does not exist");
110
        }
111
112
        // Set ImageGenerator param call
113
        $paramName = strtolower($name);
114
        $settableGeneratorParamGroups = $this->generatorParams
115
            ->filter(function ($generatorParams) use ($paramName) {
116
                return $generatorParams->has($paramName);
117
            });
118
119
        if (count($settableGeneratorParamGroups)) {
120
            $value = $arguments[0];
121
            $settableGeneratorParamGroups
122
                ->map(function ($settableGeneratorParams) use ($paramName, $value) {
123
                    $settableGeneratorParams[$paramName] = $value;
124
                });
125
126
                return $this;
127
        }
128
129
        throw new BadMethodCallException("Manipulation or Generator Parameter `{$name}` does not exist");
130
    }
131
132
    /**
133
     * Set the manipulations for this conversion.
134
     *
135
     * @param \Spatie\Image\Manipulations|closure $manipulations
136
     *
137
     * @return $this
138
     */
139
    public function setManipulations($manipulations)
140
    {
141
        if ($manipulations instanceof Manipulations) {
142
            $this->manipulations = $this->manipulations->mergeManipulations($manipulations);
143
        }
144
145
        if (is_callable($manipulations)) {
146
            $manipulations($this->manipulations);
147
        }
148
149
        return $this;
150
    }
151
152
    /**
153
     * Add the given manipulations as the first ones.
154
     *
155
     * @param \Spatie\Image\Manipulations $manipulations
156
     *
157
     * @return $this
158
     */
159
    public function addAsFirstManipulations(Manipulations $manipulations)
160
    {
161
        $manipulationSequence = $manipulations->getManipulationSequence()->toArray();
162
163
        $this->manipulations
164
            ->getManipulationSequence()
165
            ->mergeArray($manipulationSequence);
166
167
        return $this;
168
    }
169
170
    /**
171
     * Set the collection names on which this conversion must be performed.
172
     *
173
     * @param  $collectionNames
174
     *
175
     * @return $this
176
     */
177
    public function performOnCollections(...$collectionNames)
178
    {
179
        $this->performOnCollections = $collectionNames;
180
181
        return $this;
182
    }
183
184
    /*
185
     * Determine if this conversion should be performed on the given
186
     * collection.
187
     */
188
    public function shouldBePerformedOn(string $collectionName): bool
189
    {
190
        //if no collections were specified, perform conversion on all collections
191
        if (! count($this->performOnCollections)) {
192
            return true;
193
        }
194
195
        if (in_array('*', $this->performOnCollections)) {
196
            return true;
197
        }
198
199
        return in_array($collectionName, $this->performOnCollections);
200
    }
201
202
    /**
203
     * Mark this conversion as one that should be queued.
204
     *
205
     * @return $this
206
     */
207
    public function queued()
208
    {
209
        $this->performOnQueue = true;
210
211
        return $this;
212
    }
213
214
    /**
215
     * Mark this conversion as one that should not be queued.
216
     *
217
     * @return $this
218
     */
219
    public function nonQueued()
220
    {
221
        $this->performOnQueue = false;
222
223
        return $this;
224
    }
225
226
    /**
227
     * Avoid optimization of the converted image.
228
     *
229
     * @return $this
230
     */
231
    public function nonOptimized()
232
    {
233
        $this->removeManipulation('optimize');
234
235
        return $this;
236
    }
237
238
    /*
239
     * Determine if the conversion should be queued.
240
     */
241
    public function shouldBeQueued(): bool
242
    {
243
        return $this->performOnQueue;
244
    }
245
246
    /*
247
     * Get the extension that the result of this conversion must have.
248
     */
249
    public function getResultExtension(string $originalFileExtension = ''): string
250
    {
251
        if ($this->shouldKeepOriginalImageFormat()) {
252
            return $originalFileExtension;
253
        }
254
255
        if ($manipulationArgument = $this->manipulations->getManipulationArgument('format')) {
256
            return $manipulationArgument;
257
        }
258
259
        return $originalFileExtension;
260
    }
261
262
    /**
263
     * Collection of all ImageGenerator drivers.
264
     */
265
    protected function getImageGenerators(): Collection
266
    {
267
        return collect(config('medialibrary.image_generators'));
268
    }
269
}
270