Completed
Pull Request — symfony5 (#1255)
by Axel
01:35
created

WatermarkFilterLoader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 16.16 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 5
dl 16
loc 99
rs 10
c 0
b 0
f 0

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
/*
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
        $file = $this->projectDir . '/' . $options['image'];
57
58
        if (!file_exists($file) {
59
            @trigger_error(
60
                'The ' . $file . ' does not exists, change the path based on kernel.project_dir parameter',
61
                E_USER_DEPRECATED
62
            );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';'
Loading history...
63
            $file = $this->projectDir . '/app/' . $options['image'];
64
        }
65
66
        $watermark = $this->imagine->open($file);
67
68
        $size = $image->getSize();
69
        $watermarkSize = $watermark->getSize();
70
71
        // If 'null': Downscale if needed
72
        if (!$options['size'] && ($size->getWidth() < $watermarkSize->getWidth() || $size->getHeight() < $watermarkSize->getHeight())) {
73
            $options['size'] = 1.0;
74
        }
75
76
        if ($options['size']) {
77
            $factor = $options['size'] * min($size->getWidth() / $watermarkSize->getWidth(), $size->getHeight() / $watermarkSize->getHeight());
78
79
            $watermark->resize(new Box($watermarkSize->getWidth() * $factor, $watermarkSize->getHeight() * $factor));
80
            $watermarkSize = $watermark->getSize();
81
        }
82
83
        switch ($options['position']) {
84
            case 'topleft':
85
                $x = 0;
86
                $y = 0;
87
                break;
88
            case 'top':
89
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
90
                $y = 0;
91
                break;
92
            case 'topright':
93
                $x = $size->getWidth() - $watermarkSize->getWidth();
94
                $y = 0;
95
                break;
96
            case 'left':
97
                $x = 0;
98
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
99
                break;
100
            case 'center':
101
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
102
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
103
                break;
104
            case 'right':
105
                $x = $size->getWidth() - $watermarkSize->getWidth();
106
                $y = ($size->getHeight() - $watermarkSize->getHeight()) / 2;
107
                break;
108
            case 'bottomleft':
109
                $x = 0;
110
                $y = $size->getHeight() - $watermarkSize->getHeight();
111
                break;
112
            case 'bottom':
113
                $x = ($size->getWidth() - $watermarkSize->getWidth()) / 2;
114
                $y = $size->getHeight() - $watermarkSize->getHeight();
115
                break;
116
            case 'bottomright':
117
                $x = $size->getWidth() - $watermarkSize->getWidth();
118
                $y = $size->getHeight() - $watermarkSize->getHeight();
119
                break;
120
            default:
121
                throw new \InvalidArgumentException("Unexpected position '{$options['position']}'");
122
                break;
123
        }
124
125
        return $image->paste($watermark, new Point($x, $y));
126
    }
127
}
128