Test Failed
Branch master (e9e3e6)
by compolom
03:05
created

Image::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 1
b 0
f 1
1
<?php declare(strict_types=1);
2
3
namespace Compolomus\Compomage;
4
5
use Exception;
6
use InvalidArgumentException;
7
8
/**
9
 * Class Image
10
 * @package Compolomus\Compomage
11
 * @method Image save(string $filename): bool
12
 * @method Image __toString(): string
13
 * @method Image getBase64(): string
14
 * @method Image resize(int $width, int $height): ImageInterface
15
 * @method Image resizeByHeight(int $height): ImageInterface
16
 * @method Image resizeByWidth(int $width): ImageInterface
17
 * @method Image resizeByPercent(int $percent): ImageInterface
18
 * @method Image resizeBy(string $mode, int $param): ImageInterface
19
 * @method Image resizeByTransparentBackground(int $width, int $height): ImageInterface
20
 * @method Image resizeByBlurBackground(int $width, int $height): ImageInterface
21
 * @method Image crop(int $width, int $height, int $x, int $y): ImageInterface
22
 * @method Image rotate(int $angle = 90): ImageInterface
23
 * @method Image flip(): ImageInterface
24
 * @method Image flop(): ImageInterface
25
 * @method Image brightness(int $level): ImageInterface
26
 * @method Image contrast(int $level): ImageInterface
27
 * @method Image negate(): ImageInterface
28
 * @method Image blur(): ImageInterface
29
 * @method Image grayscale(): ImageInterface
30
 * @method Image getImage()
31
 * @method Image getWidth(): int
32
 * @method Image getHeight(): int
33
 * @method Image watermark($watermark, string $position): ImageInterface
34
 * @method Image thumbnail(int $width, int $height): ImageInterface
35
 * @method Image evaluateImage(int $op, float $constant, int $channel = \Imagick::CHANNEL_DEFAULT) // bug
36
 */
37
38
class Image
39
{
40
    public const AUTO = 0;
41
42
    public const GD = 1;
43
44
    public const IMAGICK = 2;
45
46
    private $class = self::GD;
47
48
    private $object;
49
50
    /**
51
     * Image constructor.
52
     * @param string $filename
53
     * @param int $mode
54
     * @throws Exception
55
     */
56 10
    public function __construct(string $filename, $mode = self::AUTO)
57
    {
58 10
        $this->check($filename, $mode);
59
    }
60
61
    /**
62
     * @param string $filename
63
     * @param int $mode
64
     * @throws Exception
65
     */
66 10
    private function check(string $filename, $mode = self::AUTO): void
67
    {
68 10
        if ($mode === self::IMAGICK || ($mode === self::AUTO && extension_loaded('imagick') === true)) {
69 6
            $this->class = self::IMAGICK;
70 6
            $this->object = new Imagick2($filename);
71 6
            return;
72
        }
73 5
        $this->object = new GD($filename);
74
    }
75
76
    /**
77
     * @param string $method
78
     * @param $args
79
     * @return mixed
80
     * @throws InvalidArgumentException
81
     */
82 5
    public function __call(string $method, $args)
83
    {
84 5
        if (!method_exists($this->object, $method)) {
85 1
            throw new InvalidArgumentException('Undefined method ' . $method);
86
        }
87 4
        return $this->object->$method(...$args);
88
    }
89
}
90