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

ImageCompressMiddlewareAbstract   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 76
Duplicated Lines 11.84 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 4
dl 9
loc 76
ccs 0
cts 45
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 21 3
C compress() 0 27 12
A isAllowed() 9 9 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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