Passed
Push — master ( 19bb20...638f50 )
by Rob
01:28
created

ConsoleController::resizeImageAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 4
b 0
f 0
nc 3
nop 0
dl 0
loc 25
rs 9.8333
1
<?php
2
3
namespace Rvdlee\ZfImageResizer\Controller;
4
5
use Exception;
6
use Rvdlee\ZfImageResizer\Exception\InvalidArgumentException;
7
use Rvdlee\ZfImageResizer\Model\Image;
8
use Rvdlee\ZfImageResizer\Service\ImageResizerService;
9
use Zend\Mvc\Controller\AbstractActionController;
10
11
class ConsoleController extends AbstractActionController
12
{
13
    /**
14
     * @var ImageResizerService
15
     */
16
    protected $imageResizerService;
17
18
    public function __construct(ImageResizerService $imageResizerService)
19
    {
20
        $this->setImageResizerService($imageResizerService);
21
    }
22
23
    /**
24
     * Console wrapper for service
25
     *
26
     * @throws Exception
27
     */
28
    public function resizeImageAction()
29
    {
30
        /** @var string $image */
31
        $image = $this->params()->fromRoute('image', null);
32
        if ( ! file_exists($image)) {
33
            throw new InvalidArgumentException('You need the --image|-i param to be an existing image.');
34
        }
35
36
        /** @var int $width */
37
        $width = (int) $this->params()->fromRoute('width', 100);
38
        /** @var int $height */
39
        $height = (int) $this->params()->fromRoute('height', 100);
40
        /** @var string $modus */
41
        $modus = $this->params()->fromRoute('modus', Image::ONLY_RESIZE_MODUS);
0 ignored issues
show
Unused Code introduced by
The assignment to $modus is dead and can be removed.
Loading history...
42
        /** @var string $cropModus */
43
        $cropModus = $this->params()->fromRoute('crop-modus', Image::CENTERED_CROP);
0 ignored issues
show
Unused Code introduced by
The assignment to $cropModus is dead and can be removed.
Loading history...
44
        /** @var int $x */
45
        $x = (int) $this->params()->fromRoute('x', 0);
0 ignored issues
show
Unused Code introduced by
The assignment to $x is dead and can be removed.
Loading history...
46
        /** @var int $y */
47
        $y = (int) $this->params()->fromRoute('y', 0);
0 ignored issues
show
Unused Code introduced by
The assignment to $y is dead and can be removed.
Loading history...
48
49
        try {
50
            $this->getImageResizerService()->resizeImage($image, $width, $height);
51
        } catch (Exception $exception) {
52
            throw $exception;
53
        }
54
    }
55
56
    /**
57
     * @return ImageResizerService
58
     */
59
    public function getImageResizerService(): ImageResizerService
60
    {
61
        return $this->imageResizerService;
62
    }
63
64
    /**
65
     * @param ImageResizerService $imageResizerService
66
     * @return ConsoleController
67
     */
68
    public function setImageResizerService(ImageResizerService $imageResizerService): ConsoleController
69
    {
70
        $this->imageResizerService = $imageResizerService;
71
        return $this;
72
    }
73
}