|
1
|
|
|
<?php |
|
2
|
|
|
namespace Staticus\Resources\Middlewares\Image; |
|
3
|
|
|
|
|
4
|
|
|
use League\Flysystem\FilesystemInterface; |
|
5
|
|
|
use Staticus\Diactoros\Response\ResourceDoResponse; |
|
6
|
|
|
use Staticus\Middlewares\MiddlewareAbstract; |
|
7
|
|
|
use Staticus\Resources\Exceptions\SaveResourceErrorException; |
|
8
|
|
|
use Staticus\Resources\Image\ResourceImageDOInterface; |
|
9
|
|
|
use Staticus\Resources\ResourceDOInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Zend\Diactoros\Response\EmptyResponse; |
|
12
|
|
|
use Zend\Stratigility\Http\Response; |
|
13
|
|
|
|
|
14
|
|
|
abstract class ImagePostProcessingMiddlewareAbstract extends MiddlewareAbstract |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ResourceImageDOInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $resourceDO; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var FilesystemInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $filesystem; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(ResourceDOInterface $resourceDO, FilesystemInterface $filesystem) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->resourceDO = $resourceDO; |
|
|
|
|
|
|
28
|
|
|
$this->filesystem = $filesystem; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param ResponseInterface $response |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function isSupportedResponse(ResponseInterface $response) |
|
36
|
|
|
{ |
|
37
|
|
|
return $response instanceof EmptyResponse |
|
38
|
|
|
|| $response instanceof ResourceDoResponse |
|
39
|
|
|
|| $response instanceof Response; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return \Staticus\Resources\Image\ResourceImageDOInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function getResourceWithoutSizes() |
|
46
|
|
|
{ |
|
47
|
|
|
$modelResourceDO = clone $this->resourceDO; |
|
48
|
|
|
$modelResourceDO->setWidth(); |
|
49
|
|
|
$modelResourceDO->setHeight(); |
|
50
|
|
|
|
|
51
|
|
|
return $modelResourceDO; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $directory |
|
56
|
|
|
* @throws SaveResourceErrorException |
|
57
|
|
|
* @see \Staticus\Resources\Middlewares\SaveResourceMiddlewareAbstract::createDirectory |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function createDirectory($directory) |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$this->filesystem->createDir($directory)) { |
|
62
|
|
|
throw new SaveResourceErrorException('Can\'t create a directory: ' . $directory); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function getImagick($sourcePath) |
|
67
|
|
|
{ |
|
68
|
|
|
if (!class_exists(\Imagick::class)) { |
|
69
|
|
|
throw new SaveResourceErrorException('Imagick is not installed'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return new \Imagick(realpath($sourcePath)); |
|
73
|
|
|
} |
|
74
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.