Completed
Pull Request — master (#1213)
by
unknown
01:34
created

ImagineController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 9
dl 0
loc 116
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A filterAction() 0 19 5
B filterRuntimeAction() 0 32 7
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\Controller;
13
14
use Imagine\Exception\RuntimeException;
15
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
16
use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
17
use Liip\ImagineBundle\Imagine\Cache\Helper\PathHelper;
18
use Liip\ImagineBundle\Imagine\Cache\SignerInterface;
19
use Liip\ImagineBundle\Imagine\Data\DataManager;
20
use Liip\ImagineBundle\Service\FilterService;
21
use Symfony\Component\HttpFoundation\RedirectResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
24
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25
26
class ImagineController
27
{
28
    /**
29
     * @var FilterService
30
     */
31
    private $filterService;
32
33
    /**
34
     * @var DataManager
35
     */
36
    private $dataManager;
37
38
    /**
39
     * @var SignerInterface
40
     */
41
    private $signer;
42
43
    /**
44
     * @param FilterService   $filterService
45
     * @param DataManager     $dataManager
46
     * @param SignerInterface $signer
47
     */
48
    public function __construct(FilterService $filterService, DataManager $dataManager, SignerInterface $signer)
49
    {
50
        $this->filterService = $filterService;
51
        $this->dataManager = $dataManager;
52
        $this->signer = $signer;
53
    }
54
55
    /**
56
     * This action applies a given filter to a given image, saves the image and redirects the browser to the stored
57
     * image.
58
     *
59
     * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
60
     * filter and storing the image again.
61
     *
62
     * @param Request $request
63
     * @param string  $path
64
     * @param string  $filter
65
     *
66
     * @throws RuntimeException
67
     * @throws NotFoundHttpException
68
     *
69
     * @return RedirectResponse
70
     */
71
    public function filterAction(Request $request, $path, $filter)
72
    {
73
        $path = PathHelper::urlPathToFilePath($path);
74
        $resolver = $request->get('resolver');
75
76
        try {
77
            return new RedirectResponse($this->filterService->getUrlOfFilteredImage($path, $filter, $resolver), 301);
78
        } catch (NotLoadableException $e) {
79
            if (null !== $this->dataManager->getDefaultImageUrl($filter)) {
80
                return new RedirectResponse($this->dataManager->getDefaultImageUrl($filter));
81
            }
82
83
            throw new NotFoundHttpException(sprintf('Source image for path "%s" could not be found', $path));
84
        } catch (NonExistingFilterException $e) {
85
            throw new NotFoundHttpException(sprintf('Requested non-existing filter "%s"', $filter));
86
        } catch (RuntimeException $e) {
87
            throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $path, $filter, $e->getMessage()), 0, $e);
88
        }
89
    }
90
91
    /**
92
     * This action applies a given filter -merged with additional runtime filters- to a given image, saves the image and
93
     * redirects the browser to the stored image.
94
     *
95
     * The resulting image is cached so subsequent requests will redirect to the cached image instead applying the
96
     * filter and storing the image again.
97
     *
98
     * @param Request $request
99
     * @param string  $hash
100
     * @param string  $path
101
     * @param string  $filter
102
     *
103
     * @throws RuntimeException
104
     * @throws BadRequestHttpException
105
     * @throws NotFoundHttpException
106
     *
107
     * @return RedirectResponse
108
     */
109
    public function filterRuntimeAction(Request $request, $hash, $path, $filter)
110
    {
111
        $resolver = $request->get('resolver');
112
        $runtimeConfig = $request->query->get('filters', []);
113
114
        if (!\is_array($runtimeConfig)) {
115
            throw new NotFoundHttpException(sprintf('Filters must be an array. Value was "%s"', $runtimeConfig));
116
        }
117
118
        if (true !== $this->signer->check($hash, $path, $runtimeConfig)) {
119
            throw new BadRequestHttpException(sprintf(
120
                'Signed url does not pass the sign check for path "%s" and filter "%s" and runtime config %s',
121
                $path,
122
                $filter,
123
                json_encode($runtimeConfig)
124
            ));
125
        }
126
127
        try {
128
            return new RedirectResponse($this->filterService->getUrlOfFilteredImageWithRuntimeFilters($path, $filter, $runtimeConfig, $resolver), 301);
129
        } catch (NotLoadableException $e) {
130
            if (null !== $this->dataManager->getDefaultImageUrl($filter)) {
131
                return new RedirectResponse($this->dataManager->getDefaultImageUrl($filter));
132
            }
133
134
            throw new NotFoundHttpException(sprintf('Source image for path "%s" could not be found', $path));
135
        } catch (NonExistingFilterException $e) {
136
            throw new NotFoundHttpException(sprintf('Requested non-existing filter "%s"', $filter));
137
        } catch (RuntimeException $e) {
138
            throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $hash.'/'.$path, $filter, $e->getMessage()), 0, $e);
139
        }
140
    }
141
}
142