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

ThumbnailFilterLoader::load()   C

Complexity

Conditions 14
Paths 64

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
rs 5.0864
cc 14
eloc 22
nc 64
nop 2

How to fix   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\Filter\Basic\Thumbnail;
6
use Imagine\Image\Box;
7
use Imagine\Image\ImageInterface;
8
9
class ThumbnailFilterLoader implements LoaderInterface
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function load(ImageInterface $image, array $options = array())
15
    {
16
        $mode = ImageInterface::THUMBNAIL_OUTBOUND;
17
        if (!empty($options['mode']) && 'inset' === $options['mode']) {
18
            $mode = ImageInterface::THUMBNAIL_INSET;
19
        }
20
21
        if (!empty($options['filter'])) {
22
            $filter = constant('Imagine\Image\ImageInterface::FILTER_'.strtoupper($options['filter']));
23
        }
24
        if (empty($filter)) {
25
            $filter = ImageInterface::FILTER_UNDEFINED;
26
        }
27
28
        list($width, $height) = $options['size'];
29
30
        $size = $image->getSize();
31
        $origWidth = $size->getWidth();
32
        $origHeight = $size->getHeight();
33
34
        if (null === $width || null === $height) {
35
            if (null === $height) {
36
                $height = (int) (($width / $origWidth) * $origHeight);
37
            } elseif (null === $width) {
38
                $width = (int) (($height / $origHeight) * $origWidth);
39
            }
40
        }
41
42
        if (($origWidth > $width || $origHeight > $height)
43
            || (!empty($options['allow_upscale']) && ($origWidth !== $width || $origHeight !== $height))
44
        ) {
45
            $filter = new Thumbnail(new Box($width, $height), $mode, $filter);
46
            $image = $filter->apply($image);
47
        }
48
49
        return $image;
50
    }
51
}
52