Completed
Pull Request — 1.0 (#941)
by Rob
02:56
created

ResolutionFilterLoader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 21 2
A getTemporaryFilePath() 0 11 2
A sanitizeOptions() 0 16 3
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 Imagine\Image\ImagineInterface;
16
use Liip\ImagineBundle\Utility\OptionsResolver\OptionsResolver;
17
use Liip\ImagineBundle\Exception\InvalidArgumentException;
18
use Symfony\Component\OptionsResolver\Options;
19
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
20
21
class ResolutionFilterLoader implements LoaderInterface
22
{
23
    /**
24
     * @var ImagineInterface
25
     */
26
    private $imagine;
27
28
    /**
29
     * @param ImagineInterface $imagine
30
     */
31
    public function __construct(ImagineInterface $imagine)
32
    {
33
        $this->imagine = $imagine;
34
    }
35
36
    /**
37
     * @param ImageInterface $image
38
     * @param array          $options
39
     *
40
     * @throws \Exception
41
     *
42
     * @return ImageInterface
43
     */
44
    public function load(ImageInterface $image, array $options = array())
45
    {
46
        $options = $this->sanitizeOptions($options);
47
        $temporaryFile = $this->getTemporaryFilePath();
48
49
        try {
50
            $image->save($temporaryFile, array(
51
                'resolution-units' => $options['units-per-pixel'],
52
                'resolution-x' => $options['x'],
53
                'resolution-y' => $options['y'],
54
            ));
55
56
            $image = $this->imagine->open($temporaryFile);
57
            unlink($temporaryFile);
58
59
            return $image;
60
        } catch (\Exception $exception) {
61
            unlink($temporaryFile);
62
            throw $exception;
63
        }
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    private function getTemporaryFilePath()
70
    {
71
        $path = sys_get_temp_dir();
72
        $path = tempnam($path, 'liip-imagine-bundle');
73
74
        if (false === $path) {
75
            throw new \RuntimeException(sprintf('Temporary file can not be created in "%s".', $path));
76
        }
77
78
        return $path;
79
    }
80
81
    /**
82
     * @param array $options
83
     *
84
     * @return array
85
     */
86
    private function sanitizeOptions(array $options)
87
    {
88
        $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...
89
        $resolver->setAllowedTypes('x', ['int']);
90
        $resolver->setAllowedTypes('y', ['int']);
91
        $resolver->setAllowedValues('units-per-pixel', ['inch', 'centimeter']);
92
        $resolver->setNormalizer('units-per-pixel', function (Options $options, $value) {
93
            return $value === 'inch' ? ImageInterface::RESOLUTION_PIXELSPERINCH : ImageInterface::RESOLUTION_PIXELSPERCENTIMETER;
94
        });
95
96
        try {
97
            return $resolver->resolve($options);
98
        } catch (ExceptionInterface $e) {
99
            throw new InvalidArgumentException('The "units" option must be set to "inch" or "centimeter" and you must set a "x" and "y" value.');
100
        }
101
    }
102
}
103