Magickly::open()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2.032
1
<?php
2
/**
3
 * This file is part of the Magickly project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Magickly\Imagick;
9
10
use GravityMedia\Magickly\Exception\RuntimeException;
11
use GravityMedia\Magickly\MagicklyInterface;
12
13
/**
14
 * The Magickly class.
15
 *
16
 * @package GravityMedia\Magickly\Imagick
17
 */
18 View Code Duplication
class Magickly implements MagicklyInterface
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /**
21
     * @var string
22
     */
23
    private static $version;
24
25
    /**
26
     * Create Magickly object.
27
     *
28
     * @throws RuntimeException
29
     */
30 4
    public function __construct()
31
    {
32 4
        if (!class_exists('Imagick')) {
33
            throw new RuntimeException('Imagick not installed');
34
        }
35
36 4
        $version = $this->getVersion();
37 4
        if (version_compare('6.2.9', $version) > 0) {
38
            throw new RuntimeException(sprintf('ImageMagick version 6.2.9 or higher is required, %s provided', $version));
39
        }
40 4
    }
41
42
    /**
43
     * Get version.
44
     *
45
     * @return string
46
     */
47 4
    public function getVersion()
48
    {
49 4
        if (null === self::$version) {
50 2
            $imagick = new \Imagick();
51 2
            $version = $imagick->getVersion();
52
53 2
            list(self::$version) = sscanf($version['versionString'], 'ImageMagick %s %04d-%02d-%02d %s %s');
54 1
        }
55
56 4
        return self::$version;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function open($path)
63
    {
64
        try {
65 2
            $imagick = new \Imagick($path);
66 1
        } catch (\ImagickException $exception) {
67
            throw new RuntimeException(sprintf('Unable to open image %s', $path), 0, $exception);
68
        }
69
70 2
        return new Image($imagick);
71
    }
72
}
73