ImageStripMiddlewareAbstract   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 57
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __invoke() 0 17 3
A stripImage() 0 8 1
A mustBeStripped() 0 9 4
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 ImageStripMiddlewareAbstract extends ImagePostProcessingMiddlewareAbstract
11
{
12
    /**
13
     * @var ConfigInterface
14
     */
15
    public $config;
16
17
    public function __construct(
18
        ResourceDOInterface $resourceDO
19
        , FilesystemInterface $filesystem
20
        , ConfigInterface $config
21
    )
22
    {
23
        parent::__construct($resourceDO, $filesystem);
24
        $this->config = $config;
25
    }
26
27
    public function __invoke(
28
        ServerRequestInterface $request,
29
        ResponseInterface $response,
30
        callable $next = null
31
    )
32
    {
33
        parent::__invoke($request, $response, $next);
34
        if (!$this->isSupportedResponse($response)) {
35
36
            return $next($request, $response);
37
        }
38
        if ($this->mustBeStripped()) {
39
            $this->stripImage($this->resourceDO->getFilePath());
40
        }
41
42
        return $next($request, $response);
43
    }
44
45
    public function stripImage($sourcePath)
46
    {
47
        $imagick = $this->getImagick($sourcePath);
48
        $imagick->stripImage();
49
        $imagick->writeImage($sourcePath);
50
        $imagick->clear();
51
        $imagick->destroy();
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    protected function mustBeStripped()
58
    {
59
        return $this->config->get('staticus.images.exif.strip', false)
60
        && (
61
            $this->resourceDO->isNew() // For the POST method
62
            || $this->resourceDO->isRecreate() // For the POST method
63
        )
64
        && $this->filesystem->has($this->resourceDO->getFilePath());
65
    }
66
}
67