Completed
Push — master ( 5633c4...87cf42 )
by Brent
09:40 queued 07:11
created

Conversion::getResultExtension()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
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)
85
    {
86
        $this->manipulations->removeManipulation($manipulationName);
87
88
        return $this;
89
    }
90
91
    public function __call($name, $arguments)
92
    {
93
        if (! method_exists($this->manipulations, $name)) {
94
            throw new BadMethodCallException("Manipulation `{$name}` does not exist");
95
        }
96
97
        $this->manipulations->$name(...$arguments);
98
99
        return $this;
100
    }
101
102
    /**
103
     * Set the manipulations for this conversion.
104
     *
105
     * @param \Spatie\Image\Manipulations|closure $manipulations
106
     *
107
     * @return $this
108
     */
109
    public function setManipulations($manipulations)
110
    {
111
        if ($manipulations instanceof Manipulations) {
112
            $this->manipulations = $this->manipulations->mergeManipulations($manipulations);
113
        }
114
115
        if (is_callable($manipulations)) {
116
            $manipulations($this->manipulations);
117
        }
118
119
        return $this;
120
    }
121
122
    /**
123
     * Add the given manipulations as the first ones.
124
     *
125
     * @param \Spatie\Image\Manipulations $manipulations
126
     *
127
     * @return $this
128
     */
129
    public function addAsFirstManipulations(Manipulations $manipulations)
130
    {
131
        $manipulationSequence = $manipulations->getManipulationSequence()->toArray();
132
133
        $this->manipulations
134
            ->getManipulationSequence()
135
            ->mergeArray($manipulationSequence);
136
137
        return $this;
138
    }
139
140
    /**
141
     * Set the collection names on which this conversion must be performed.
142
     *
143
     * @param  $collectionNames
144
     *
145
     * @return $this
146
     */
147
    public function performOnCollections(...$collectionNames)
148
    {
149
        $this->performOnCollections = $collectionNames;
150
151
        return $this;
152
    }
153
154
    /*
155
     * Determine if this conversion should be performed on the given
156
     * collection.
157
     */
158
    public function shouldBePerformedOn(string $collectionName): bool
159
    {
160
        //if no collections were specified, perform conversion on all collections
161
        if (! count($this->performOnCollections)) {
162
            return true;
163
        }
164
165
        if (in_array('*', $this->performOnCollections)) {
166
            return true;
167
        }
168
169
        return in_array($collectionName, $this->performOnCollections);
170
    }
171
172
    /**
173
     * Mark this conversion as one that should be queued.
174
     *
175
     * @return $this
176
     */
177
    public function queued()
178
    {
179
        $this->performOnQueue = true;
180
181
        return $this;
182
    }
183
184
    /**
185
     * Mark this conversion as one that should not be queued.
186
     *
187
     * @return $this
188
     */
189
    public function nonQueued()
190
    {
191
        $this->performOnQueue = false;
192
193
        return $this;
194
    }
195
196
    /**
197
     * Avoid optimization of the converted image.
198
     *
199
     * @return $this
200
     */
201
    public function nonOptimized()
202
    {
203
        $this->removeManipulation('optimize');
204
205
        return $this;
206
    }
207
208
    /**
209
     * When creating the converted image, responsive images will be created as well.
210
     */
211
    public function withResponsiveImages()
212
    {
213
        $this->generateResponsiveImages = true;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Determine if responsive images should be created for this conversion.
220
     */
221
    public function shouldGenerateResponsiveImages(): bool
222
    {
223
        return $this->generateResponsiveImages;
224
    }
225
226
    /*
227
     * Determine if the conversion should be queued.
228
     */
229
    public function shouldBeQueued(): bool
230
    {
231
        return $this->performOnQueue;
232
    }
233
234
    /*
235
     * Get the extension that the result of this conversion must have.
236
     */
237
    public function getResultExtension(string $originalFileExtension = ''): string
238
    {
239
        if ($this->shouldKeepOriginalImageFormat()) {
240
            return $originalFileExtension;
241
        }
242
243
        if ($manipulationArgument = $this->manipulations->getManipulationArgument('format')) {
244
            return $manipulationArgument;
245
        }
246
247
        return $originalFileExtension;
248
    }
249
250
    public function getConversionFile(string $file): string
251
    {
252
        $fileName = pathinfo($file, PATHINFO_FILENAME);
253
254
        $extension = $this->getResultExtension();
255
256
        if (! $extension) {
257
            $extension = pathinfo($file, PATHINFO_EXTENSION);
258
        }
259
260
        return "{$fileName}-{$this->getName()}.{$extension}";
261
    }
262
}
263