GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 526374...66f3b5 )
by Freek
04:13
created

Manipulations::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Image;
4
5
use ReflectionClass;
6
use League\Flysystem\FileNotFoundException;
7
use Spatie\Image\Exceptions\InvalidManipulation;
8
9
class Manipulations
10
{
11
    const CROP_TOP_LEFT = 'crop-top-left';
12
    const CROP_TOP = 'crop-top';
13
    const CROP_TOP_RIGHT = 'crop-top-right';
14
    const CROP_LEFT = 'crop-left';
15
    const CROP_CENTER = 'crop-center';
16
    const CROP_RIGHT = 'crop-right';
17
    const CROP_BOTTOM_LEFT = 'crop-bottom-left';
18
    const CROP_BOTTOM = 'crop-bottom';
19
    const CROP_BOTTOM_RIGHT = 'crop-bottom-right';
20
21
    const ORIENTATION_AUTO = 'auto';
22
    const ORIENTATION_90 = 90;
23
    const ORIENTATION_180 = 180;
24
    const ORIENTATION_270 = 270;
25
26
    const FIT_CONTAIN = 'contain';
27
    const FIT_MAX = 'max';
28
    const FIT_FILL = 'fill';
29
    const FIT_STRETCH = 'stretch';
30
    const FIT_CROP = 'crop';
31
32
    const BORDER_OVERLAY = 'overlay';
33
    const BORDER_SHRINK = 'shrink';
34
    const BORDER_EXPAND = 'expand';
35
36
    const FORMAT_JPG = 'jpg';
37
    const FORMAT_PJPG = 'pjpg';
38
    const FORMAT_PNG = 'png';
39
    const FORMAT_GIF = 'gif';
40
41
    const FILTER_GREYSCALE = 'greyscale';
42
    const FILTER_SEPIA = 'sepia';
43
44
    const UNIT_PIXELS = 'px';
45
    const UNIT_PERCENT = '%';
46
47
    const POSITION_TOP_LEFT = 'top-left';
48
    const POSITION_TOP = 'top';
49
    const POSITION_TOP_RIGHT = 'top-right';
50
    const POSITION_LEFT = 'left';
51
    const POSITION_CENTER = 'center';
52
    const POSITION_RIGHT = 'right';
53
    const POSITION_BOTTOM_LEFT = 'bottom-left';
54
    const POSITION_BOTTOM = 'bottom';
55
    const POSITION_BOTTOM_RIGHT = 'bottom-right';
56
57
    /** @var \Spatie\Image\ManipulationSequence */
58
    protected $manipulationSequence;
59
60
    public function __construct(array $manipulations = [])
61
    {
62
        if (! $this->hasMultipleConversions($manipulations)) {
63
            $manipulations = [$manipulations];
64
        }
65
66
        foreach ($manipulations as $manipulation) {
67
            $this->manipulationSequence = new ManipulationSequence($manipulation);
68
        }
69
    }
70
71
    /**
72
     * @param string $orientation
73
     *
74
     * @return $this
75
     *
76
     * @throws InvalidManipulation
77
     */
78 View Code Duplication
    public function orientation(string $orientation)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        if (! $this->validateManipulation($orientation, 'orientation')) {
81
            throw InvalidManipulation::invalidParameter(
82
                'orientation',
83
                $orientation,
84
                $this->getValidManipulationOptions('orientation')
85
            );
86
        }
87
88
        return $this->addManipulation('orientation', $orientation);
89
    }
90
91
    /**
92
     * @param string $cropMethod
93
     * @param int $width
94
     * @param int $height
95
     *
96
     * @return $this
97
     *
98
     * @throws InvalidManipulation
99
     */
100 View Code Duplication
    public function crop(string $cropMethod, int $width, int $height)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        if (! $this->validateManipulation($cropMethod, 'crop')) {
103
            throw InvalidManipulation::invalidParameter(
104
                'cropmethod',
105
                $cropMethod,
106
                $this->getValidManipulationOptions('crop')
107
            );
108
        }
