CropCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Jackal\ImageMerge\Command;
4
5
use InvalidArgumentException;
6
use Jackal\ImageMerge\Command\Options\CropCommandOption;
7
use Jackal\ImageMerge\Model\Image;
8
9
/**
10
 * Class CropCommand
11
 * @package Jackal\ImageMerge\Command
12
 */
13
class CropCommand extends AbstractCommand
14
{
15
    /**
16
     * CropCommand constructor.
17
     * @param CropCommandOption $options
18
     */
19
    public function __construct(CropCommandOption $options)
20
    {
21
        parent::__construct($options);
22
    }
23
24
    /**
25
     * @param Image $image
26
     * @return Image
27
     */
28
    public function execute(Image $image)
29
    {
30
        if ($image->getWidth() == $this->options->getDimention()->getWidth() and $image->getHeight() == $this->options->getDimention()->getHeight()) {
0 ignored issues
show
Bug introduced by
The method getDimention() does not exist on Jackal\ImageMerge\Comman...\CommandOptionInterface. It seems like you code against a sub-type of Jackal\ImageMerge\Comman...\CommandOptionInterface such as Jackal\ImageMerge\Comman...\DimensionCommandOption. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        if ($image->getWidth() == $this->options->/** @scrutinizer ignore-call */ getDimention()->getWidth() and $image->getHeight() == $this->options->getDimention()->getHeight()) {
Loading history...
31
            return $image;
32
        }
33
34
        if ($this->options->getDimention()->getWidth() > $image->getWidth() || $this->options->getDimention()->getHeight() > $image->getHeight()) {
35
            throw new InvalidArgumentException(sprintf('Crop area exceed, max dimensions are: %s X %s', $image->getWidth(), $image->getHeight()));
36
        }
37
38
        /** @var CropCommandOption $options */
39
        $options = $this->options;
40
        $newImage = imagecrop($image->getResource(), [
41
            'x' => $options->getCoordinate1()->getX(),
42
            'y' => $options->getCoordinate1()->getY(),
43
            'width' => $options->getDimention()->getWidth(),
44
            'height' => $options->getDimention()->getHeight(),
45
        ]);
46
47
        return $image->assignResource($newImage);
48
    }
49
}
50