Test Failed
Branch develop (c705b4)
by Florian
03:13
created

Operations::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
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 $arguments Arguments
44
     */
45
    public function __call($name, $arguments)
46
    {
47
        throw UnsupportedOperationException::withName($name);
48
    }
49
50
    /**
51
     * Crops the image
52
     *
53
     * @link http://image.intervention.io/api/fit
54
     * @param array $arguments Arguments
55
     * @return void
56
     */
57
    public function fit(array $arguments): void
58
    {
59
        if (!isset($arguments['width'])) {
60
            throw new InvalidArgumentException('Missing width');
61
        }
62
63
        $preventUpscale = $arguments['preventUpscale'] ?? false;
64
        $height = $arguments['height'] ?? null;
65
66
        $this->image->fit(
67
            (int)$arguments['width'],
68
            (int)$height,
69
            static function ($constraint) use ($preventUpscale) {
70
                if ($preventUpscale) {
71
                    $constraint->upsize();
72
                }
73
            }
74
        );
75
    }
76
77
    /**
78
     * Crops the image
79
     *
80
     * @link http://image.intervention.io/api/crop
81
     * @param array $arguments Arguments
82
     * @return void
83
     */
84
    public function crop(array $arguments): void
85
    {
86
        if (!isset($arguments['height'], $arguments['width'])) {
87
            throw new InvalidArgumentException('Missing height or width');
88
        }
89
90
        $height = $arguments['height'] ? (int)$arguments['height'] : null;
91
        $width = $arguments['width'] ? (int)$arguments['width'] : null;
92
        $x = $arguments['x'] ? (int)$arguments['x'] : null;
93
        $y = $arguments['y'] ? (int)$arguments['y'] : null;
94
95
        $this->image->crop($width, $height, $x, $y);
96
    }
97
98
    /**
99
     * Flips the image horizontal
100
     *
101
     * @link http://image.intervention.io/api/flip
102
     * @return void
103
     */
104
    public function flipHorizontal(): void
105
    {
106
        $this->flip(['direction' => 'h']);
107
    }
108
109
    /**
110
     * Flips the image vertical
111
     *
112
     * @link http://image.intervention.io/api/flip
113
     * @return void
114
     */
115
    public function flipVertical(): void
116
    {
117
        $this->flip(['direction' => 'v']);
118
    }
119
120
    /**
121
     * Flips the image
122
     *
123
     * @link http://image.intervention.io/api/flip
124
     * @param array $arguments Arguments
125
     * @return void
126
     */
127
    public function flip(array $arguments): void
128
    {
129
        if (!isset($arguments['direction'])) {
130
            throw new InvalidArgumentException('Direction missing');
131
        }
132
133
        if ($arguments['direction'] !== 'v' && $arguments['direction'] !== 'h') {
134
            throw new InvalidArgumentException(
135
                'Invalid argument, you must provide h or v'
136
            );
137
        }
138
139
        $this->image->flip($arguments['direction']);
140
    }
141
142
    /**
143
     * Resizes the image
144
     *
145
     * @link http://image.intervention.io/api/resize
146
     * @param array $arguments Arguments
147
     * @return void
148
     */
149
    public function resize(array $arguments): void
150
    {
151
        if (!isset($arguments['height'], $arguments['width'])) {
152
            throw new InvalidArgumentException(
153
                'Missing height or width'
154
            );
155
        }
156
157
        $aspectRatio = $arguments['aspectRatio'] ?? true;
158
        $preventUpscale = $arguments['preventUpscale'] ?? false;
159
160
        $this->image->resize(
161
            $arguments['width'],
162
            $arguments['height'],
163
            static function ($constraint) use ($aspectRatio, $preventUpscale) {
164
                if ($aspectRatio) {
165
                    $constraint->aspectRatio();
166
                }
167
                if ($preventUpscale) {
168
                    $constraint->upsize();
169
                }
170
            }
171
        );
172
    }
173
174
    /**
175
     * @link http://image.intervention.io/api/widen
176
     * @param array $arguments Arguments
177
     * @return void
178
     */
179
    public function widen(array $arguments): void
180
    {
181
        if (!isset($arguments['width'])) {
182
            throw new InvalidArgumentException(
183
                'Missing width'
184
            );
185
        }
186
187
        $preventUpscale = $arguments['preventUpscale'] ?? false;
188
189
        $this->image->widen((int)$arguments['width'], function ($constraint) use ($preventUpscale) {
190
            if ($preventUpscale) {
191
                $constraint->upsize();
192
            }
193
        });
194
    }
195
196
    /**
197
     * @link http://image.intervention.io/api/heighten
198
     * @param array $arguments Arguments
199
     * @return void
200
     */
201
    public function heighten(array $arguments): void
202
    {
203
        if (!isset($arguments['height'])) {
204
            throw new InvalidArgumentException(
205
                'Missing height'
206
            );
207
        }
208
209
        $preventUpscale = $arguments['preventUpscale'] ?? false;
210
211
        $this->image->heighten((int)$arguments['height'], function ($constraint) use ($preventUpscale) {
212
            if ($preventUpscale) {
213
                $constraint->upsize();
214
            }
215
        });
216
    }
217
218
    /**
219
     * @link http://image.intervention.io/api/rotate
220
     * @param array $arguments Arguments
221
     * @return void
222
     */
223
    public function rotate(array $arguments): void
224
    {
225
        if (!isset($arguments['angle'])) {
226
            throw new InvalidArgumentException(
227
                'Missing angle'
228
            );
229
        }
230
231
        $this->image->rotate((int)$arguments['angle']);
232
    }
233
234
    /**
235
     * @link http://image.intervention.io/api/rotate
236
     * @param array $arguments Arguments
237
     * @return void
238
     */
239
    public function sharpen(array $arguments): void
240
    {
241
        if (!isset($arguments['amount'])) {
242
            throw new InvalidArgumentException(
243
                'Missing amount'
244
            );
245
        }
246
247
        $this->image->sharpen((int)$arguments['amount']);
248
    }
249
250
    /**
251
     * Allows the declaration of a callable that gets the image manager instance
252
     * and the arguments passed to it.
253
     *
254
     * @param array $arguments Arguments
255
     * @return void
256
     */
257
    public function callback(array $arguments): void
258
    {
259
        if (!isset($arguments['callback'])) {
260
            throw new InvalidArgumentException(
261
                'Missing callback argument'
262
            );
263
        }
264
265
        if (!is_callable($arguments['callback'])) {
266
            throw new InvalidArgumentException(
267
                'Provided value for callback is not a callable'
268
            );
269
        }
270
271
        $arguments['callable']($this->image, $arguments);
272
    }
273
}
274