Completed
Push — master ( 2eb7e3...927700 )
by Tim
25:04 queued 10:07
created

ImageViewHelper::renderStatic()   C

Complexity

Conditions 11
Paths 56

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 30
nc 56
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 TYPO3Fluid\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
     * @param array                     $arguments
30
     * @param callable|\Closure         $renderChildrenClosure
31
     * @param RenderingContextInterface $renderingContext
32
     *
33
     * @return string
34
     * @throws Exception
35
     */
36
    public static function renderStatic(
37
        array $arguments,
38
        \Closure $renderChildrenClosure,
39
        RenderingContextInterface $renderingContext
40
    ) {
41
        $src = $arguments['src'];
42
        $image = $arguments['image'];
43
        $treatIdAsReference = $arguments['treatIdAsReference'];
44
        $crop = $arguments['crop'];
45
        //$absolute = $arguments['absolute'];
46
47
        if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
48
            throw new Exception('You must either specify a string src or a File object.', 1382284105);
49
        }
50
51
        try {
52
            $imageService = self::getImageService();
53
            $image = $imageService->getImage($src, $image, $treatIdAsReference);
54
55
            if ($crop === null) {
56
                $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
57
            }
58
59
            $processingInstructions = [
60
                'width'     => $arguments['width'],
61
                'height'    => $arguments['height'],
62
                'minWidth'  => $arguments['minWidth'],
63
                'minHeight' => $arguments['minHeight'],
64
                'maxWidth'  => $arguments['maxWidth'],
65
                'maxHeight' => $arguments['maxHeight'],
66
                'crop'      => $crop,
67
            ];
68
            $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
69
            return $imageService->getImageUri($processedImage);
70
        } catch (ResourceDoesNotExistException $e) {
71
            // thrown if file does not exist
72
        } catch (\UnexpectedValueException $e) {
73
            // thrown if a file has been replaced with a folder
74
        } catch (\RuntimeException $e) {
75
            // RuntimeException thrown if a file is outside of a storage
76
        } catch (\InvalidArgumentException $e) {
77
            // thrown if file storage does not exist
78
        }
79
        return '';
80
    }
81
82
    /**
83
     * Return an instance of ImageService using object manager
84
     *
85
     * @return ImageService
86
     */
87
    protected static function getImageService()
88
    {
89
        /** @var ObjectManager $objectManager */
90
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
91
        return $objectManager->get(ImageService::class);
92
    }
93
}
94