Image::renderInternal()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 9
nc 2
nop 0
1
<?php
2
/**
3
 * Rendering image elements
4
 *
5
 * @author Tim Lochmüller
6
 */
7
8
namespace FRUIT\Ink\Rendering;
9
10
use TYPO3\CMS\Core\Resource\File;
11
use TYPO3\CMS\Core\Resource\FileRepository;
12
use TYPO3\CMS\Extbase\Object\ObjectManager;
13
14
/**
15
 * Rendering image elements
16
 */
17
class Image extends AbstractRendering
18
{
19
20
    /**
21
     * Render the given element
22
     *
23
     * @return array
24
     */
25
    public function renderInternal()
26
    {
27
        $objectManager = new ObjectManager();
28
        /** @var FileRepository $fileRepository */
29
        $fileRepository = $objectManager->get('TYPO3\\CMS\\Core\\Resource\\FileRepository');
30
        $files = $fileRepository->findByRelation('tt_content', 'image', $this->contentObject->data['uid']);
31
32
        $images_arr = array();
33
        foreach ($files as $file) {
34
            /** @var $file File */
35
            $images_arr[] = $this->getLink($file->getPublicUrl());
0 ignored issues
show
Security Bug introduced by
It seems like $file->getPublicUrl() targeting TYPO3\CMS\Core\Resource\File::getPublicUrl() can also be of type false; however, FRUIT\Ink\Rendering\AbstractRendering::getLink() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
36
        }
37
38
        $lines[] = $this->renderImagesHelper($images_arr, !$this->contentObject->data['image_zoom'] ? $this->contentObject->data['image_link'] : '', $this->contentObject->data['imagecaption']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$lines was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lines = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
39
40
        return $lines;
41
42
    }
43
44
    /**
45
     * Render block of images - which means creating lines with links to the images.
46
     *
47
     * @param     array $images_arr : the image array
48
     * @param    string $links      : Link value from the "image_link" field in tt_content records
49
     * @param    string $caption    : Caption text
50
     *
51
     * @return    string        Content
52
     * @see getImages()
53
     */
54
    public function renderImagesHelper($images_arr, $links, $caption)
55
    {
56
        $linksArr = explode(',', $links);
57
        $lines = array();
58
        $imageExists = false;
59
60
        foreach ($images_arr as $k => $file) {
61
            if (strlen(trim($file)) > 0) {
62
                $lines[] = $file;
63
                if ($links && count($linksArr) > 1) {
64
                    if (isset($linksArr[$k])) {
65
                        $ll = $linksArr[$k];
66
                    } else {
67
                        $ll = $linksArr[0];
68
                    }
69
70
                    $theLink = $this->getLink($ll);
71
                    if ($theLink) {
72
                        $lines[] = $this->configuration['images.']['linkPrefix'] . $theLink;
73
                    }
74
                }
75
                $imageExists = true;
76
            }
77
        }
78
        if ($this->configuration['images.']['header'] && $imageExists) {
79
            array_unshift($lines, $this->configuration['images.']['header']);
80
        }
81
        if ($links && count($linksArr) == 1) {
82
            $theLink = $this->getLink($links);
83
            if ($theLink) {
84
                $lines[] = $this->configuration['images.']['linkPrefix'] . $theLink;
85
            }
86
        }
87
        if ($caption) {
88
            $lines[] = '';
89
            $cHeader = trim($this->configuration['images.']['captionHeader']);
90
            if ($cHeader) {
91
                $lines[] = $cHeader;
92
            }
93
            $lines[] = $this->breakContent($caption);
94
        }
95
96
        return chr(10) . implode(chr(10), $lines);
97
    }
98
}
99