109
110
        $this->width($width);
111
        $this->height($height);
112
113
        return $this->addManipulation('crop', $cropMethod);
114
    }
115
116
    /**
117
     * @param int $width
118
     * @param int $height
119
     * @param int $focalX Crop center X in percent
120
     * @param int $focalY Crop center Y in percent
121
     *
122
     * @return $this
123
     */
124
    public function focalCrop(int $width, int $height, int $focalX, int $focalY)
125
    {
126
        $this->width($width);
127
        $this->height($height);
128
129
        return $this->addManipulation('crop', "crop-{$focalX}-{$focalY}");
130
    }
131
132
    /**
133
     * @param int $width
134
     * @param int $height
135
     * @param int $x
136
     * @param int $y
137
     *
138
     * @return $this
139
     *
140
     * @throws InvalidManipulation
141
     */
142
    public function manualCrop(int $width, int $height, int $x, int $y)
143
    {
144
        if ($width < 0) {
145
            throw InvalidManipulation::invalidWidth($width);
146
        }
147
148
        if ($height < 0) {
149
            throw InvalidManipulation::invalidWidth($height);
150
        }
151
152
        return $this->addManipulation('manualCrop', "{$width},{$height},{$x},{$y}");
153
    }
154
155
    /**
156
     * @param int $width
157
     *
158
     * @return $this
159
     *
160
     * @throws InvalidManipulation
161
     */
162
    public function width(int $width)
163
    {
164
        if ($width < 0) {
165
            throw InvalidManipulation::invalidWidth($width);
166
        }
167
168
        return $this->addManipulation('width', $width);
169
    }
170
171
    /**
172
     * @param int $height
173
     *
174
     * @return $this
175
     *
176
     * @throws InvalidManipulation
177
     */
178
    public function height(int $height)
179
    {
180
        if ($height < 0) {
181
            throw InvalidManipulation::invalidWidth($height);
182
        }
183
184
        return $this->addManipulation('height', $height);
185
    }
186
187
    /**
188
     * @param string $fitMethod
189
     * @param int $width
190
     * @param int $height
191
     *
192
     * @return $this
193
     *
194
     * @throws InvalidManipulation
195
     */
196 View Code Duplication
    public function fit(string $fitMethod, int $width, int $height)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
    {
198
        if (! $this->validateManipulation($fitMethod, 'fit')) {
199
            throw InvalidManipulation::invalidParameter(
200
                'fit',
201
                $fitMethod,
202
                $this->getValidManipulationOptions('fit')
203
            );
204
        }
205
206
        $this->width($width);
207
        $this->height($height);
208
209
        return $this->addManipulation('fit', $fitMethod);
210
    }
211
212
    /**
213
     * @param int $ratio A value between 1 and 8
214
     *
215
     * @return $this
216
     *
217
     * @throws InvalidManipulation
218
     */
219
    public function devicePixelRatio(int $ratio)
220
    {
221
        if ($ratio < 1 || $ratio > 8) {
222
            throw InvalidManipulation::valueNotInRange('ratio', $ratio, 1, 8);
223
        }
224
225
        return $this->addManipulation('devicePixelRatio', $ratio);
226
    }
227
228
    /**
229
     * @param int $brightness A value between -100 and 100
230
     *
231
     * @return $this
232
     *
233
     * @throws InvalidManipulation
234
     */
235
    public function brightness(int $brightness)
236
    {
237
        if ($brightness < -100 || $brightness > 100) {
238
            throw InvalidManipulation::valueNotInRange('brightness', $brightness, -100, 100);
239
        }
240
241
        return $this->addManipulation('brightness', $brightness);
242
    }
243
244
    /**
245
     * @param float $gamma A value between 0.01 and 9.99
246
     *
247
     * @return $this
248
     *
249
     * @throws InvalidManipulation
250
     */
251
    public function gamma(float $gamma)
