Completed
Push — master ( 5a3ce5...9ac351 )
by David
09:33 queued 07:56
created

WatermarkFilterLoader::load()   D

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
     * @param ImageInterface $image
41
     * @param array          $options
42
     *
43
     * @return ImageInterface|static
44
     */
45
    public function load(ImageInterface $image, array $options = [])
46
    {
47
        $options += [
48
            'size' => null,
49
            'position' => 'center',
50
        ];
51
52
        if ('%' === mb_substr($options['size'], -1)) {
53
            $options['size'] = mb_substr($options['size'], 0, -1) / 100;
54
        }
55
56
        $watermark = $this->imagine->open($this->projectDir.'/'.$options['image']);
57
58
        $size = $image->getSize();
59
        $watermarkSize = $watermark->getSize();
60
61
        // If 'null': Downscale if needed
62
        if (!$options['size'] && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) {
63
            $options['size'] = 1.0;
64
        }
65
66
        if ($options['size']) {
67
            $factor = $options['size'] * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight());
68
69
            $watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor));
70
            $watermarkSize = $watermark->getSize();
71
        }
72
73
        if ('multiple' === $options['position']) {
74
            // we loop over the coordinates of the image to apply the watermark as much as possible
75
            $pasteX = 0;
76
            while ($pasteX < $size->getWidth()) {
77
                $pasteY = 0;
78
                while ($pasteY < $size->getHeight()) {
79
                    $image->paste($watermark, new Point($pasteX, $pasteY));
80
                    $pasteY += $watermarkSize->getHeight();
81
                }
82
                $pasteX += $watermarkSize->getWidth();
83
            }
84
85
            return $image;
86
        }
87
88
        switch ($options['position']) {
89
            case 'topleft':
90
                $x = 0;
91
                $y = 0;
92
                break;
93
            case 'top':
94
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
95
                $y = 0;
96
                break;
97
            case 'topright':
98
                $x = $size->getWidth() - $watermarkSize->getWidth();
99
                $y = 0;
100
                break;
101
            case 'left':
102
                $x = 0;
103
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
104
                break;
105 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...
106
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
107
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
108
                break;
109 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...
110
                $x = $size->getWidth() - $watermarkSize->getWidth();
111
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
112
                break;
113
            case 'bottomleft':
114
                $x = 0;
115
                $y = $size->getHeight() - $watermarkSize->getHeight();
116
                break;
117 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...
118
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
119
                $y = $size->getHeight() - $watermarkSize->getHeight();
120
                break;
121 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...
122
                $x = $size->getWidth() - $watermarkSize->getWidth();
123
                $y = $size->getHeight() - $watermarkSize->getHeight();
124
                break;
125
            default:
126
                throw new \InvalidArgumentException("Unexpected position '{$options['position']}'");
127
                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...
128
        }
129
130
        return $image->paste($watermark, new Point($x, $y));
131
    }
132
}
133