Completed
Push — 2.0 ( d9081a...4a1e3c )
by Rob
11:15
created

FlipFilterLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
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\Options;
17
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
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 = [])
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();
43
        $resolver->setDefault('axis', 'x');
44
        $resolver->setAllowedValues('axis', ['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