252
    {
253
        if ($gamma < 0.01 || $gamma > 9.99) {
254
            throw InvalidManipulation::valueNotInRange('gamma', $gamma, 0.01, 9.00);
255
        }
256
257
        return $this->addManipulation('gamma', $gamma);
258
    }
259
260
    /**
261
     * @param int $contrast A value between -100 and 100
262
     *
263
     * @return $this
264
     *
265
     * @throws InvalidManipulation
266
     */
267
    public function contrast(int $contrast)
268
    {
269
        if ($contrast < -100 || $contrast > 100) {
270
            throw InvalidManipulation::valueNotInRange('contrast', $contrast, -100, 100);
271
        }
272
273
        return $this->addManipulation('contrast', $contrast);
274
    }
275
276
    /**
277
     * @param int $sharpen A value between 0 and 100
278
     *
279
     * @return $this
280
     *
281
     * @throws InvalidManipulation
282
     */
283
    public function sharpen(int $sharpen)
284
    {
285
        if ($sharpen < 0 || $sharpen > 100) {
286
            throw InvalidManipulation::valueNotInRange('sharpen', $sharpen, 0, 100);
287
        }
288
289
        return $this->addManipulation('sharpen', $sharpen);
290
    }
291
292
    /**
293
     * @param int $blur A value between 0 and 100
294
     *
295
     * @return $this
296
     *
297
     * @throws InvalidManipulation
298
     */
299 View Code Duplication
    public function blur(int $blur)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
300
    {
301
        if ($blur < 0 || $blur > 100) {
302
            throw InvalidManipulation::valueNotInRange('blur', $blur, 0, 100);
303
        }
304
305
        return $this->addManipulation('blur', $blur);
306
    }
307
308
    /**
309
     * @param int $pixelate A value between 0 and 1000
310
     *
311
     * @return $this
312
     *
313
     * @throws InvalidManipulation
314
     */
315
    public function pixelate(int $pixelate)
316
    {
317
        if ($pixelate < 0 || $pixelate > 1000) {
318
            throw InvalidManipulation::valueNotInRange('pixelate', $pixelate, 0, 1000);
319
        }
320
321
        return $this->addManipulation('pixelate', $pixelate);
322
    }
323
324
    /**
325
     * @return $this
326
     */
327
    public function greyscale()
328
    {
329
        return $this->filter('greyscale');
330
    }
331
332
    /**
333
     * @return $this
334
     */
335
    public function sepia()
336
    {
337
        return $this->filter('sepia');
338
    }
339
340
    /**
341
     * @param string $colorName
342
     *
343
     * @return $this
344
     */
345
    public function background(string $colorName)
346
    {
347
        return $this->addManipulation('background', $colorName);
348
    }
349
350
    /**
351
     * @param int $width
352
     * @param string $color
353
     * @param string $borderType
354
     *
355
     * @return $this
356
     *
357
     * @throws InvalidManipulation
358
     */
359
    public function border(int $width, string $color, string $borderType = 'overlay')
360
    {
361
        if ($width < 0) {
362
            throw InvalidManipulation::invalidWidth($width);
363
        }
364
365
        if (! $this->validateManipulation($borderType, 'border')) {
366
            throw InvalidManipulation::invalidParameter(
367
                'border',
368
                $borderType,
369
                $this->getValidManipulationOptions('border')
370
            );
371
        }
372
373
        return $this->addManipulation('border', "{$width},{$color},{$borderType}");
374
    }
375
376
    /**
377
     * @param int $quality
378
     *
379
     * @return $this
380
     *
381
     * @throws InvalidManipulation
382
     */
383 View Code Duplication
    public function quality(int $quality)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
384
    {
385
        if ($quality < 0 || $quality > 100) {
386
            throw InvalidManipulation::valueNotInRange('quality', $quality, 0, 100);
387
        }
388
389
        return $this->addManipulation('quality', $quality);
390
    }
391
392
    /**
393
     * @param string $format
394
     *
395
     * @return $this
396
     *
397
     * @throws InvalidManipulation
398
     */
