Completed
Push — master ( f8e51c...b0a057 )
by Freek
01:09
created

Conversion::getPerformOnCollections()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary\Conversion;
4
5
use BadMethodCallException;
6
use Spatie\Image\Manipulations;
7
8
/** @mixin \Spatie\Image\Manipulations */
9
class Conversion
10
{
11
    /** @var string */
12
    protected $name = '';
13
14
    /** @var int */
15
    protected $extractVideoFrameAtSecond = 0;
16
17
    /** @var \Spatie\Image\Manipulations */
18
    protected $manipulations;
19
20
    /** @var array */
21
    protected $performOnCollections = [];
22
23
    /** @var bool */
24
    protected $performOnQueue = true;
25
26
    /** @var bool */
27
    protected $keepOriginalImageFormat = false;
28
29
    /** @var bool */
30
    protected $generateResponsiveImages = false;
31
32
    public function __construct(string $name)
33
    {
34
        $this->name = $name;
35
36
        $this->manipulations = (new Manipulations())
37
            ->optimize(config('medialibrary.image_optimizers'))
38
            ->format('jpg');
39
    }
40
41
    public static function create(string $name)
42
    {
43
        return new static($name);
44
    }
45
46
    public function getName(): string
47
    {
48
        return $this->name;
49
    }
50
51
    /*
52
     * Set the timecode in seconds to extract a video thumbnail.
53
     * Only used on video media.
54
     */
55
    public function extractVideoFrameAtSecond(int $timeCode): self
56
    {
57
        $this->extractVideoFrameAtSecond = $timeCode;
58
59
        return $this;
60
    }
61
62
    public function getExtractVideoFrameAtSecond(): int
63
    {
64
        return $this->extractVideoFrameAtSecond;
65
    }
66
67
    public function keepOriginalImageFormat(): self
68
    {
69
        $this->keepOriginalImageFormat = true;
70
71
        return $this;
72
    }
73
74
    public function shouldKeepOriginalImageFormat(): bool
75
    {
76
        return $this->keepOriginalImageFormat;
77
    }
78
79
    public function getManipulations(): Manipulations
80
    {
81
        return $this->manipulations;
82
    }
83
84
    public function removeManipulation(string $manipulationName) : self
85
    {
86
        $this->manipulations->removeManipulation($manipulationName);
87
88
        return $this;
89
    }
90
91
    public function withoutManipulations() : self
92
    {
93
        $this->manipulations = new Manipulations();
94
95
        return $this;
96
    }
97
98
    public function __call($name, $arguments)
99
    {
100
        if (! method_exists($this->manipulations, $name)) {
101
            throw new BadMethodCallException("Manipulation `{$name}` does not exist");
102
        }
103
104
        $this->manipulations->$name(...$arguments);
105
106
        return $this;
107
    }
108
109
    /**
110
     * Set the manipulations for this conversion.
111
     *
112
     * @param \Spatie\Image\Manipulations|\Closure $manipulations
113
     *
114
     * @return $this
115
     */
116
    public function setManipulations($manipulations) : self
117
    {
118
        if ($manipulations instanceof Manipulations) {
0 ignored issues
show
Bug introduced by
The class Spatie\Image\Manipulations does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
119
            $this->manipulations = $this->manipulations->mergeManipulations($manipulations);
120
        }
121
122
        if (is_callable($manipulations)) {
123
            $manipulations($this->manipulations);
124
        }
125
126
        return $this;
127
    }
128
129
    /**
130
     * Add the given manipulations as the first ones.
131
     *
132
     * @param \Spatie\Image\Manipulations $manipulations
133
     *
134
     * @return $this
135
     */
136
    public function addAsFirstManipulations(Manipulations $manipulations) : self
137
    {
138
        $manipulationSequence = $manipulations->getManipulationSequence()->toArray();
139
140
        $this->manipulations
141
            ->getManipulationSequence()
142
            ->mergeArray($manipulationSequence);
143
144
        return $this;
145
    }
146
147
    /**
148
     * Set the collection names on which this conversion must be performed.
149
     *
150
     * @param  $collectionNames
151
     *
152
     * @return $this
153
     */
154
    public function performOnCollections(...$collectionNames) : self
155
    {
156
        $this->performOnCollections = $collectionNames;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Returns the collection names on which this conversion must be performed.
163
     * Assumes 'default' when none is present.
164
     *
165
     * @return array
166
     */
167
    public function getPerformOnCollections()
168
    {
169
        if (! count($this->performOnCollections)) {
170
            return ['default'];
171
        }
172
173
        return $this->performOnCollections;
174
    }
175
176
    /*
177
     * Determine if this conversion should be performed on the given
178
     * collection.
179
     */
180
    public function shouldBePerformedOn(string $collectionName): bool
181
    {
182
        //if no collections were specified, perform conversion on all collections
183
        if (! count($this->performOnCollections)) {
184
            return true;
185
        }
186
187
        if (in_array('*', $this->performOnCollections)) {
188
            return true;
189
        }
190
191
        return in_array($collectionName, $this->performOnCollections);
192
    }
193
194
    /**
195
     * Mark this conversion as one that should be queued.
196
     *
197
     * @return $this
198
     */
199
    public function queued() : self
200
    {
201
        $this->performOnQueue = true;
202
203
        return $this;
204
    }
205
206
    /**
207
     * Mark this conversion as one that should not be queued.
208
     *
209
     * @return $this
210
     */
211
    public function nonQueued() : self
212
    {
213
        $this->performOnQueue = false;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Avoid optimization of the converted image.
220
     *
221
     * @return $this
222
     */
223
    public function nonOptimized() : self
224
    {
225
        $this->removeManipulation('optimize');
226
227
        return $this;
228
    }
229
230
    /**
231
     * When creating the converted image, responsive images will be created as well.
232
     */
233
    public function withResponsiveImages() : self
234
    {
235
        $this->generateResponsiveImages = true;
236
237
        return $this;
238
    }
239
240
    /**
241
     * Determine if responsive images should be created for this conversion.
242
     */
243
    public function shouldGenerateResponsiveImages(): bool
244
    {
245
        return $this->generateResponsiveImages;
246
    }
247
248
    /*
249
     * Determine if the conversion should be queued.
250
     */
251
    public function shouldBeQueued(): bool
252
    {
253
        return $this->performOnQueue;
254
    }
255
256
    /*
257
     * Get the extension that the result of this conversion must have.
258
     */
259
    public function getResultExtension(string $originalFileExtension = ''): string
260
    {
261
        if ($this->shouldKeepOriginalImageFormat()) {
262
            if (in_array($originalFileExtension, ['jpg', 'jpeg', 'pjpg', 'png', 'gif'])) {
263
                return $originalFileExtension;
264
            }
265
        }
266
267
        if ($manipulationArgument = $this->manipulations->getManipulationArgument('format')) {
268
            return $manipulationArgument;
269
        }
270
271
        return $originalFileExtension;
272
    }
273
274
    public function getConversionFile(string $file): string
275
    {
276
        $fileName = pathinfo($file, PATHINFO_FILENAME);
277
278
        $extension = $this->getResultExtension();
279
280
        if (! $extension) {
281
            $extension = pathinfo($file, PATHINFO_EXTENSION);
282
        }
283
284
        return "{$fileName}-{$this->getName()}.{$extension}";
285
    }
286
}
287