FlipFilterLoader::sanitizeOptions()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.7666
cc 4
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\Exception\InvalidArgumentException;
16
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
17
use Symfony\Component\OptionsResolver\Options;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
class FlipFilterLoader implements LoaderInterface
21
{
22
    /**
23
     * @return ImageInterface
24
     */
25
    public function load(ImageInterface $image, array $options = [])
26
    {
27
        $options = $this->sanitizeOptions($options);
28
29
        return 'x' === $options['axis'] ? $image->flipHorizontally() : $image->flipVertically();
30
    }
31
32
    /**
33
     * @return array
34
     */
35
    private function sanitizeOptions(array $options)
36
    {
37
        $resolver = new OptionsResolver();
38
        $resolver->setDefault('axis', 'x');
39
        $resolver->setAllowedValues('axis', ['x', 'horizontal', 'y', 'vertical']);
40
        $resolver->setNormalizer('axis', function (Options $options, $value) {
41
            return 'horizontal' === $value ? 'x' : ('vertical' === $value ? 'y' : $value);
42
        });
43
44
        try {
45
            return $resolver->resolve($options);
46
        } catch (ExceptionInterface $e) {
47
            throw new InvalidArgumentException('The "axis" option must be set to "x", "horizontal", "y", or "vertical".');
48
        }
49
    }
50
}
51