Completed
Push — master ( 55c0ed...187ca7 )
by Daniel
10:59
created

Image::getColorSpace()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 12

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 6.4222

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 8
cts 13
cp 0.6153
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 0
crap 6.4222
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\Exception\RuntimeException;
12
use GravityMedia\Magickly\Image\AbstractImage;
13
use GravityMedia\Magickly\Image\ColorProfile;
14
use GuzzleHttp\Stream\Utils as StreamUtils;
15
16
/**
17
 * The image class.
18
 *
19
 * @package GravityMedia\Magickly\Gmagick
20
 */
21
class Image extends AbstractImage
22
{
23
    /**
24
     * @var \Gmagick
25
     */
26
    private $gmagick;
27
28
    /**
29
     * @var array
30
     */
31
    protected static $colorSpaceMapping = [
32
        ColorSpace::COLOR_SPACE_RGB => \Gmagick::COLORSPACE_RGB,
33
        ColorSpace::COLOR_SPACE_CMYK => \Gmagick::COLORSPACE_CMYK,
34
        ColorSpace::COLOR_SPACE_GRAYSCALE => \Gmagick::COLORSPACE_GRAY
35
    ];
36
37
    /**
38
     * Create image object.
39
     *
40
     * @param \Gmagick $gmagick
41
     */
42 30
    public function __construct(\Gmagick $gmagick)
43
    {
44 30
        $this->gmagick = $gmagick;
45 30
    }
46
47
    /**
48
     * Clone image object.
49
     */
50 2
    public function __clone()
51
    {
52 2
        $this->gmagick = clone $this->gmagick;
53 2
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 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...
59
    {
60 30
        $this->gmagick->getimagetype(); \Gmagick::IMGTYPE_GRAYSCALE;
61
62 30
        switch ($this->gmagick->getimagecolorspace()) {
63 30
            case \Gmagick::COLORSPACE_RGB:
64 20
            case \Gmagick::COLORSPACE_SRGB:
65 22
                return ColorSpace::COLOR_SPACE_RGB;
66 10
            case \Gmagick::COLORSPACE_CMYK:
67 10
                return ColorSpace::COLOR_SPACE_CMYK;
68
            case \Gmagick::COLORSPACE_GRAY:
69
                return ColorSpace::COLOR_SPACE_GRAYSCALE;
70
            default:
71
                throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 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...
79
    {
80 2
        if (!isset(static::$colorSpaceMapping[$colorSpace])) {
81
            throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
82
        }
83
84 2
        $image = clone $this;
85 2
        $image->gmagick->setimagecolorspace(static::$colorSpaceMapping[$colorSpace]);
86
87 2
        return $image;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getColorProfile()
94
    {
95
        try {
96
            $data = $this->gmagick->getimageprofile('ICM');
97
        } 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...
98
            return null;
99
        }
100
101
        return new ColorProfile(StreamUtils::create($data));
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function withColorProfile(ColorProfile $colorProfile)
108
    {
109
        $image = clone $this;
110
        $image->gmagick->setimageprofile('ICM', $colorProfile->getData());
111
112
        return $image;
113
    }
114
}
115