ImageCropMiddlewareAbstract::__invoke()   C
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 0
cts 21
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 6
nop 3
crap 72
1
<?php
2
namespace Staticus\Resources\Middlewares\Image;
3
4
use Psr\Http\Message\ResponseInterface;
5
use Psr\Http\Message\ServerRequestInterface;
6
use Staticus\Resources\Image\CropImageDOInterface;
7
use Zend\Expressive\Container\Exception\NotFoundException;
8
9
abstract class ImageCropMiddlewareAbstract extends ImagePostProcessingMiddlewareAbstract
10
{
11
    public function __invoke(
12
        ServerRequestInterface $request,
13
        ResponseInterface $response,
14
        callable $next = null
15
    )
16
    {
17
        parent::__invoke($request, $response, $next);
18
        if (!$this->isSupportedResponse($response)) {
19
20
            return $next($request, $response);
21
        }
22
        $crop = $this->resourceDO->getCrop();
23
        if ($this->resourceDO->getDimension() && $crop) {
24
            $path = $this->resourceDO->getFilePath();
25
26
            // (POST) Resource just created or re-created
27
            if ($this->resourceDO->isNew()
28
                || $this->resourceDO->isRecreate()
29
            ) {
30
                /**
31
                 * Explanation: If it's just an artifact that is left from the previous file after re-creation
32
                 * than you need to remove it exact in recreation moment
33
                 * @see \Staticus\Resources\Middlewares\Image\SaveImageMiddlewareAbstract::afterSave
34
                 */
35
                // Some of previous middlewares already created this file size
36
                if ($this->filesystem->has($path)) {
37
                    $this->cropImage($path, $path, $crop);
38
                } else {
39
                    $modelResourceDO = $this->getResourceWithoutSizes();
40
                    $this->cropImage($modelResourceDO->getFilePath(), $path, $crop);
41
                }
42
43
            // (GET) Resource should be exist, just check if this size wasn't created before
44
            } else if (!$this->filesystem->has($path)) {
45
                $modelResourceDO = $this->getResourceWithoutSizes();
46
                $this->cropImage($modelResourceDO->getFilePath(), $path, $crop);
47
            }
48
        }
49
50
        return $next($request, $response);
51
    }
52
53
    public function cropImage($sourcePath, $destinationPath, CropImageDOInterface $crop)
54
    {
55
        if (!$this->filesystem->has($sourcePath)) {
56
            throw new NotFoundException('Can not crop. Resource is not found');
57
        }
58
        $this->createDirectory(dirname($destinationPath));
59
        $imagick = $this->getImagick($sourcePath);
60
        $imagick->cropImage(
61
            $crop->getWidth(),
62
            $crop->getHeight(),
63
            $crop->getX(),
64
            $crop->getY()
65
        );
66
        $imagick->writeImage($destinationPath);
67
        $imagick->clear();
68
        $imagick->destroy();
69
    }
70
}
71