WatermarkFilterLoader::load()   D
last analyzed

Complexity

Conditions 18
Paths 104

Size

Total Lines 87

Duplication

Lines 16
Ratio 18.39 %

Importance

Changes 0
Metric Value
dl 16
loc 87
rs 4.8333
c 0
b 0
f 0
cc 18
nc 104
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 WatermarkFilterLoader implements LoaderInterface
20
{
21
    /**
22
     * @var ImagineInterface
23
     */
24
    protected $imagine;
25
26
    /**
27
     * @var string
28
     */
29
    protected $projectDir;
30
31
    public function __construct(ImagineInterface $imagine, $projectDir)
32
    {
33
        $this->imagine = $imagine;
34
        $this->projectDir = $projectDir;
35
    }
36
37
    /**
38
     * @see \Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface::load()
39
     *
40
     * @return ImageInterface|static
41
     */
42
    public function load(ImageInterface $image, array $options = [])
43
    {
44
        $options += [
45
            'size' => null,
46
            'position' => 'center',
47
        ];
48
49
        if ('%' === mb_substr($options['size'], -1)) {
50
            $options['size'] = mb_substr($options['size'], 0, -1) / 100;
51
        }
52
53
        $watermark = $this->imagine->open($this->projectDir.'/'.$options['image']);
54
55
        $size = $image->getSize();
56
        $watermarkSize = $watermark->getSize();
57
58
        // If 'null': Downscale if needed
59
        if (!$options['size'] && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) {
60
            $options['size'] = 1.0;
61
        }
62
63
        if ($options['size']) {
64
            $factor = $options['size'] * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight());
65
66
            $watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor));
67
            $watermarkSize = $watermark->getSize();
68
        }
69
70
        if ('multiple' === $options['position']) {
71
            // we loop over the coordinates of the image to apply the watermark as much as possible
72
            $pasteX = 0;
73
            while ($pasteX < $size->getWidth()) {
74
                $pasteY = 0;
75
                while ($pasteY < $size->getHeight()) {
76
                    $image->paste($watermark, new Point($pasteX, $pasteY));
77
                    $pasteY += $watermarkSize->getHeight();
78
                }
79
                $pasteX += $watermarkSize->getWidth();
80
            }
81
82
            return $image;
83
        }
84
85
        switch ($options['position']) {
86
            case 'topleft':
87
                $x = 0;
88
                $y = 0;
89
                break;
90
            case 'top':
91
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
92
                $y = 0;
93
                break;
94
            case 'topright':
95
                $x = $size->getWidth() - $watermarkSize->getWidth();
96
                $y = 0;
97
                break;
98
            case 'left':
99
                $x = 0;
100
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
101
                break;
102 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...
103
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
104
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
105
                break;
106 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...
107
                $x = $size->getWidth() - $watermarkSize->getWidth();
108
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
109
                break;
110
            case 'bottomleft':
111
                $x = 0;
112
                $y = $size->getHeight() - $watermarkSize->getHeight();
113
                break;
114 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...
115
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
116
                $y = $size->getHeight() - $watermarkSize->getHeight();
117
                break;
118 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...
119
                $x = $size->getWidth() - $watermarkSize->getWidth();
120
                $y = $size->getHeight() - $watermarkSize->getHeight();
121
                break;
122
            default:
123
                throw new \InvalidArgumentException("Unexpected position '{$options['position']}'");
124
                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...
125
        }
126
127
        return $image->paste($watermark, new Point($x, $y));
128
    }
129
}
130