Completed
Push — master ( 187ca7...b80fb6 )
by Daniel
11:30
created

Image::getType()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 21
Code Lines 18

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 10.9327

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 21
loc 21
ccs 15
cts 19
cp 0.7895
rs 6.6746
cc 10
eloc 18
nc 10
nop 0
crap 10.9327

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Enum\ColorSpace;
11
use GravityMedia\Magickly\Enum\Type;
12
use GravityMedia\Magickly\Exception\RuntimeException;
13
use GravityMedia\Magickly\Image\AbstractImage;
14
use GravityMedia\Magickly\Image\ColorProfile;
15
use GravityMedia\Magickly\Image\ImageInterface;
16
use GuzzleHttp\Stream\Utils as StreamUtils;
17
18
/**
19
 * The image class.
20
 *
21
 * @package GravityMedia\Magickly\Imagick
22
 */
23
class Image extends AbstractImage
24
{
25
    /**
26
     * @var \Imagick
27
     */
28
    private $imagick;
29
30
    /**
31
     * @var array
32
     */
33
    protected static $typeMapping = [
34
        Type::TYPE_BILEVEL => \Imagick::IMGTYPE_BILEVEL,
35
        Type::TYPE_GRAYSCALE => \Imagick::IMGTYPE_GRAYSCALE,
36
        Type::TYPE_PALETTE => \Imagick::IMGTYPE_PALETTE,
37
        Type::TYPE_TRUECOLOR =>  \Imagick::IMGTYPE_TRUECOLOR,
38
        Type::TYPE_COLORSEPARATION =>  \Imagick::IMGTYPE_COLORSEPARATION,
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    protected static $colorSpaceMapping = [
45
        ColorSpace::COLOR_SPACE_RGB => \Imagick::COLORSPACE_RGB,
46
        ColorSpace::COLOR_SPACE_CMYK => \Imagick::COLORSPACE_CMYK,
47
        ColorSpace::COLOR_SPACE_GRAYSCALE => \Imagick::COLORSPACE_GRAY,
48
    ];
49
50
    /**
51
     * Create image object.
52
     *
53
     * @param \Imagick $imagick
54
     */
55 30
    public function __construct(\Imagick $imagick)
56
    {
57 30
        $this->imagick = $imagick;
58 30
    }
59
60
    /**
61
     * Clone image object.
62
     */
63 2
    public function __clone()
64
    {
65 2
        $this->imagick = clone $this->imagick;
66 2
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 28 View Code Duplication
    public function getType()
0 ignored issues
show
Duplication introduced by
This method 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...
72
    {
73 28
        switch ($this->imagick->getImageType()) {
74 28
            case \Imagick::IMGTYPE_BILEVEL:
75
                return Type::TYPE_BILEVEL;
76 28
            case \Imagick::IMGTYPE_GRAYSCALE:
77 26
            case \Imagick::IMGTYPE_GRAYSCALEMATTE:
78 6
                return Type::TYPE_GRAYSCALE;
79 22
            case \Imagick::IMGTYPE_PALETTE:
80 20
            case \Imagick::IMGTYPE_PALETTEMATTE:
81 4
                return Type::TYPE_PALETTE;
82 18
            case \Imagick::IMGTYPE_TRUECOLOR:
83 14
            case \Imagick::IMGTYPE_TRUECOLORMATTE:
84 10
                return Type::TYPE_TRUECOLOR;
85 8
            case \Imagick::IMGTYPE_COLORSEPARATION:
86 4
            case \Imagick::IMGTYPE_COLORSEPARATIONMATTE:
87 8
                return Type::TYPE_COLORSEPARATION;
88
            default:
89
                throw new RuntimeException('Unsupported image type');
90
        }
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 View Code Duplication
    public function withType($type)
0 ignored issues
show
Duplication introduced by
This method 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...
97
    {
98
        if (!isset(static::$typeMapping[$type])) {
99
            throw new RuntimeException('Unsupported image type');
100
        }
101
102
        $image = clone $this;
103
        $image->imagick->setImageType(static::$typeMapping[$type]);
104
105
        return $image;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 30 View Code Duplication
    public function getColorSpace()
0 ignored issues
show
Duplication introduced by
This method 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...
112
    {
113 30
        switch ($this->imagick->getImageColorspace()) {
114 30
            case \Imagick::COLORSPACE_RGB:
115 30
            case \Imagick::COLORSPACE_SRGB:
116 12
                return ColorSpace::COLOR_SPACE_RGB;
117 20
            case \Imagick::COLORSPACE_CMYK:
118 10
                return ColorSpace::COLOR_SPACE_CMYK;
119 10
            case \Imagick::COLORSPACE_GRAY:
120 10
                return ColorSpace::COLOR_SPACE_GRAYSCALE;
121
            default:
122
                throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 2 View Code Duplication
    public function withColorSpace($colorSpace)
0 ignored issues
show
Duplication introduced by
This method 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...
130
    {
131 2
        if (!isset(static::$colorSpaceMapping[$colorSpace])) {
132
            throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
133
        }
134
135 2
        $image = clone $this;
136 2
        $image->imagick->setImageColorspace(static::$colorSpaceMapping[$colorSpace]);
137
138 2
        return $image;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getColorProfile()
145
    {
146
        if (!in_array('icc', $this->imagick->getImageProfiles('*', false))) {
147
            return null;
148
        }
149
150
        $data = $this->imagick->getImageProfile('icc');
151
152
        return new ColorProfile(StreamUtils::create($data));
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function withColorProfile(ColorProfile $colorProfile)
159
    {
160
        $image = clone $this;
161
        $image->imagick->setImageProfile('icc', $colorProfile->getData());
162
163
        return $image;
164
    }
165
}
166