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

Image   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 28.72 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 61.76%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 27
loc 94
ccs 21
cts 34
cp 0.6176
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 4 1
A __construct() 0 4 1
B getColorSpace() 16 16 5
A withColorSpace() 11 11 2
A getColorProfile() 0 10 2
A withColorProfile() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\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\Imagick
20
 */
21
class Image extends AbstractImage
22
{
23
    /**
24
     * @var \Imagick
25
     */
26
    private $imagick;
27
28
    /**
29
     * @var array
30
     */
31
    protected static $colorSpaceMapping = [
32
        ColorSpace::COLOR_SPACE_RGB => \Imagick::COLORSPACE_RGB,
33
        ColorSpace::COLOR_SPACE_CMYK => \Imagick::COLORSPACE_CMYK,
34
        ColorSpace::COLOR_SPACE_GRAYSCALE => \Imagick::COLORSPACE_GRAY
35
    ];
36
37
    /**
38
     * Create image object.
39
     *
40
     * @param \Imagick $imagick
41
     */
42 30
    public function __construct(\Imagick $imagick)
43
    {
44 30
        $this->imagick = $imagick;
45 30
    }
46
47
    /**
48
     * Clone image object.
49
     */
50 2
    public function __clone()
51
    {
52 2
        $this->imagick = clone $this->imagick;
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->imagick->getImageType(); \Imagick::IMGTYPE_GRAYSCALE;
61
62 30
        switch ($this->imagick->getImageColorspace()) {
63 30
            case \Imagick::COLORSPACE_RGB:
64 30
            case \Imagick::COLORSPACE_SRGB:
65 12
                return ColorSpace::COLOR_SPACE_RGB;
66 20
            case \Imagick::COLORSPACE_CMYK:
67 10
                return ColorSpace::COLOR_SPACE_CMYK;
68 10
            case \Imagick::COLORSPACE_GRAY:
69 10
                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->imagick->setImageColorspace(static::$colorSpaceMapping[$colorSpace]);
86
87 2
        return $image;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getColorProfile()
94
    {
95
        if (!in_array('icc', $this->imagick->getImageProfiles('*', false))) {
96
            return null;
97
        }
98
99
        $data = $this->imagick->getImageProfile('icc');
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->imagick->setImageProfile('icc', $colorProfile->getData());
111
112
        return $image;
113
    }
114
}
115