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 |
||
| 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 | ); |
||
|
|
|||
| 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(); |
||
| 128 |