Completed
Pull Request — 1.0 (#920)
by Rob
10:30
created

FlipFilterLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 6 2
A sanitizeOptions() 0 15 4
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\ImageInterface;
15
use Liip\ImagineBundle\Utility\OptionsResolver\OptionsResolver;
16
use Liip\ImagineBundle\Exception\InvalidArgumentException;
17
use Symfony\Component\OptionsResolver\Options;
18
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
19
20
class FlipFilterLoader implements LoaderInterface
21
{
22
    /**
23
     * @param ImageInterface $image
24
     * @param array          $options
25
     *
26
     * @return ImageInterface
27
     */
28
    public function load(ImageInterface $image, array $options = array())
29
    {
30
        $options = $this->sanitizeOptions($options);
31
32
        return $options['axis'] === 'x' ? $image->flipHorizontally() : $image->flipVertically();
33
    }
34
35
    /**
36
     * @param array $options
37
     *
38
     * @return array
39
     */
40
    private function sanitizeOptions(array $options)
41
    {
42
        $resolver = new OptionsResolver();
0 ignored issues
show
Deprecated Code introduced by
The class Liip\ImagineBundle\Utili...esolver\OptionsResolver has been deprecated with message: Deprecated in v1.7.x and scheduled for removal in v2.0.x

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
43
        $resolver->setDefault('axis', 'x');
44
        $resolver->setAllowedValues('axis', array('x', 'horizontal', 'y', 'vertical'));
45
        $resolver->setNormalizer('axis', function (Options $options, $value) {
46
            return $value === 'horizontal' ? 'x' : ($value === 'vertical' ? 'y' : $value);
47
        });
48
49
        try {
50
            return $resolver->resolve($options);
51
        } catch (ExceptionInterface $e) {
52
            throw new InvalidArgumentException('The "axis" option must be set to "x", "horizontal", "y", or "vertical".');
53
        }
54
    }
55
}
56