createDirectory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
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;
0 ignored issues
show
Documentation Bug introduced by
$resourceDO is of type object<Staticus\Resources\ResourceDOInterface>, but the property $resourceDO was declared to be of type object<Staticus\Resource...sourceImageDOInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

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.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
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
}