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
Pull Request — master (#16)
by
unknown
02:02
created

Manipulations::checkIfHasMultipleConversions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
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
        //Check if is a single array or multi arrays
63
        if ($this->checkIfHasMultipleConversions($manipulations)) {
64
            foreach ($manipulations as $manipulation) {
65
                $this->manipulationSequence = new ManipulationSequence($manipulation);
66
            }
67
        } else {
68
            $this->manipulationSequence = new ManipulationSequence($manipulations);
69
        }
70
    }
71
72
    /**
73
     * @param string $orientation
74
     *
75
     * @return $this
76
     *
77
     * @throws InvalidManipulation
78
     */
79 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...
80
    {
81
        if (! $this->validateManipulation($orientation, 'orientation')) {
82
            throw InvalidManipulation::invalidParameter(
83
                'orientation',
84
                $orientation,
85
                $this->getValidManipulationOptions('orientation')
86
            );
87
        }
88
89
        return $this->addManipulation('orientation', $orientation);
90
    }
91
92
    /**
93
     * @param string $cropMethod
94
     * @param int $width
95
     * @param int $height
96
     *
97
     * @return $this
98
     *
99
     * @throws InvalidManipulation
100
     */
101 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...
102
    {
103
        if (! $this->validateManipulation($cropMethod, 'crop')) {
104
            throw InvalidManipulation::invalidParameter(
105
                'cropmethod',
106
                $cropMethod,
107
                $this->getValidManipulationOptions('crop')
108
            );
109
        }
110
111
        $this->width($width);
112
        $this->height($height);
113
114
        return $this->addManipulation('crop', $cropMethod);
115
    }
116
117
    /**
118
     * @param int $width
119
     * @param int $height
120
     * @param int $focalX Crop center X in percent
121
     * @param int $focalY Crop center Y in percent
122
     *
123
     * @return $this
124
     */
125
    public function focalCrop(int $width, int $height, int $focalX, int $focalY)
126
    {
127
        $this->width($width);
128
        $this->height($height);
129
130
        return $this->addManipulation('crop', "crop-{$focalX}-{$focalY}");
131
    }
132
133
    /**
134
     * @param int $width
135
     * @param int $height
136
     * @param int $x
137
     * @param int $y
138
     *
139
     * @return $this
140
     *
141
     * @throws InvalidManipulation
142
     */
143
    public function manualCrop(int $width, int $height, int $x, int $y)
144
    {
145
        if ($width < 0) {
146
            throw InvalidManipulation::invalidWidth($width);
147
        }
148
149
        if ($height < 0) {
150
            throw InvalidManipulation::invalidWidth($height);
151
        }
152
153
        return $this->addManipulation('manualCrop', "{$width},{$height},{$x},{$y}");
154
    }
155
156
    /**
157
     * @param int $width
158
     *
159
     * @return $this
160
     *
161
     * @throws InvalidManipulation
162
     */
163
    public function width(int $width)
164
    {
165
        if ($width < 0) {
166
            throw InvalidManipulation::invalidWidth($width);
167
        }
168
169
        return $this->addManipulation('width', $width);
170
    }
171
172
    /**
173
     * @param int $height
174
     *
175
     * @return $this
176
     *
177
     * @throws InvalidManipulation
178
     */
179
    public function height(int $height)
180
    {
181
        if ($height < 0) {
182
            throw InvalidManipulation::invalidWidth($height);
183
        }
184
185
        return $this->addManipulation('height', $height);
186
    }
187
188
    /**
189
     * @param string $fitMethod
190
     * @param int $width
191
     * @param int $height
192
     *
193
     * @return $this
194
     *
195
     * @throws InvalidManipulation
196
     */
197 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...
198
    {
199
        if (! $this->validateManipulation($fitMethod, 'fit')) {
200
            throw InvalidManipulation::invalidParameter(
201
                'fit',
202
                $fitMethod,
203
                $this->getValidManipulationOptions('fit')
204
            );
205
        }
206
207
        $this->width($width);
208
        $this->height($height);
209
210
        return $this->addManipulation('fit', $fitMethod);
211
    }
212
213
    /**
214
     * @param int $ratio A value between 1 and 8
215
     *
216
     * @return $this
217
     *
218
     * @throws InvalidManipulation
219
     */
