Passed
Branch master (267be1)
by Eugene
03:26
created

ImageCompressMiddlewareAbstract::compress()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 0
cts 22
cp 0
rs 5.1612
cc 12
eloc 18
nc 9
nop 5
crap 156

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Staticus\Resources\Middlewares\Image;
3
4
use League\Flysystem\FilesystemInterface;
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Staticus\Config\ConfigInterface;
8
use Staticus\Resources\ResourceDOInterface;
9
10
abstract class ImageCompressMiddlewareAbstract extends ImagePostProcessingMiddlewareAbstract
11
{
12
    /**
13
     * @var ConfigInterface
14
     */
15
    protected $config;
16
17
    public function __construct(ResourceDOInterface $resourceDO, FilesystemInterface $filesystem, ConfigInterface $config)
18
    {
19
        parent::__construct($resourceDO, $filesystem);
20
        $this->config = $config;
21
    }
22
23
    public function __invoke(
24
        ServerRequestInterface $request,
25
        ResponseInterface $response,
26
        callable $next = null
27
    )
28
    {
29
        parent::__invoke($request, $response, $next);
30
        if (!$this->isSupportedResponse($response)) {
31
32
            return $next($request, $response);
33
        }
34
        if ($this->isAllowed()) {
35
            $interlacing = $this->config->get('staticus.images.compress.interlace', false);
36
            $quality = $this->config->get('staticus.images.compress.quality', false);
37
            $maxWidth = $this->config->get('staticus.images.compress.maxWidth', false);
38
            $maxHeight = $this->config->get('staticus.images.compress.maxHeight', false);
39
            $this->compress($this->resourceDO->getFilePath(), $interlacing, $quality, $maxWidth, $maxHeight);
40
        }
41
42
        return $next($request, $response);
43
    }
44
45
    public function compress($sourcePath, $interlacing = null, $quality = null, $maxWidth = null, $maxHeight = null)
46
    {
47
        if (!$interlacing && !$quality && (!$maxWidth || !$maxHeight)) {
48
49
            return;
50
        }
51
        $imagick = $this->getImagick($sourcePath);
52
        if ($interlacing) {
53
            $imagick->setInterlaceScheme($interlacing);
54
        }
55
        if ($quality) {
56
            $imagick->setImageCompressionQuality($quality);
57
        }
58
        if (
59
            // if width and height is already set in resourceDO, this middleware MUST not call default resizing
60
            !$this->resourceDO->getDimension()
61
            && $maxWidth
62
            && $imagick->getImageWidth() > $maxWidth
63
            && $maxHeight
64
            && $imagick->getImageHeight() > $maxHeight
65
        ) {
66
            $imagick->adaptiveResizeImage($maxWidth, $maxHeight, true);
67
        }
68
        $imagick->writeImage($sourcePath);
69
        $imagick->clear();
70
        $imagick->destroy();
71
    }
72
73
    /**
74
     * @return bool
75
     */
76 View Code Duplication
    protected function isAllowed()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        return $this->config->get('staticus.images.compress.compress', false)
79
        && (
80
            $this->resourceDO->isNew() // For the POST method
81
            || $this->resourceDO->isRecreate() // For the POST method
82
        )
83
        && $this->filesystem->has($this->resourceDO->getFilePath());
84
    }
85
}
86