399 View Code Duplication
    public function format(string $format)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
400
    {
401
        if (! $this->validateManipulation($format, 'format')) {
402
            throw InvalidManipulation::invalidParameter(
403
                'format',
404
                $format,
405
                $this->getValidManipulationOptions('format')
406
            );
407
        }
408
409
        return $this->addManipulation('format', $format);
410
    }
411
412
    /**
413
     * @param string $filterName
414
     *
415
     * @return $this
416
     *
417
     * @throws InvalidManipulation
418
     */
419 View Code Duplication
    protected function filter(string $filterName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
420
    {
421
        if (! $this->validateManipulation($filterName, 'filter')) {
422
            throw InvalidManipulation::invalidParameter(
423
                'filter',
424
                $filterName,
425
                $this->getValidManipulationOptions('filter')
426
            );
427
        }
428
429
        return $this->addManipulation('filter', $filterName);
430
    }
431
432
    /**
433
     * @param string $filePath
434
     *
435
     * @return $this
436
     *
437
     * @throws FileNotFoundException
438
     */
439
    public function watermark(string $filePath)
440
    {
441
        if (! file_exists($filePath)) {
442
            throw new FileNotFoundException($filePath);
443
        }
444
445
        $this->addManipulation('watermark', $filePath);
446
447
        return $this;
448
    }
449
450
    /**
451
     * @param int    $width The width of the watermark in pixels (default) or percent.
452
     * @param string $unit  The unit of the `$width` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
453
     *
454
     * @return $this
455
     */
456
    public function watermarkWidth(int $width, string $unit = 'px')
457
    {
458
        $width = ($unit == static::UNIT_PERCENT ? $width.'w' : $width);
459
460
        return $this->addManipulation('watermarkWidth', $width);
461
    }
462
463
    /**
464
     * @param int    $height The height of the watermark in pixels (default) or percent.
465
     * @param string $unit   The unit of the `$height` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
466
     *
467
     * @return $this
468
     */
469
    public function watermarkHeight(int $height, string $unit = 'px')
470
    {
471
        $height = ($unit == static::UNIT_PERCENT ? $height.'h' : $height);
472
473
        return $this->addManipulation('watermarkHeight', $height);
474
    }
475
476
    /**
477
     * @param string $fitMethod How is the watermark fitted into the watermarkWidth and watermarkHeight properties.
478
     *
479
     * @return $this
480
     *
481
     * @throws InvalidManipulation
482
     */
483 View Code Duplication
    public function watermarkFit(string $fitMethod)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
484
    {
485
        if (! $this->validateManipulation($fitMethod, 'fit')) {
486
            throw InvalidManipulation::invalidParameter(
487
                'watermarkFit',
488
                $fitMethod,
489
                $this->getValidManipulationOptions('fit')
490
            );
491
        }
492
493
        return $this->addManipulation('watermarkFit', $fitMethod);
494
    }
495
496
    /**
497
     * @param int $xPadding         How far is the watermark placed from the left and right edges of the image.
498
     * @param int|null $yPadding    How far is the watermark placed from the top and bottom edges of the image.
499
     * @param string $unit          Unit of the padding values. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
500
     *
501
     * @return $this
502
     */
503
    public function watermarkPadding(int $xPadding, int $yPadding = null, string $unit = 'px')
504
    {
505
        $yPadding = $yPadding ?? $xPadding;
506
507
        $xPadding = ($unit == static::UNIT_PERCENT ? $xPadding.'w' : $xPadding);
508
        $yPadding = ($unit == static::UNIT_PERCENT ? $yPadding.'h' : $yPadding);
509
510
        $this->addManipulation('watermarkPaddingX', $xPadding);
511
        $this->addManipulation('watermarkPaddingY', $yPadding);
512
513
        return $this;
514
    }
515
516
    /**
517
     * @param string $position
518
     *
519
     * @return $this
520
     *
521
     * @throws InvalidManipulation
522
     */
523 View Code Duplication
    public function watermarkPosition(string $position)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
524
    {
525
        if (! $this->validateManipulation($position, 'position')) {
526
            throw InvalidManipulation::invalidParameter(
527
                'watermarkPosition',
528
                $position,
529
                $this->getValidManipulationOptions('position')
530
            );
531
        }
532
533
        return $this->addManipulation('watermarkPosition', $position);
534
    }
535
536
    /**
537
     * Sets the opacity of the watermark. Only works with the `imagick` driver.
538
     *
539
     * @param int $opacity A value between 0 and 100.
540
     *
541
     * @return $this
542
     *
543
     * @throws InvalidManipulation
544
     */
545
    public function watermarkOpacity(int $opacity)
546
    {
547
        if ($opacity < 0 || $opacity > 100) {
548
            throw InvalidManipulation::valueNotInRange('opacity', $opacity, 0, 100);
549
        }
550
551
        return $this->addManipulation('watermarkOpacity', $opacity);
552
    }
553
554
    /**
555
     * @return $this
556
     */
557
    public function apply()
558
    {
559
        $this->manipulationSequence->startNewGroup();
560
561
        return $this;
562
    }
563
564
    /**
565
     * Create new manipulations class.
566
     *
567
     * @param array $manipulations
568
     *
569
     * @return self
570
     */
571
    public static function create(array $manipulations = [])
572
    {
573
        return new self($manipulations);
574
    }
575
576
    public function toArray() : array
577
    {
578
        return $this->manipulationSequence->toArray();
579
    }
580
581
    /**
582
     * Checks if the given manipulations has arrays inside or not.
583
     *
584
     * @param  array $manipulations
585
     *
586
     * @return bool
587
     */
588
    private function hasMultipleConversions(array $manipulations) : bool
589
    {
590
        foreach ($manipulations as $manipulation) {
591
            if (isset($manipulation[0]) && is_array($manipulation[0])) {
592
                return true;
593
            }
594
        }
595
596
        return false;
597
    }
598
599
    public function removeManipulation(string $name)
600
    {
601
        $this->manipulationSequence->removeManipulation($name);
602
    }
603
604
    public function hasManipulation(string $manipulationName): bool
605
    {
606
        return ! is_null($this->getManipulationArgument($manipulationName));
607
    }
608
609
    /**
610
     * @param string $manipulationName
611
     *
612
     * @return string|null
613
     */
614
    public function getManipulationArgument(string $manipulationName)
615
    {
616
        foreach ($this->manipulationSequence->getGroups() as $manipulationSet) {
617
            if (array_key_exists($manipulationName, $manipulationSet)) {
618
                return $manipulationSet[$manipulationName];
619
            }
620
        }
621
    }
622
623
    protected function addManipulation(string $manipulationName, string $manipulationArgument)
624
    {
625
        $this->manipulationSequence->addManipulation($manipulationName, $manipulationArgument);
626
627
        return $this;
628
    }
629
630
    public function mergeManipulations(Manipulations $manipulations)
631
    {
632
        $this->manipulationSequence->merge($manipulations->manipulationSequence);
633
634
        return $this;
635
    }
636
637
    public function getManipulationSequence(): ManipulationSequence
638
    {
639
        return $this->manipulationSequence;
640
    }
641
642
    protected function validateManipulation(string $value, string $constantNamePrefix): bool
643
    {
644
        return in_array($value, $this->getValidManipulationOptions($constantNamePrefix));
645
    }
646
647
    protected function getValidManipulationOptions(string $manipulation): array
648
    {
649
        $options = (new ReflectionClass(static::class))->getConstants();
650
651
        return array_filter($options, function ($value, $name) use ($manipulation) {
652
            return strpos($name, strtoupper($manipulation)) === 0;
653
        }, ARRAY_FILTER_USE_BOTH);
654
    }
655
656
    public function isEmpty(): bool
657
    {
658
        return $this->manipulationSequence->isEmpty();
659
    }
660
}
661