Completed
Push — master ( b375d3...089ee0 )
by Freek
01:52
created

Conversion::withoutManipulations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
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) {
119
            $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...
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
     * Determine if this conversion should be performed on the given
163
     * collection.
164
     */
165
    public function shouldBePerformedOn(string $collectionName): bool
166
    {
167
        //if no collections were specified, perform conversion on all collections
168
        if (! count($this->performOnCollections)) {
169
            return true;
170
        }
171
172
        if (in_array('*', $this->performOnCollections)) {
173
            return true;
174
        }
175
176
        return in_array($collectionName, $this->performOnCollections);
177
    }
178
179
    /**
180
     * Mark this conversion as one that should be queued.
181
     *
182
     * @return $this
183
     */
184
    public function queued() : self
185
    {
186
        $this->performOnQueue = true;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Mark this conversion as one that should not be queued.
193
     *
194
     * @return $this
195
     */
196
    public function nonQueued() : self
197
    {
198
        $this->performOnQueue = false;
199
200
        return $this;
201
    }
202
203
    /**
204
     * Avoid optimization of the converted image.
205
     *
206
     * @return $this
207
     */
208
    public function nonOptimized() : self
209
    {
210
        $this->removeManipulation('optimize');
211
212
        return $this;
213
    }
214
215
    /**
216
     * When creating the converted image, responsive images will be created as well.
217
     */
218
    public function withResponsiveImages() : self
219
    {
220
        $this->generateResponsiveImages = true;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Determine if responsive images should be created for this conversion.
227
     */
228
    public function shouldGenerateResponsiveImages(): bool
229
    {
230
        return $this->generateResponsiveImages;
231
    }
232
233
    /*
234
     * Determine if the conversion should be queued.
235
     */
236
    public function shouldBeQueued(): bool
237
    {
238
        return $this->performOnQueue;
239
    }
240
241
    /*
242
     * Get the extension that the result of this conversion must have.
243
     */
244
    public function getResultExtension(string $originalFileExtension = ''): string
245
    {
246
        if ($this->shouldKeepOriginalImageFormat()) {
247
            if (in_array($originalFileExtension, ['jpg', 'pjpg', 'png', 'gif'])) {
248
                return $originalFileExtension;
249
            }
250
        }
251
252
        if ($manipulationArgument = $this->manipulations->getManipulationArgument('format')) {
253
            return $manipulationArgument;
254
        }
255
256
        return $originalFileExtension;
257
    }
258
259
    public function getConversionFile(string $file): string
260
    {
261
        $fileName = pathinfo($file, PATHINFO_FILENAME);
262
263
        $extension = $this->getResultExtension();
264
265
        if (! $extension) {
266
            $extension = pathinfo($file, PATHINFO_EXTENSION);
267
        }
268
269
        return "{$fileName}-{$this->getName()}.{$extension}";
270
    }
271
}
272