Completed
Push — master ( e102f3...fc92b7 )
by Tim
15:38
created

ImageViewHelper::render()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 47
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 47
rs 9.0303
cc 3
eloc 42
nc 3
nop 11

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Overwrite Uri/ImageViewHelper
4
 *
5
 * @author  Dennis Geldmacher
6
 */
7
8
namespace FRUIT\FlRealurlImage\Xclass\Fluid\ViewHelpers\Uri;
9
10
use FRUIT\FlRealurlImage\Service\ImageService;
11
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
12
use TYPO3\CMS\Core\Resource\FileInterface;
13
use TYPO3\CMS\Core\Resource\FileReference;
14
use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
15
use TYPO3\CMS\Extbase\Object\ObjectManager;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
18
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
19
20
/**
21
 * Overwrite Image ViewHelper
22
 *
23
 * @author Dennis Geldmacher
24
 */
25
class ImageViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper
26
{
27
28
    /**
29
     * Resizes the image (if required) and returns its path. If the image was not resized, the path will be equal to $src
30
     *
31
     * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/ImgResource/
32
     *
33
     * @param string                           $src
34
     * @param FileInterface|AbstractFileFolder $image
35
     * @param string                           $width              width of the image. This can be a numeric value representing the fixed width of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.
36
     * @param string                           $height             height of the image. This can be a numeric value representing the fixed height of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.
37
     * @param int                              $minWidth           minimum width of the image
38
     * @param int                              $minHeight          minimum height of the image
39
     * @param int                              $maxWidth           maximum width of the image
40
     * @param int                              $maxHeight          maximum height of the image
41
     * @param bool                             $treatIdAsReference given src argument is a sys_file_reference record
42
     * @param string|bool                      $crop               overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
43
     * @param bool                             $absolute           Force absolute URL
44
     *
45
     * @return string
46
     * @throws Exception
47
     * @throws \Exception
48
     */
49
    public function render(
50
        $src = null,
51
        $image = null,
52
        $width = null,
53
        $height = null,
54
        $minWidth = null,
55
        $minHeight = null,
56
        $maxWidth = null,
57
        $maxHeight = null,
58
        $treatIdAsReference = false,
59
        $crop = null,
60
        $absolute = false
61
    ) {
62
        if (GeneralUtility::compat_version('7.6')) {
63
            return self::renderStatic([
64
                'src'                => $src,
65
                'image'              => $image,
66
                'width'              => $width,
67
                'height'             => $height,
68
                'minWidth'           => $minWidth,
69
                'minHeight'          => $minHeight,
70
                'maxWidth'           => $maxWidth,
71
                'maxHeight'          => $maxHeight,
72
                'treatIdAsReference' => $treatIdAsReference,
73
                'crop'               => $crop,
74
                'absolute'           => $absolute,
75
            ], $this->buildRenderChildrenClosure(), $this->renderingContext);
76
        } else {
77
            $this->imageService = $this->objectManager->get(ImageService::class);
0 ignored issues
show
Bug introduced by
The property imageService does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
78
            try {
79
                $return = parent::render(
80
                    $src,
81
                    $image,
82
                    $width,
83
                    $height,
84
                    $minWidth,
85
                    $minHeight,
86
                    $maxWidth,
87
                    $maxHeight,
88
                    $treatIdAsReference
89
                );
90
            } catch (\Exception $ex) {
91
                throw $ex;
92
            }
93
            return $return;
94
        }
95
    }
96
97
    /**
98
     * @param array                     $arguments
99
     * @param callable|\Closure         $renderChildrenClosure
100
     * @param RenderingContextInterface $renderingContext
101
     *
102
     * @return string
103
     * @throws Exception
104
     */
105
    public static function renderStatic(
106
        array $arguments,
107
        \Closure $renderChildrenClosure,
108
        RenderingContextInterface $renderingContext
109
    ) {
110
        $src = $arguments['src'];
111
        $image = $arguments['image'];
112
        $treatIdAsReference = $arguments['treatIdAsReference'];
113
        $crop = $arguments['crop'];
114
        //$absolute = $arguments['absolute'];
115
116
        if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
117
            throw new Exception('You must either specify a string src or a File object.', 1382284105);
118
        }
119
120
        try {
121
            $imageService = self::getImageService();
122
            $image = $imageService->getImage($src, $image, $treatIdAsReference);
123
124
            if ($crop === null) {
125
                $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
126
            }
127
128
            $processingInstructions = [
129
                'width'     => $arguments['width'],
130
                'height'    => $arguments['height'],
131
                'minWidth'  => $arguments['minWidth'],
132
                'minHeight' => $arguments['minHeight'],
133
                'maxWidth'  => $arguments['maxWidth'],
134
                'maxHeight' => $arguments['maxHeight'],
135
                'crop'      => $crop,
136
            ];
137
            $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
138
            return $imageService->getImageUri($processedImage);
139
        } catch (ResourceDoesNotExistException $e) {
140
            // thrown if file does not exist
141
        } catch (\UnexpectedValueException $e) {
142
            // thrown if a file has been replaced with a folder
143
        } catch (\RuntimeException $e) {
144
            // RuntimeException thrown if a file is outside of a storage
145
        } catch (\InvalidArgumentException $e) {
146
            // thrown if file storage does not exist
147
        }
148
        return '';
149
    }
150
151
    /**
152
     * Return an instance of ImageService using object manager
153
     *
154
     * @return ImageService
155
     */
156
    protected static function getImageService()
157
    {
158
        /** @var ObjectManager $objectManager */
159
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
160
        return $objectManager->get(ImageService::class);
161
    }
162
}
163