220
    public function devicePixelRatio(int $ratio)
221
    {
222
        if ($ratio < 1 || $ratio > 8) {
223
            throw InvalidManipulation::valueNotInRange('ratio', $ratio, 1, 8);
224
        }
225
226
        return $this->addManipulation('devicePixelRatio', $ratio);
227
    }
228
229
    /**
230
     * @param int $brightness A value between -100 and 100
231
     *
232
     * @return $this
233
     *
234
     * @throws InvalidManipulation
235
     */
236
    public function brightness(int $brightness)
237
    {
238
        if ($brightness < -100 || $brightness > 100) {
239
            throw InvalidManipulation::valueNotInRange('brightness', $brightness, -100, 100);
240
        }
241
242
        return $this->addManipulation('brightness', $brightness);
243
    }
244
245
    /**
246
     * @param float $gamma A value between 0.01 and 9.99
247
     *
248
     * @return $this
249
     *
250
     * @throws InvalidManipulation
251
     */
252
    public function gamma(float $gamma)
253
    {
254
        if ($gamma < 0.01 || $gamma > 9.99) {
255
            throw InvalidManipulation::valueNotInRange('gamma', $gamma, 0.01, 9.00);
256
        }
257
258
        return $this->addManipulation('gamma', $gamma);
259
    }
260
261
    /**
262
     * @param int $contrast A value between -100 and 100
263
     *
264
     * @return $this
265
     *
266
     * @throws InvalidManipulation
267
     */
268
    public function contrast(int $contrast)
269
    {
270
        if ($contrast < -100 || $contrast > 100) {
271
            throw InvalidManipulation::valueNotInRange('contrast', $contrast, -100, 100);
272
        }
273
274
        return $this->addManipulation('contrast', $contrast);
275
    }
276
277
    /**
278
     * @param int $sharpen A value between 0 and 100
279
     *
280
     * @return $this
281
     *
282
     * @throws InvalidManipulation
283
     */
284
    public function sharpen(int $sharpen)
285
    {
286
        if ($sharpen < 0 || $sharpen > 100) {
287
            throw InvalidManipulation::valueNotInRange('sharpen', $sharpen, 0, 100);
288
        }
289
290
        return $this->addManipulation('sharpen', $sharpen);
291
    }
292
293
    /**
294
     * @param int $blur A value between 0 and 100
295
     *
296
     * @return $this
297
     *
298
     * @throws InvalidManipulation
299
     */
300 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...
301
    {
302
        if ($blur < 0 || $blur > 100) {
303
            throw InvalidManipulation::valueNotInRange('blur', $blur, 0, 100);
304
        }
305
306
        return $this->addManipulation('blur', $blur);
307
    }
308
309
    /**
310
     * @param int $pixelate A value between 0 and 1000
311
     *
312
     * @return $this
313
     *
314
     * @throws InvalidManipulation
315
     */
316
    public function pixelate(int $pixelate)
317
    {
318
        if ($pixelate < 0 || $pixelate > 1000) {
319
            throw InvalidManipulation::valueNotInRange('pixelate', $pixelate, 0, 1000);
320
        }
321
322
        return $this->addManipulation('pixelate', $pixelate);
323
    }
324
325
    /**
326
     * @return $this
327
     */
328
    public function greyscale()
329
    {
330
        return $this->filter('greyscale');
331
    }
332
333
    /**
334
     * @return $this
335
     */
336
    public function sepia()
337
    {
338
        return $this->filter('sepia');
339
    }
340
341
    /**
342
     * @param string $colorName
343
     *
344
     * @return $this
345
     */
346
    public function background(string $colorName)
347
    {
348
        return $this->addManipulation('background', $colorName);
349
    }
350
351
    /**
352
     * @param int $width
353
     * @param string $color
354
     * @param string $borderType
355
     *
356
     * @return $this
357
     *
358
     * @throws InvalidManipulation
359
     */
360
    public function border(int $width, string $color, string $borderType = 'overlay')
