Conversion   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 2

Importance

Changes 0
Metric Value
wmc 34
lcom 5
cbo 2
dl 0
loc 269
rs 9.68
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A create() 0 4 1
A getName() 0 4 1
A getPerformOnCollections() 0 8 2
A extractVideoFrameAtSecond() 0 6 1
A getExtractVideoFrameAtSecond() 0 4 1
A keepOriginalImageFormat() 0 6 1
A shouldKeepOriginalImageFormat() 0 4 1
A getManipulations() 0 4 1
A removeManipulation() 0 6 1
A withoutManipulations() 0 6 1
A __call() 0 10 2
A setManipulations() 0 12 3
A addAsFirstManipulations() 0 10 1
A performOnCollections() 0 6 1
A shouldBePerformedOn() 0 13 3
A queued() 0 6 1
A nonQueued() 0 6 1
A nonOptimized() 0 6 1
A withResponsiveImages() 0 6 1
A shouldGenerateResponsiveImages() 0 4 1
A shouldBeQueued() 0 4 1
A getResultExtension() 0 14 4
A getConversionFile() 0 9 2
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(Manipulations::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
    public function getPerformOnCollections(): array
52
    {
53
        if (! count($this->performOnCollections)) {
54
            return ['default'];
55
        }
56
57
        return $this->performOnCollections;
58
    }
59
60
    /*
61
     * Set the timecode in seconds to extract a video thumbnail.
62
     * Only used on video media.
63
     */
64
    public function extractVideoFrameAtSecond(int $timeCode): self
65
    {
66
        $this->extractVideoFrameAtSecond = $timeCode;
67
68
        return $this;
69
    }
70
71
    public function getExtractVideoFrameAtSecond(): int
72
    {
73
        return $this->extractVideoFrameAtSecond;
74
    }
75
76
    public function keepOriginalImageFormat(): self
77
    {
78
        $this->keepOriginalImageFormat = true;
79
80
        return $this;
81
    }
82
83
    public function shouldKeepOriginalImageFormat(): bool
84
    {
85
        return $this->keepOriginalImageFormat;
86
    }
87
88
    public function getManipulations(): Manipulations
89
    {
90
        return $this->manipulations;
91
    }
92
93
    public function removeManipulation(string $manipulationName): self
94
    {
95
        $this->manipulations->removeManipulation($manipulationName);
96
97
        return $this;
98
    }
99
100
    public function withoutManipulations(): self
101
    {
102
        $this->manipulations = new Manipulations();
103
104
        return $this;
105
    }
106
107
    public function __call($name, $arguments)
108
    {
109
        if (! method_exists($this->manipulations, $name)) {
110
            throw new BadMethodCallException("Manipulation `{$name}` does not exist");
111
        }
112
113
        $this->manipulations->$name(...$arguments);
114
115
        return $this;
116
    }
117
118
    /**
119
     * Set the manipulations for this conversion.
120
     *
121
     * @param \Spatie\Image\Manipulations|\Closure $manipulations
122
     *
123
     * @return $this
124
     */
125
    public function setManipulations($manipulations): self
126
    {
127
        if ($manipulations instanceof Manipulations) {
128
            $this->manipulations = $this->manipulations->mergeManipulations($manipulations);
0 ignored issues
show
Documentation introduced by
$manipulations is of type object<Spatie\Image\Manipulations>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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