Completed
Push — 2.0 ( 057aa6...b272ae )
by Maksim
02:01
created

ImagineController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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