361
    {
362
        if ($width < 0) {
363
            throw InvalidManipulation::invalidWidth($width);
364
        }
365
366
        if (! $this->validateManipulation($borderType, 'border')) {
367
            throw InvalidManipulation::invalidParameter(
368
                'border',
369
                $borderType,
370
                $this->getValidManipulationOptions('border')
371
            );
372
        }
373
374
        return $this->addManipulation('border', "{$width},{$color},{$borderType}");
375
    }
376
377
    /**
378
     * @param int $quality
379
     *
380
     * @return $this
381
     *
382
     * @throws InvalidManipulation
383
     */
384 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...
385
    {
386
        if ($quality < 0 || $quality > 100) {
387
            throw InvalidManipulation::valueNotInRange('quality', $quality, 0, 100);
388
        }
389
390
        return $this->addManipulation('quality', $quality);
391
    }
392
393
    /**
394
     * @param string $format
395
     *
396
     * @return $this
397
     *
398
     * @throws InvalidManipulation
399
     */
400 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...
401
    {
402
        if (! $this->validateManipulation($format, 'format')) {
403
            throw InvalidManipulation::invalidParameter(
404
                'format',
405
                $format,
406
                $this->getValidManipulationOptions('format')
407
            );
408
        }
409
410
        return $this->addManipulation('format', $format);
411
    }
412
413
    /**
414
     * @param string $filterName
415
     *
416
     * @return $this
417
     *
418
     * @throws InvalidManipulation
419
     */
420 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...
421
    {
422
        if (! $this->validateManipulation($filterName, 'filter')) {
423
            throw InvalidManipulation::invalidParameter(
424
                'filter',
425
                $filterName,
426
                $this->getValidManipulationOptions('filter')
427
            );
428
        }
429
430
        return $this->addManipulation('filter', $filterName);
431
    }
432
433
    /**
434
     * @param string $filePath
435
     *
436
     * @return $this
437
     *
438
     * @throws FileNotFoundException
439
     */
440
    public function watermark(string $filePath)
441
    {
442
        if (! file_exists($filePath)) {
443
            throw new FileNotFoundException($filePath);
444
        }
445
446
        $this->addManipulation('watermark', $filePath);
447
448
        return $this;
449
    }
450
451
    /**
452
     * @param int    $width The width of the watermark in pixels (default) or percent.
453
     * @param string $unit  The unit of the `$width` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
454
     *
455
     * @return $this
456
     */
457
    public function watermarkWidth(int $width, string $unit = 'px')
458
    {
459
        $width = ($unit == static::UNIT_PERCENT ? $width.'w' : $width);
460
461
        return $this->addManipulation('watermarkWidth', $width);
462
    }
463
464
    /**
465
     * @param int    $height The height of the watermark in pixels (default) or percent.
466
     * @param string $unit   The unit of the `$height` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
467
     *
468
     * @return $this
469
     */
470
    public function watermarkHeight(int $height, string $unit = 'px')
471
    {
472
        $height = ($unit == static::UNIT_PERCENT ? $height.'h' : $height);
473
474
        return $this->addManipulation('watermarkHeight', $height);
475
    }
476
477
    /**
478
     * @param string $fitMethod How is the watermark fitted into the watermarkWidth and watermarkHeight properties.
479
     *
480
     * @return $this
481
     *
482
     * @throws InvalidManipulation
483
     */
484 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...
485
    {
486
        if (! $this->validateManipulation($fitMethod, 'fit')) {
487
            throw InvalidManipulation::invalidParameter(
488
                'watermarkFit',
489
                $fitMethod,
490
                $this->getValidManipulationOptions('fit')
491
            );
492
        }
493
494
        return $this->addManipulation('watermarkFit', $fitMethod);
495
    }
496
497
    /**
498
     * @param int $xPadding         How far is the watermark placed from the left and right edges of the image.
499
     * @param int|null $yPadding    How far is the watermark placed from the top and bottom edges of the image.
500
     * @param string $unit          Unit of the padding values. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`.
501
     *
502
     * @return $this
503
     */
504
    public function watermarkPadding(int $xPadding, int $yPadding = null, string $unit = 'px')
505
    {
506
        $yPadding = $yPadding ?? $xPadding;
507
508
        $xPadding = ($unit == static::UNIT_PERCENT ? $xPadding.'w' : $xPadding);
509
        $yPadding = ($unit == static::UNIT_PERCENT ? $yPadding.'h' : $yPadding);
510
511
        $this->addManipulation('watermarkPaddingX', $xPadding);
512
        $this->addManipulation('watermarkPaddingY', $yPadding);
513
514
        return $this;
515
    }
