Completed
Push — 1.0 ( 07d7de...09dc0e )
by Cedric
10s
created

FlipFilterLoader::sanitizeOptions()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 2
nop 1
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