1 | <?php |
||
2 | |||
3 | namespace Rvdlee\ZfImageResizer\Controller; |
||
4 | |||
5 | use Exception; |
||
6 | use Rvdlee\ZfImageResizer\Exception\InvalidArgumentException; |
||
7 | use Rvdlee\ZfImageResizer\Service\ImageResizerService; |
||
8 | use Zend\Mvc\Controller\AbstractActionController; |
||
9 | |||
10 | class ConsoleController extends AbstractActionController |
||
11 | { |
||
12 | /** |
||
13 | * @var ImageResizerService |
||
14 | */ |
||
15 | protected $imageResizerService; |
||
16 | |||
17 | public function __construct(ImageResizerService $imageResizerService) |
||
18 | { |
||
19 | $this->setImageResizerService($imageResizerService); |
||
20 | } |
||
21 | |||
22 | /** |
||
23 | * Console wrapper for service |
||
24 | * |
||
25 | * @throws Exception |
||
26 | */ |
||
27 | public function resizeImageAction() |
||
28 | { |
||
29 | /** @var string $image */ |
||
30 | $image = $this->params()->fromRoute('image', null); |
||
31 | if ($image === null) { |
||
32 | throw new InvalidArgumentException('You need the --image|-i param.'); |
||
33 | } |
||
34 | |||
35 | /** @var string $width */ |
||
36 | $width = $this->params()->fromRoute('width', null); |
||
37 | if ($width === null) { |
||
38 | throw new InvalidArgumentException('You need the --width|-w param.'); |
||
39 | } |
||
40 | |||
41 | /** @var string $height */ |
||
42 | $height = $this->params()->fromRoute('height', null); |
||
43 | if ($height === null) { |
||
44 | throw new InvalidArgumentException('You need the --height|-h param.'); |
||
45 | } |
||
46 | |||
47 | /** @var string $modus */ |
||
48 | $modus = $this->params()->fromRoute('modus', null); |
||
49 | if ($modus === null) { |
||
50 | throw new InvalidArgumentException('You need the --modus|-m param.'); |
||
51 | } |
||
52 | |||
53 | /** @var string $cropModus */ |
||
54 | $cropModus = $this->params()->fromRoute('crop-modus', null); |
||
55 | if ($cropModus === null) { |
||
56 | throw new InvalidArgumentException('You need the --crop-modus|-cm param.'); |
||
57 | } |
||
58 | |||
59 | /** @var string $x */ |
||
60 | $x = $this->params()->fromRoute('x', null); |
||
61 | if ($x === null) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
62 | throw new InvalidArgumentException('You need the --x|-x param.'); |
||
63 | } |
||
64 | |||
65 | /** @var string $y */ |
||
66 | $y = $this->params()->fromRoute('y', null); |
||
67 | if ($y === null) { |
||
0 ignored issues
–
show
|
|||
68 | throw new InvalidArgumentException('You need the --y|-y param.'); |
||
69 | } |
||
70 | |||
71 | try { |
||
72 | $this->getImageResizerService()->resize($image); |
||
73 | } catch (Exception $exception) { |
||
74 | throw $exception; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return ImageResizerService |
||
80 | */ |
||
81 | public function getImageResizerService(): ImageResizerService |
||
82 | { |
||
83 | return $this->imageResizerService; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param ImageResizerService $imageResizerService |
||
88 | * @return ConsoleController |
||
89 | */ |
||
90 | public function setImageResizerService(ImageResizerService $imageResizerService): ConsoleController |
||
91 | { |
||
92 | $this->imageResizerService = $imageResizerService; |
||
93 | return $this; |
||
94 | } |
||
95 | } |