Completed
Pull Request — master (#732)
by 12345
03:41
created

WatermarkFilterLoader::load()   C

Complexity

Conditions 15
Paths 80

Size

Total Lines 72
Code Lines 56

Duplication

Lines 16
Ratio 22.22 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 72
rs 5.4849
cc 15
eloc 56
nc 80
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
namespace Liip\ImagineBundle\Imagine\Filter\Loader;
4
5
use Imagine\Image\Box;
6
use Imagine\Image\Point;
7
use Imagine\Image\ImageInterface;
8
use Imagine\Image\ImagineInterface;
9
10
class WatermarkFilterLoader implements LoaderInterface
11
{
12
    /**
13
     * @var ImagineInterface
14
     */
15
    protected $imagine;
16
17
    /**
18
     * @var string
19
     */
20
    protected $rootPath;
21
22
    public function __construct(ImagineInterface $imagine, $rootPath)
23
    {
24
        $this->imagine = $imagine;
25
        $this->rootPath = $rootPath;
26
    }
27
28
    /**
29
     * @see Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load()
30
     */
31
    public function load(ImageInterface $image, array $options = array())
32
    {
33
        $options += array(
34
            'size' => null,
35
            'position' => 'center',
36
        );
37
38
        if (substr($options['size'], -1) == '%') {
39
            $options['size'] = substr($options['size'], 0, -1) / 100;
40
        }
41
42
        $watermark = $this->imagine->open($this->rootPath.'/'.$options['image']);
43
44
        $size = $image->getSize();
45
        $watermarkSize = $watermark->getSize();
46
47
        // If 'null': Downscale if needed
48
        if (!$options['size'] && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) {
49
            $options['size'] = 1.0;
50
        }
51
52
        if ($options['size']) {
53
            $factor = $options['size'] * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight());
54
55
            $watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor));
56
            $watermarkSize = $watermark->getSize();
57
        }
58
59
        switch ($options['position']) {
60
            case 'topleft':
61
                $x = 0;
62
                $y = 0;
63
                break;
64
            case 'top':
65
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
66
                $y = 0;
67
                break;
68
            case 'topright':
69
                $x = $size->getWidth() - $watermarkSize->getWidth();
70
                $y = 0;
71
                break;
72
            case 'left':
73
                $x = 0;
74
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
75
                break;
76 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...
77
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
78
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
79
                break;
80 View Code Duplication
            case 'right':
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...
81
                $x = $size->getWidth() - $watermarkSize->getWidth();
82
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
83
                break;
84
            case 'bottomleft':
85
                $x = 0;
86
                $y = $size->getHeight() - $watermarkSize->getHeight();
87
                break;
88 View Code Duplication
            case 'bottom':
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...
89
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
90
                $y = $size->getHeight() - $watermarkSize->getHeight();
91
                break;
92 View Code Duplication
            case 'bottomright':
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...
93
                $x = $size->getWidth() - $watermarkSize->getWidth();
94
                $y = $size->getHeight() - $watermarkSize->getHeight();
95
                break;
96
            default:
97
                throw new \InvalidArgumentException("Unexpected position '{$options['position']}'");
98
                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...
99
        }
100
101
        return $image->paste($watermark, new Point($x, $y));
102
    }
103
}
104