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

parseSizeParameter()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 25
ccs 0
cts 21
cp 0
rs 4.909
cc 9
eloc 17
nc 6
nop 1
crap 90
1
<?php
2
namespace Staticus\Resources\Middlewares\Image;
3
4
use Staticus\Exceptions\WrongRequestException;
5
use Staticus\Resources\Image\CropImageDO;
6
use Staticus\Resources\Image\ResourceImageDO;
7
use Staticus\Resources\Image\ResourceImageDOInterface;
8
use Staticus\Resources\Middlewares\PrepareResourceMiddlewareAbstract;
9
10
abstract class PrepareImageMiddlewareAbstract extends PrepareResourceMiddlewareAbstract
11
{
12
    protected function fillResourceSpecialFields()
13
    {
14
        $size = static::getParamFromRequest('size', $this->request);
15
        $this->parseSizeParameter($size);
16
        $crop = static::getParamFromRequest('crop', $this->request);
17
        $this->parseCropParameter($crop);
18
    }
19
20
    protected function parseCropParameter($crop)
21
    {
22
        if ($crop) {
23
            /* @var ResourceImageDOInterface $resource */
24
            $resource = $this->resourceDO;
25
            $crop = explode('x', $crop);
26
            if (count($crop) != 4) {
27
                throw new WrongRequestException('Crop parameter has to consist of four parts, concatenated by "x" char.');
28
            }
29
30
            $cropObject = new CropImageDO();
31
            $cropObject->setX((int) $crop[0]);
32
            $cropObject->setY((int) $crop[1]);
33
            $cropObject->setWidth((int) $crop[2]);
34
            $cropObject->setHeight((int) $crop[3]);
35
36
            if (!$resource->getWidth() || !$cropObject->getHeight()) {
37
                throw new WrongRequestException('You should send the size=[X]x[Y] parameter together with the crop parameter');
38
            }
39
            if ($cropObject->getX() < 0 || $cropObject->getY() < 0 ||
40
                $cropObject->getWidth() < 1 || $cropObject->getHeight() < 1
41
            ) {
42
                throw new WrongRequestException('Crop parameters can not be less than zero');
43
            }
44
45
            $resizeRatio = $resource->getWidth() / $resource->getHeight();
46
            $cropRatio = $cropObject->getWidth() / $cropObject->getHeight();
47
48
            if ($resizeRatio !== $cropRatio) {
49
                throw new WrongRequestException('Wrong width to height ratio in crop parameter.
50
                    It should be same as width to height ratio in size parameter');
51
            }
52
53
            $resource->setCrop($cropObject);
54
        }
55
    }
56
57
    protected function parseSizeParameter($size)
58
    {
59
        $width = ResourceImageDO::DEFAULT_WIDTH;
60
        $height = ResourceImageDO::DEFAULT_HEIGHT;
61
        $resource = $this->resourceDO;
62
        if ($size) {
63
            $size = explode('x', $size);
64
            if (!empty($size[0]) && !empty($size[1])) {
65
                $width = (int)$size[0];
66
                $height = (int)$size[1];
67
                if ($width < 0 || $height < 0) {
68
                    throw new WrongRequestException('Sizes can not be less than zero');
69
                }
70
                if ($width && $height) {
71
                    $allowedSizes = $this->config->get('staticus.images.sizes');
72
                    if (!in_array([$width, $height], $allowedSizes)) {
73
                        throw new WrongRequestException('Resource size is not allowed: ' . $width . 'x' . $height);
74
                    }
75
                }
76
            }
77
        }
78
        /** @var ResourceImageDOInterface $resource */
79
        $resource->setWidth($width);
80
        $resource->setHeight($height);
81
    }
82
}
83