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 ( 1d2cc7...778cb5 )
by Freek
01:45
created

Manipulations::classHasConstantValue()   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 2
1
<?php
2
3
namespace Spatie\Image;
4
5
use ReflectionClass;
6
use Spatie\Image\Exceptions\InvalidManipulation;
7
8
class Manipulations
9
{
10
    const CROP_TOP_LEFT = 'crop-top-left';
11
    const CROP_TOP = 'crop-top';
12
    const CROP_TOP_RIGHT = 'crop-top-right';
13
    const CROP_LEFT = 'crop-left';
14
    const CROP_CENTER = 'crop-center';
15
    const CROP_RIGHT = 'crop-right';
16
    const CROP_BOTTOM_LEFT = 'crop-bottom-left';
17
    const CROP_BOTTOM = 'crop-bottom';
18
    const CROP_BOTTOM_RIGHT = 'crop-bottom-right';
19
20
    const ORIENTATION_AUTO = 'auto';
21
    const ORIENTATION_90 = 90;
22
    const ORIENTATION_180 = 180;
23
    const ORIENTATION_270 = 270;
24
25
    const FIT_CONTAIN = 'contain';
26
    const FIT_MAX = 'max';
27
    const FIT_FILL = 'fill';
28
    const FIT_STRETCH = 'stretch';
29
    const FIT_CROP = 'crop';
30
31
    const BORDER_OVERLAY = 'overlay';
32
    const BORDER_SHRINK = 'shrink';
33
    const BORDER_EXPAND = 'expand';
34
35
    const FORMAT_JPG = 'jpg';
36
    const FORMAT_PJPG = 'pjpg';
37
    const FORMAT_PNG = 'png';
38
    const FORMAT_GIF = 'gif';
39
40
    /** @var \Spatie\Image\ManipulationSequence */
41
    protected $manipulationSequence;
42
43
    public function __construct(array $manipulations = [])
44
    {
45
        $this->manipulationSequence = new ManipulationSequence($manipulations);
46
    }
47
48
    /**
49
     * @param string $orientation
50
     * @return static
51
     *
52
     * @throws \Spatie\Image\Exceptions\InvalidManipulation
53
     */
54
    public function orientation(string $orientation)
55
    {
56
        if (!$this->classHasConstantValue($orientation, 'orientation')) {
57
            throw InvalidManipulation::invalidOrientation(
58
                $orientation,
59
                $this->getConstantValues('orientation')
60
            );
61
        }
62
63
        return $this->addManipulation($orientation);
64
    }
65
66
    /**
67
     * @param string $cropMethod
68
     * @param int $width
69
     * @param int $height
70
     *
71
     * @return static
72
     */
73
    public function crop(string $cropMethod, int $width, int $height)
74
    {
75
        return $this
76
            ->addManipulation($cropMethod, 'crop')
77
            ->addManipulation($width, 'width')
78
            ->addManipulation($height, 'height');
79
    }
80
81
    /**
82
     * @param int $width
83
     * @param int $height
84
     * @param int $focalX Crop center X in percent
85
     * @param int $focalY Crop center Y in percent
86
     *
87
     * @return static
88
     */
89
    public function focalCrop(int $width, int $height, $focalX, $focalY)
90
    {
91
        return $this
92
            ->addManipulation("crop-{$focalX}-{$focalY}", 'crop')
93
            ->addManipulation($width, 'width')
94
            ->addManipulation($height, 'height');
95
    }
96
97
    /**
98
     * @param int $width
99
     * @param int $height
100
     * @param int $x
101
     * @param int $y
102
     *
103
     * @return static
104
     */
105
    public function manualCrop(int $width, int $height, int $x, int $y)
106
    {
107
        return $this->addManipulation("{$width},{$height},{$x},{$y}");
108
    }
109
110
    /**
111
     * @param int $width
112
     *
113
     * @return $this
114
     * @throws InvalidManipulation
115
     */
116
    public function width(int $width)
117
    {
118
        if ($width < 0) {
119
            throw InvalidManipulation::invalidWidth($width);
120
        }
121
122
        return $this->addManipulation($width);
123
    }
124
125
    /**
126
     * @param int $height
127
     *
128
     * @return static
129
     */
130
    public function height(int $height)
131
    {
132
        return $this->addManipulation($height);
133
    }
134
135
    /**
136
     * @param string $fitMethod
137
     * @param int $width
138
     * @param int $height
139
     *
140
     * @return static
141
     */
142
    public function fit(string $fitMethod, int $width, int $height)
143
    {
144
        return $this
145
            ->addManipulation($fitMethod, 'fit')
146
            ->addManipulation($width, 'width')
147
            ->addManipulation($height, 'height');
148
    }
149
150
    /**
151
     * @param int $ratio
152
     *
153
     * @return static
154
     */
155
    public function devicePixelRatio(int $ratio)
156
    {
157
        return $this->addManipulation($ratio);
158
    }
159
160
    /**
161
     * @param int $brightness
162
     *
163
     * @return static
164
     */
165
    public function brightness(int $brightness)
166
    {
167
        return $this->addManipulation($brightness);
168
    }
169
170
    /**
171
     * @param float $gamma
172
     *
173
     * @return static
174
     */
175
    public function gamma(float $gamma)
176
    {
177
        return $this->addManipulation($gamma);
178
    }
179
180
    /**
181
     * @param int $contrast
182
     *
183
     * @return static
184
     */
185
    public function contrast(int $contrast)
186
    {
187
        return $this->addManipulation($contrast);
188
    }
189
190
    /**
191
     * @param int $sharpen
192
     *
193
     * @return static
194
     */
195
    public function sharpen(int $sharpen)
196
    {
197
        return $this->addManipulation($sharpen);
198
    }
199
200
    /**
201
     * @param int $blur
202
     *
203
     * @return static
204
     */
205
    public function blur(int $blur)
206
    {
207
        return $this->addManipulation($blur);
208
    }
209
210
    /**
211
     * @param int $pixelate
212
     *
213
     * @return static
214
     */
215
    public function pixelate(int $pixelate)
216
    {
217
        return $this->addManipulation($pixelate);
218
    }
219
220
    /**
221
     * @return static
222
     */
223
    public function greyscale()
224
    {
225
        return $this->filter('greyscale');
226
    }
227
228
    /**
229
     * @return static
230
     */
231
    public function sepia()
232
    {
233
        return $this->filter('sepia');
234
    }
235
236
    /**
237
     * @param string $colorName
238
     *
239
     * @return static
240
     */
241
    public function background(string $colorName)
242
    {
243
        return $this->addManipulation($colorName);
244
    }
245
246
    /**
247
     * @param int $width
248
     * @param string $color
249
     * @param string $borderType
250
     *
251
     * @return static
252
     */
253
    public function border(int $width, string $color, string $borderType = 'overlay')
254
    {
255
        return $this->addManipulation("{$width},{$color},{$borderType}", 'border');
256
    }
257
258
    /**
259
     * @param int $quality
260
     *
261
     * @return static
262
     */
263
    public function quality(int $quality)
264
    {
265
        return $this->addManipulation($quality);
266
    }
267
268
    /**
269
     * @param string $format
270
     *
271
     * @return static
272
     */
273
    public function format(string $format)
274
    {
275
        return $this->addManipulation($format);
276
    }
277
278
    /**
279
     * @param string $filterName
280
     *
281
     * @return static
282
     */
283
    protected function filter(string $filterName)
284
    {
285
        return $this->addManipulation($filterName);
286
    }
287
288
    /**
289
     * @return static
290
     */
291
    public function apply()
292
    {
293
        $this->manipulationSequence->startNewGroup();
294
295
        return $this;
296
    }
297
298
    public function removeManipulation(string $name)
299
    {
300
        $this->manipulationSequence->removeManipulation($name);
301
    }
302
303
    public function hasManipulation(string $manipulationName): bool
304
    {
305
        return !is_null($this->getManipulationArgument($manipulationName));
306
    }
307
308
    /**
309
     * @param string $manipulationName
310
     *
311
     * @return string|null
312
     */
313
    public function getManipulationArgument(string $manipulationName)
314
    {
315
        foreach ($this->manipulationSequence->getGroups() as $manipulationSet) {
316
            if (array_key_exists($manipulationName, $manipulationSet)) {
317
                return $manipulationSet[$manipulationName];
318
            }
319
        }
320
    }
321
322
    protected function addManipulation(string $manipulationArgument, string $manipulationName = null)
323
    {
324
        $manipulationName = $manipulationName ?? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
325
326
        $this->manipulationSequence->addManipulation($manipulationName, $manipulationArgument);
327
328
        return $this;
329
    }
330
331
    public function mergeManipulations(Manipulations $manipulations)
332
    {
333
        $this->manipulationSequence->merge($manipulations->manipulationSequence);
334
335
        return $this;
336
    }
337
338
    public function getManipulationSequence(): ManipulationSequence
339
    {
340
        return $this->manipulationSequence;
341
    }
342
343
    protected function classHasConstantValue(string $value, string $constantNamePrefix): bool
344
    {
345
        return in_array($value, $this->getConstantValues($constantNamePrefix));
346
    }
347
348
    protected function getConstantValues(string $namePrefix): array
349
    {
350
        $allConstants = (new ReflectionClass(static::class))->getConstants();
351
352
        return array_filter($allConstants, function($constantValue, $constantName) use ($namePrefix) {
353
            return strpos($constantName, strtoupper($namePrefix)) === 0;
354
        }, ARRAY_FILTER_USE_BOTH);
355
    }
356
}
357