Test Setup Failed
Push — master ( 6a2c76...786817 )
by Florian
02:09
created

Operations::heighten()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Storage\Processor\Image;
18
19
use Intervention\Image\Image;
20
use InvalidArgumentException;
21
use Phauthentic\Infrastructure\Storage\Processor\Image\Exception\UnsupportedOperationException;
22
23
/**
24
 * Operations
25
 */
26
class Operations
27
{
28
    /**
29
     * @var \Intervention\Image\Image
30
     */
31
    protected Image $image;
32
33
    /**
34
     * @param \Intervention\Image\Image $image Image
35
     */
36
    public function __construct(Image $image)
37
    {
38
        $this->image = $image;
39
    }
40
41
    /**
42
     * @param string $name Name
43
     * @param array<string, mixed> $arguments Arguments
44
     * @return mixed
45
     */
46
    public function __call(string $name, array $arguments): mixed
47
    {
48
        throw UnsupportedOperationException::withName($name);
49
    }
50
51
    /**
52
     * Crops the image
53
     *
54
     * @link http://image.intervention.io/api/fit
55
     * @param array<string, mixed> $arguments Arguments
56
     * @return void
57
     */
58
    public function fit(array $arguments): void
59
    {
60
        if (!isset($arguments['width'])) {
61
            throw new InvalidArgumentException('Missing width');
62
        }
63
64
        $preventUpscale = $arguments['preventUpscale'] ?? false;
65
        $height = $arguments['height'] ?? null;
66
67
        $this->image->fit(
68
            (int)$arguments['width'],
69
            (int)$height,
70
            static function ($constraint) use ($preventUpscale) {
71
                if ($preventUpscale) {
72
                    $constraint->upsize();
73
                }
74
            }
75
        );
76
    }
77
78
    /**
79
     * Crops the image
80
     *
81
     * @link http://image.intervention.io/api/crop
82
     * @param array<string, mixed> $arguments Arguments
83
     * @return void
84
     */
85
    public function crop(array $arguments): void
86
    {
87
        if (!isset($arguments['height'], $arguments['width'])) {
88
            throw new InvalidArgumentException('Missing width or height');
89
        }
90
91
        $arguments = array_merge(['x' => null, 'y' => null], $arguments);
92
        $height = $arguments['height'] ? (int)$arguments['height'] : null;
93
        $width = $arguments['width'] ? (int)$arguments['width'] : null;
94
        $x = $arguments['x'] ? (int)$arguments['x'] : null;
95
        $y = $arguments['y'] ? (int)$arguments['y'] : null;
96
97
        $this->image->crop($width, $height, $x, $y);
98
    }
99
100
    /**
101
     * Flips the image horizontal
102
     *
103
     * @link http://image.intervention.io/api/flip
104
     * @return void
105
     */
106
    public function flipHorizontal(): void
107
    {
108
        $this->flip(['direction' => 'h']);
109
    }
110
111
    /**
112
     * Flips the image vertical
113
     *
114
     * @link http://image.intervention.io/api/flip
115
     * @return void
116
     */
117
    public function flipVertical(): void
118
    {
119
        $this->flip(['direction' => 'v']);
120
    }
121
122
    /**
123
     * Flips the image
124
     *
125
     * @link http://image.intervention.io/api/flip
126
     * @param array<string, mixed> $arguments Arguments
127
     * @return void
128
     */
129
    public function flip(array $arguments): void
130
    {
131
        if (!isset($arguments['direction'])) {
132
            throw new InvalidArgumentException('Direction missing');
133
        }
134
135
        if ($arguments['direction'] !== 'v' && $arguments['direction'] !== 'h') {
136
            throw new InvalidArgumentException(
137
                'Invalid argument, you must provide h or v'
138
            );
139
        }
140
141
        $this->image->flip($arguments['direction']);
142
    }
143
144
    /**
145
     * Resizes the image
146
     *
147
     * @link http://image.intervention.io/api/resize
148
     * @param array<string, mixed> $arguments Arguments
149
     * @return void
150
     */
151
    public function resize(array $arguments): void
152
    {
153
        if (!isset($arguments['height'], $arguments['width'])) {
154
            throw new InvalidArgumentException(
155
                'Missing height or width'
156
            );
157
        }
158
159
        $aspectRatio = $arguments['aspectRatio'] ?? true;
160
        $preventUpscale = $arguments['preventUpscale'] ?? false;
161
162
        $this->image->resize(
163
            $arguments['width'],
164
            $arguments['height'],
165
            static function ($constraint) use ($aspectRatio, $preventUpscale) {
166
                if ($aspectRatio) {
167
                    $constraint->aspectRatio();
168
                }
169
                if ($preventUpscale) {
170
                    $constraint->upsize();
171
                }
172
            }
173
        );
174
    }
175
176
    /**
177
     * @link http://image.intervention.io/api/widen
178
     * @param array<string, mixed> $arguments Arguments
179
     * @return void
180
     */
181
    public function widen(array $arguments): void
182
    {
183
        if (!isset($arguments['width'])) {
184
            throw new InvalidArgumentException(
185
                'Missing width'
186
            );
187
        }
188
189
        $preventUpscale = $arguments['preventUpscale'] ?? false;
190
191
        $this->image->widen((int)$arguments['width'], function ($constraint) use ($preventUpscale) {
192
            if ($preventUpscale) {
193
                $constraint->upsize();
194
            }
195
        });
196
    }
197
198
    /**
199
     * @link http://image.intervention.io/api/heighten
200
     * @param array<string, mixed> $arguments Arguments
201
     * @return void
202
     */
203
    public function heighten(array $arguments): void
204
    {
205
        if (!isset($arguments['height'])) {
206
            throw new InvalidArgumentException(
207
                'Missing height'
208
            );
209
        }
210
211
        $preventUpscale = $arguments['preventUpscale'] ?? false;
212
213
        $this->image->heighten((int)$arguments['height'], function ($constraint) use ($preventUpscale) {
214
            if ($preventUpscale) {
215
                $constraint->upsize();
216
            }
217
        });
218
    }
219
220
    /**
221
     * @link http://image.intervention.io/api/rotate
222
     * @param array<string, mixed> $arguments Arguments
223
     * @return void
224
     */
225
    public function rotate(array $arguments): void
226
    {
227
        if (!isset($arguments['angle'])) {
228
            throw new InvalidArgumentException(
229
                'Missing angle'
230
            );
231
        }
232
233
        $this->image->rotate((int)$arguments['angle']);
234
    }
235
236
    /**
237
     * @link http://image.intervention.io/api/rotate
238
     * @param array<string, mixed> $arguments Arguments
239
     * @return void
240
     */
241
    public function sharpen(array $arguments): void
242
    {
243
        if (!isset($arguments['amount'])) {
244
            throw new InvalidArgumentException(
245
                'Missing amount'
246
            );
247
        }
248
249
        $this->image->sharpen((int)$arguments['amount']);
250
    }
251
252
    /**
253
     * Allows the declaration of a callable that gets the image manager instance
254
     * and the arguments passed to it.
255
     *
256
     * @param array<string, mixed> $arguments Arguments
257
     * @return void
258
     */
259
    public function callback(array $arguments): void
260
    {
261
        if (!isset($arguments['callback'])) {
262
            throw new InvalidArgumentException(
263
                'Missing callback argument'
264
            );
265
        }
266
267
        if (!is_callable($arguments['callback'])) {
268
            throw new InvalidArgumentException(
269
                'Provided value for callback is not a callable'
270
            );
271
        }
272
273
        $arguments['callable']($this->image, $arguments);
274
    }
275
}
276