Completed
Pull Request — master (#732)
by 12345
03:41
created

ImagineController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 11
nc 1
nop 5
1
<?php
2
3
namespace Liip\ImagineBundle\Controller;
4
5
use Imagine\Exception\RuntimeException;
6
use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
7
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
8
use Liip\ImagineBundle\Imagine\Data\DataManager;
9
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
10
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
11
use Liip\ImagineBundle\Imagine\Cache\SignerInterface;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17
18
class ImagineController
19
{
20
    /**
21
     * @var DataManager
22
     */
23
    protected $dataManager;
24
25
    /**
26
     * @var FilterManager
27
     */
28
    protected $filterManager;
29
30
    /**
31
     * @var CacheManager
32
     */
33
    protected $cacheManager;
34
35
    /**
36
     * @var SignerInterface
37
     */
38
    protected $signer;
39
40
    /**
41
     * @var LoggerInterface
42
     */
43
    protected $logger;
44
45
    /**
46
     * @param DataManager     $dataManager
47
     * @param FilterManager   $filterManager
48
     * @param CacheManager    $cacheManager
49
     * @param SignerInterface $signer
50
     */
51
    public function __construct(
52
        DataManager $dataManager,
53
        FilterManager $filterManager,
54
        CacheManager $cacheManager,
55
        SignerInterface $signer,
56
        LoggerInterface $logger = null
57
    ) {
58
        $this->dataManager = $dataManager;
59
        $this->filterManager = $filterManager;
60
        $this->cacheManager = $cacheManager;
61
        $this->signer = $signer;
62
        $this->logger = $logger;
63
    }
64
65
    /**
66
     * This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
67
     *
68
     * @param Request $request
69
     * @param string  $path
70
     * @param string  $filter
71
     *
72
     * @throws \RuntimeException
73
     * @throws BadRequestHttpException
74
     *
75
     * @return RedirectResponse
76
     */
77
    public function filterAction(Request $request, $path, $filter)
78
    {
79
        // decoding special characters and whitespaces from path obtained from url
80
        $path = urldecode($path);
81
        $resolver = $request->get('resolver');
82
83
        try {
84
            if (!$this->cacheManager->isStored($path, $filter, $resolver)) {
85
                try {
86
                    $binary = $this->dataManager->find($filter, $path);
87
                } catch (NotLoadableException $e) {
88
                    if ($defaultImageUrl = $this->dataManager->getDefaultImageUrl($filter)) {
89
                        return new RedirectResponse($defaultImageUrl);
90
                    }
91
92
                    throw new NotFoundHttpException('Source image could not be found', $e);
93
                }
94
95
                $this->cacheManager->store(
96
                    $this->filterManager->applyFilter($binary, $filter),
97
                    $path,
98
                    $filter,
99
                    $resolver
100
                );
101
            }
102
103
            return new RedirectResponse($this->cacheManager->resolve($path, $filter, $resolver), 301);
104
        } catch (NonExistingFilterException $e) {
105
            $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $path, $e->getMessage());
106
107
            if (null !== $this->logger) {
108
                $this->logger->debug($message);
109
            }
110
111
            throw new NotFoundHttpException($message, $e);
112
        } catch (RuntimeException $e) {
113
            throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $path, $filter, $e->getMessage()), 0, $e);
114
        }
115
    }
116
117
    /**
118
     * This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
119
     *
120
     * @param Request $request
121
     * @param string  $hash
122
     * @param string  $path
123
     * @param string  $filter
124
     *
125
     * @throws \RuntimeException
126
     * @throws BadRequestHttpException
127
     *
128
     * @return RedirectResponse
129
     */
130
    public function filterRuntimeAction(Request $request, $hash, $path, $filter)
131
    {
132
        $resolver = $request->get('resolver');
133
134
        try {
135
            $filters = $request->query->get('filters', array());
136
137
            if (!is_array($filters)) {
138
                throw new NotFoundHttpException(sprintf('Filters must be an array. Value was "%s"', $filters));
139
            }
140
141
            if (true !== $this->signer->check($hash, $path, $filters)) {
142
                throw new BadRequestHttpException(sprintf(
143
                    'Signed url does not pass the sign check for path "%s" and filter "%s" and runtime config %s',
144
                    $path,
145
                    $filter,
146
                    json_encode($filters)
147
                ));
148
            }
149
150
            try {
151
                $binary = $this->dataManager->find($filter, $path);
152
            } catch (NotLoadableException $e) {
153
                if ($defaultImageUrl = $this->dataManager->getDefaultImageUrl($filter)) {
154
                    return new RedirectResponse($defaultImageUrl);
155
                }
156
157
                throw new NotFoundHttpException(sprintf('Source image could not be found for path "%s" and filter "%s"', $path, $filter), $e);
158
            }
159
160
            $rcPath = $this->cacheManager->getRuntimePath($path, $filters);
161
162
            $this->cacheManager->store(
163
                $this->filterManager->applyFilter($binary, $filter, array(
164
                    'filters' => $filters,
165
                )),
166
                $rcPath,
167
                $filter,
168
                $resolver
169
            );
170
171
            return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter, $resolver), 301);
172
        } catch (NonExistingFilterException $e) {
173
            $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $hash.'/'.$path, $e->getMessage());
174
175
            if (null !== $this->logger) {
176
                $this->logger->debug($message);
177
            }
178
179
            throw new NotFoundHttpException($message, $e);
180
        } catch (RuntimeException $e) {
181
            throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $hash.'/'.$path, $filter, $e->getMessage()), 0, $e);
182
        }
183
    }
184
}
185