516
517
    /**
518
     * @param string $position
519
     *
520
     * @return $this
521
     *
522
     * @throws InvalidManipulation
523
     */
524 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...
525
    {
526
        if (! $this->validateManipulation($position, 'position')) {
527
            throw InvalidManipulation::invalidParameter(
528
                'watermarkPosition',
529
                $position,
530
                $this->getValidManipulationOptions('position')
531
            );
532
        }
533
534
        return $this->addManipulation('watermarkPosition', $position);
535
    }
536
537
    /**
538
     * Sets the opacity of the watermark. Only works with the `imagick` driver.
539
     *
540
     * @param int $opacity A value between 0 and 100.
541
     *
542
     * @return $this
543
     *
544
     * @throws InvalidManipulation
545
     */
546
    public function watermarkOpacity(int $opacity)
547
    {
548
        if ($opacity < 0 || $opacity > 100) {
549
            throw InvalidManipulation::valueNotInRange('opacity', $opacity, 0, 100);
550
        }
551
552
        return $this->addManipulation('watermarkOpacity', $opacity);
553
    }
554
555
    /**
556
     * @return $this
557
     */
558
    public function apply()
559
    {
560
        $this->manipulationSequence->startNewGroup();
561
562
        return $this;
563
    }
564
565
    /**
566
     * Create new manipulations class
567
     *
568
     * @return self
569
     */
570
    public static function create()
571
    {
572
        return new self();
573
    }
574
575
    /**
576
     * Return manipulationSequence as array
577
     *
578
     * @return array
579
     */
580
    public function toArray()
581
    {
582
        return $this->manipulationSequence->toArray();
583
    }
584
585
    /**
586
     * Checks if the given manipulations has arrays inside or not
587
     *
588
     * @param  Array $manipulations
589
     * @return bool
590
     */
591
    private function checkIfHasMultipleConversions($manipulations)
592
    {
593
        $hasArrays = false;
594
        foreach ($manipulations as $manipulation) {
595
            if (isset($manipulation[0]) && is_array($manipulation[0])) {
596
                $hasArrays = true;
597
            }
598
        }
599
        return $hasArrays;
600
    }
601
602
    public function removeManipulation(string $name)
603
    {
604
        $this->manipulationSequence->removeManipulation($name);
605
    }
606
607
    public function hasManipulation(string $manipulationName): bool
608
    {
609
        return ! is_null($this->getManipulationArgument($manipulationName));
610
    }
611
612
    /**
613
     * @param string $manipulationName
614
     *
615
     * @return string|null
616
     */
617
    public function getManipulationArgument(string $manipulationName)
618
    {
619
        foreach ($this->manipulationSequence->getGroups() as $manipulationSet) {
620
            if (array_key_exists($manipulationName, $manipulationSet)) {
621
                return $manipulationSet[$manipulationName];
622
            }
623
        }
624
    }
625
626
    protected function addManipulation(string $manipulationName, string $manipulationArgument)
627
    {
628
        $this->manipulationSequence->addManipulation($manipulationName, $manipulationArgument);
629
630
        return $this;
631
    }
632
633
    public function mergeManipulations(Manipulations $manipulations)
634
    {
635
        $this->manipulationSequence->merge($manipulations->manipulationSequence);
636
637
        return $this;
638
    }
639
640
    public function getManipulationSequence(): ManipulationSequence
641
    {
642
        return $this->manipulationSequence;
643
    }
644
645
    protected function validateManipulation(string $value, string $constantNamePrefix): bool
646
    {
647
        return in_array($value, $this->getValidManipulationOptions($constantNamePrefix));
648
    }
649
650
    protected function getValidManipulationOptions(string $manipulation): array
651
    {
652
        $options = (new ReflectionClass(static::class))->getConstants();
653
654
        return array_filter($options, function ($value, $name) use ($manipulation) {
655
            return strpos($name, strtoupper($manipulation)) === 0;
656
        }, ARRAY_FILTER_USE_BOTH);
657
    }
658
}
659