5
5
* Copyright © Magento, Inc. All rights reserved.
6
6
* See COPYING.txt for license details.
7
7
*/
8
+
8
9
namespace Magento \MediaStorage \App ;
9
10
11
+ use Closure ;
12
+ use Exception ;
13
+ use LogicException ;
10
14
use Magento \Catalog \Model \View \Asset \PlaceholderFactory ;
15
+ use Magento \Framework \App ;
16
+ use Magento \Framework \App \Area ;
17
+ use Magento \Framework \App \Filesystem \DirectoryList ;
18
+ use Magento \Framework \App \ResponseInterface ;
11
19
use Magento \Framework \App \State ;
20
+ use Magento \Framework \AppInterface ;
12
21
use Magento \Framework \Filesystem ;
22
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
23
+ use Magento \MediaStorage \Model \File \Storage \Config ;
13
24
use Magento \MediaStorage \Model \File \Storage \ConfigFactory ;
14
25
use Magento \MediaStorage \Model \File \Storage \Response ;
15
- use Magento \Framework \App ;
16
- use Magento \Framework \App \Filesystem \DirectoryList ;
17
- use Magento \Framework \AppInterface ;
26
+ use Magento \MediaStorage \Model \File \Storage \Synchronization ;
18
27
use Magento \MediaStorage \Model \File \Storage \SynchronizationFactory ;
19
- use Magento \Framework \App \Area ;
20
- use Magento \MediaStorage \Model \File \Storage \Config ;
21
28
use Magento \MediaStorage \Service \ImageResize ;
22
29
23
30
/**
31
+ * Media Storage
32
+ *
24
33
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25
34
*/
26
35
class Media implements AppInterface
27
36
{
28
37
/**
29
38
* Authorization function
30
39
*
31
- * @var \ Closure
40
+ * @var Closure
32
41
*/
33
42
private $ isAllowed ;
34
43
@@ -59,7 +68,7 @@ class Media implements AppInterface
59
68
private $ response ;
60
69
61
70
/**
62
- * @var \Magento\Framework\Filesystem\Directory\ WriteInterface
71
+ * @var WriteInterface
63
72
*/
64
73
private $ directory ;
65
74
@@ -92,7 +101,7 @@ class Media implements AppInterface
92
101
* @param ConfigFactory $configFactory
93
102
* @param SynchronizationFactory $syncFactory
94
103
* @param Response $response
95
- * @param \ Closure $isAllowed
104
+ * @param Closure $isAllowed
96
105
* @param string $mediaDirectory
97
106
* @param string $configCacheFile
98
107
* @param string $relativeFileName
@@ -106,7 +115,7 @@ public function __construct(
106
115
ConfigFactory $ configFactory ,
107
116
SynchronizationFactory $ syncFactory ,
108
117
Response $ response ,
109
- \ Closure $ isAllowed ,
118
+ Closure $ isAllowed ,
110
119
$ mediaDirectory ,
111
120
$ configCacheFile ,
112
121
$ relativeFileName ,
@@ -120,6 +129,7 @@ public function __construct(
120
129
$ this ->directory = $ filesystem ->getDirectoryWrite (DirectoryList::PUB );
121
130
$ mediaDirectory = trim ($ mediaDirectory );
122
131
if (!empty ($ mediaDirectory )) {
132
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
123
133
$ this ->mediaDirectoryPath = str_replace ('\\' , '/ ' , realpath ($ mediaDirectory ));
124
134
}
125
135
$ this ->configCacheFile = $ configCacheFile ;
@@ -135,9 +145,9 @@ public function __construct(
135
145
* Run application
136
146
*
137
147
* @return Response
138
- * @throws \ LogicException
148
+ * @throws LogicException
139
149
*/
140
- public function launch ()
150
+ public function launch (): ResponseInterface
141
151
{
142
152
$ this ->appState ->setAreaCode (Area::AREA_GLOBAL );
143
153
@@ -150,12 +160,12 @@ public function launch()
150
160
$ allowedResources = $ config ->getAllowedResources ();
151
161
$ isAllowed = $ this ->isAllowed ;
152
162
if (!$ isAllowed ($ this ->relativeFileName , $ allowedResources )) {
153
- throw new \ LogicException ('The specified path is not allowed. ' );
163
+ throw new LogicException ('The path is not allowed: ' . $ this -> relativeFileName );
154
164
}
155
165
}
156
166
157
167
try {
158
- /** @var \Magento\MediaStorage\Model\File\Storage\ Synchronization $sync */
168
+ /** @var Synchronization $sync */
159
169
$ sync = $ this ->syncFactory ->create (['directory ' => $ this ->directory ]);
160
170
$ sync ->synchronize ($ this ->relativeFileName );
161
171
$ this ->imageResize ->resizeFromImageName ($ this ->getOriginalImage ($ this ->relativeFileName ));
@@ -164,14 +174,19 @@ public function launch()
164
174
} else {
165
175
$ this ->setPlaceholderImage ();
166
176
}
167
- } catch (\ Exception $ e ) {
177
+ } catch (Exception $ e ) {
168
178
$ this ->setPlaceholderImage ();
169
179
}
170
180
171
181
return $ this ->response ;
172
182
}
173
183
174
- private function setPlaceholderImage ()
184
+ /**
185
+ * Set Placeholder as a response
186
+ *
187
+ * @return void
188
+ */
189
+ private function setPlaceholderImage (): void
175
190
{
176
191
$ placeholder = $ this ->placeholderFactory ->create (['type ' => 'image ' ]);
177
192
$ this ->response ->setFilePath ($ placeholder ->getPath ());
@@ -189,9 +204,9 @@ private function getOriginalImage(string $resizedImagePath): string
189
204
}
190
205
191
206
/**
192
- * { @inheritdoc}
207
+ * @inheritdoc
193
208
*/
194
- public function catchException (App \Bootstrap $ bootstrap , \ Exception $ exception )
209
+ public function catchException (App \Bootstrap $ bootstrap , Exception $ exception )
195
210
{
196
211
$ this ->response ->setHttpResponseCode (404 );
197
212
if ($ bootstrap ->isDeveloperMode ()) {
0 commit comments