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 14
CRAP Score 11.8232

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 21
loc 21
ccs 14
cts 19
cp 0.7368
rs 6.6746
cc 10
eloc 18
nc 10
nop 0
crap 11.8232

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\Gmagick;
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 GuzzleHttp\Stream\Utils as StreamUtils;
16
17
/**
18
 * The image class.
19
 *
20
 * @package GravityMedia\Magickly\Gmagick
21
 */
22
class Image extends AbstractImage
23
{
24
    /**
25
     * @var \Gmagick
26
     */
27
    private $gmagick;
28
29
    /**
30
     * @var array
31
     */
32
    protected static $typeMapping = [
33
        Type::TYPE_BILEVEL => \Gmagick::IMGTYPE_BILEVEL,
34
        Type::TYPE_GRAYSCALE => \Gmagick::IMGTYPE_GRAYSCALE,
35
        Type::TYPE_PALETTE => \Gmagick::IMGTYPE_PALETTE,
36
        Type::TYPE_TRUECOLOR => \Gmagick::IMGTYPE_TRUECOLOR,
37
        Type::TYPE_COLORSEPARATION => \Gmagick::IMGTYPE_COLORSEPARATION,
38
    ];
39
40
    /**
41
     * @var array
42
     */
43
    protected static $colorSpaceMapping = [
44
        ColorSpace::COLOR_SPACE_RGB => \Gmagick::COLORSPACE_RGB,
45
        ColorSpace::COLOR_SPACE_CMYK => \Gmagick::COLORSPACE_CMYK,
46
        ColorSpace::COLOR_SPACE_GRAYSCALE => \Gmagick::COLORSPACE_GRAY,
47
    ];
48
49
    /**
50
     * Create image object.
51
     *
52
     * @param \Gmagick $gmagick
53
     */
54 30
    public function __construct(\Gmagick $gmagick)
55
    {
56 30
        $this->gmagick = $gmagick;
57 30
    }
58
59
    /**
60
     * Clone image object.
61
     */
62 2
    public function __clone()
63
    {
64 2
        $this->gmagick = clone $this->gmagick;
65 2
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 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...
71
    {
72 28
        switch ($this->gmagick->getimagetype()) {
73 28
            case \Gmagick::IMGTYPE_BILEVEL:
74
                return Type::TYPE_BILEVEL;
75 28
            case \Gmagick::IMGTYPE_GRAYSCALE:
76 24
            case \Gmagick::IMGTYPE_GRAYSCALEMATTE:
77 10
                return Type::TYPE_GRAYSCALE;
78 18
            case \Gmagick::IMGTYPE_PALETTE:
79 18
            case \Gmagick::IMGTYPE_PALETTEMATTE:
80
                return Type::TYPE_PALETTE;
81 18
            case \Gmagick::IMGTYPE_TRUECOLOR:
82 14
            case \Gmagick::IMGTYPE_TRUECOLORMATTE:
83 10
                return Type::TYPE_TRUECOLOR;
84 8
            case \Gmagick::IMGTYPE_COLORSEPARATION:
85 4
            case \Gmagick::IMGTYPE_COLORSEPARATIONMATTE:
86 8
                return Type::TYPE_COLORSEPARATION;
87
            default:
88
                throw new RuntimeException('Unsupported image type');
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 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...
96
    {
97
        if (!isset(static::$typeMapping[$type])) {
98
            throw new RuntimeException('Unsupported image type');
99
        }
100
101
        $image = clone $this;
102
        $image->gmagick->setimagetype(static::$typeMapping[$type]);
103
104
        return $image;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 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...
111
    {
112 30
        switch ($this->gmagick->getimagecolorspace()) {
113 30
            case \Gmagick::COLORSPACE_RGB:
114 20
            case \Gmagick::COLORSPACE_SRGB:
115 22
                return ColorSpace::COLOR_SPACE_RGB;
116 10
            case \Gmagick::COLORSPACE_CMYK:
117 10
                return ColorSpace::COLOR_SPACE_CMYK;
118
            case \Gmagick::COLORSPACE_GRAY:
119
                return ColorSpace::COLOR_SPACE_GRAYSCALE;
120
            default:
121
                throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
122
        }
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 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...
129
    {
130 2
        if (!isset(static::$colorSpaceMapping[$colorSpace])) {
131
            throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
132
        }
133
134 2
        $image = clone $this;
135 2
        $image->gmagick->setimagecolorspace(static::$colorSpaceMapping[$colorSpace]);
136
137 2
        return $image;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getColorProfile()
144
    {
145
        try {
146
            $data = $this->gmagick->getimageprofile('ICM');
147
        } catch (\GmagickException $exception) {
0 ignored issues
show
Bug introduced by
The class GmagickException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
148
            return null;
149
        }
150
151
        return new ColorProfile(StreamUtils::create($data));
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function withColorProfile(ColorProfile $colorProfile)
158
    {
159
        $image = clone $this;
160
        $image->gmagick->setimageprofile('ICM', $colorProfile->getData());
161
162
        return $image;
163
    }
164
}
165