Completed
Push — 2.0 ( 647d51...715e56 )
by David
12s queued 10s
created

BackgroundFilterLoader::load()   D

Complexity

Conditions 18
Paths 90

Size

Total Lines 72

Duplication

Lines 4
Ratio 5.56 %

Importance

Changes 0
Metric Value
dl 4
loc 72
rs 4.8666
c 0
b 0
f 0
cc 18
nc 90
nop 2

How to fix   Long Method    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
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Imagine\Filter\Loader;
13
14
use Imagine\Image\Box;
15
use Imagine\Image\ImageInterface;
16
use Imagine\Image\ImagineInterface;
17
use Imagine\Image\Point;
18
19
class BackgroundFilterLoader implements LoaderInterface
20
{
21
    /**
22
     * @var ImagineInterface
23
     */
24
    protected $imagine;
25
26
    public function __construct(ImagineInterface $imagine)
27
    {
28
        $this->imagine = $imagine;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function load(ImageInterface $image, array $options = [])
35
    {
36
        $background = $image->palette()->color(
37
            isset($options['color']) ? $options['color'] : '#fff',
38
            isset($options['transparency']) ? $options['transparency'] : null
39
        );
40
        $topLeft = new Point(0, 0);
41
        $size = $image->getSize();
42
43
        if (isset($options['size'])) {
44
            $width = isset($options['size'][0]) ? $options['size'][0] : null;
45
            $height = isset($options['size'][1]) ? $options['size'][1] : null;
46
47
            $position = isset($options['position']) ? $options['position'] : 'center';
48
            switch ($position) {
49
                case 'topleft':
50
                    $x = 0;
51
                    $y = 0;
52
                    break;
53
                case 'top':
54
                    $x = ($width - $image->getSize()->getWidth()) / 2;
55
                    $y = 0;
56
                    break;
57
                case 'topright':
58
                    $x = $width - $image->getSize()->getWidth();
59
                    $y = 0;
60
                    break;
61
                case 'left':
62
                    $x = 0;
63
                    $y = ($height - $image->getSize()->getHeight()) / 2;
64
                    break;
65
                case 'centerright':
66
                    $x = $width - $image->getSize()->getWidth();
67
                    $y = ($height - $image->getSize()->getHeight()) / 2;
68
                    break;
69 View Code Duplication
                case 'center':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
                    $x = ($width - $image->getSize()->getWidth()) / 2;
71
                    $y = ($height - $image->getSize()->getHeight()) / 2;
72
                    break;
73
                case 'centerleft':
74
                    $x = 0;
75
                    $y = ($height - $image->getSize()->getHeight()) / 2;
76
                    break;
77
                case 'right':
78
                    $x = $width - $image->getSize()->getWidth();
79
                    $y = ($height - $image->getSize()->getHeight()) / 2;
80
                    break;
81
                case 'bottomleft':
82
                    $x = 0;
83
                    $y = $height - $image->getSize()->getHeight();
84
                    break;
85
                case 'bottom':
86
                    $x = ($width - $image->getSize()->getWidth()) / 2;
87
                    $y = $height - $image->getSize()->getHeight();
88
                    break;
89
                case 'bottomright':
90
                    $x = $width - $image->getSize()->getWidth();
91
                    $y = $height - $image->getSize()->getHeight();
92
                    break;
93
                default:
94
                    throw new \InvalidArgumentException("Unexpected position '{$position}'");
95
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
96
            }
97
98
            $size = new Box($width, $height);
99
            $topLeft = new Point($x, $y);
100
        }
101
102
        $canvas = $this->imagine->create($size, $background);
103
104
        return $canvas->paste($image, $topLeft);
105
    }
106
}
107