ImagineImageProcessor   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 68
Duplicated Lines 29.41 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 8
dl 20
loc 68
ccs 32
cts 35
cp 0.9143
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 12 4
A create() 0 12 2
A load() 0 7 1
A save() 0 4 1
A insert() 0 4 1
A getWidth() 10 10 2
A getHeight() 10 10 2

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
namespace CSSPrites\ImageProcessor;
4
5
use Imagine\Gd\Imagine as ImagineGd;
6
use Imagine\Gmagick\Imagine as ImagineGmagick;
7
use Imagine\Image\Box;
8
use Imagine\Image\Palette\RGB;
9
use Imagine\Image\Point;
10
use Imagine\Imagick\Imagine as ImagineImagick;
11
12
class ImagineImageProcessor extends AbstractImageProcessor implements ImageProcessorInterface
13
{
14 21
    public function setConfig($config)
15
    {
16 21
        if ($config['driver'] == 'gd') {
17 18
            $this->manager = new ImagineGd();
18 21
        } elseif ($config['driver'] == 'imagick') {
19
            $this->manager = new ImagineImagick();
20 3
        } elseif ($config['driver'] == 'gmagick') {
21
            $this->manager = new ImagineGmagick();
22
        } else {
23 3
            throw new \Exception('Unknown Imagine Driver "'.$config['driver'].'"');
24
        }
25 18
    }
26
27 24
    public function create($width, $height, $background = null)
28
    {
29 24
        $palette = new RGB();
30 24
        $color   = is_null($background) ? $palette->color('#fff', 0) : $palette->color($background, 100);
31
32 24
        $size  = new Box($width, $height);
33
34 24
        $newImage = new self();
35 24
        $newImage->setImage($this->manager->create($size, $color));
36
37 24
        return $newImage;
38
    }
39
40 48
    public function load($path)
41
    {
42 48
        $newImage = new self();
43 48
        $newImage->setImage($this->manager->open($path));
44
45 48
        return $newImage;
46
    }
47
48 18
    public function save($path)
49
    {
50 18
        return $this->image->save($path);
51
    }
52
53 21
    public function insert(ImageProcessorInterface $image, $x = 0, $y = 0)
54
    {
55 21
        return $this->image->paste($image->getImage(), new Point($x, $y));
56
    }
57
58 27 View Code Duplication
    public function getWidth()
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 27
        if (is_null($this->image)) {
61 3
            throw new \Exception('No image created / loaded');
62
        }
63
64 24
        $size = $this->image->getSize();
65
66 24
        return $size->getWidth();
67
    }
68
69 27 View Code Duplication
    public function getHeight()
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...
70
    {
71 27
        if (is_null($this->image)) {
72 3
            throw new \Exception('No image created / loaded');
73
        }
74
75 24
        $size = $this->image->getSize();
76
77 24
        return $size->getHeight();
78
    }
79
}
80