Completed
Push — master ( c2562a...15ecc0 )
by Lukas Kahwe
09:33 queued 06:51
created

ThumbnailFilterLoader::load()   F

Complexity

Conditions 16
Paths 256

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 3.7109
c 0
b 0
f 0
cc 16
eloc 23
nc 256
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
        $width = isset($options['size'][0]) ? $options['size'][0] : null;
29
        $height = isset($options['size'][1]) ? $options['size'][1] : null;
30
31
        $size = $image->getSize();
32
        $origWidth = $size->getWidth();
33
        $origHeight = $size->getHeight();
34
35
        if (null === $width || null === $height) {
36
            if (null === $height) {
37
                $height = (int) (($width / $origWidth) * $origHeight);
38
            } elseif (null === $width) {
39
                $width = (int) (($height / $origHeight) * $origWidth);
40
            }
41
        }
42
43
        if (($origWidth > $width || $origHeight > $height)
44
            || (!empty($options['allow_upscale']) && ($origWidth !== $width || $origHeight !== $height))
45
        ) {
46
            $filter = new Thumbnail(new Box($width, $height), $mode, $filter);
47
            $image = $filter->apply($image);
48
        }
49
50
        return $image;
51
    }
52
}
53