GetData::getDataExtension()   C
last analyzed

Complexity

Conditions 15
Paths 17

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 5.9166
c 0
b 0
f 0
cc 15
nc 17
nop 5

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
/**
4
 * Get data for focuspoint.
5
 */
6
7
namespace HDNET\Focuspoint\Hooks;
8
9
use HDNET\Autoloader\Annotation\Hook;
10
use TYPO3\CMS\Core\Resource\FileReference;
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
use TYPO3\CMS\Frontend\ContentObject\ContentObjectGetDataHookInterface;
13
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
14
15
/**
16
 * Get data for focuspoint.
17
 *
18
 * @Hook("TYPO3_CONF_VARS|SC_OPTIONS|tslib/class.tslib_content.php|getData")
19
 */
20
class GetData implements ContentObjectGetDataHookInterface
21
{
22
    /**
23
     * Extends the getData()-Method of ContentObjectRenderer to process more/other commands.
24
     *
25
     * @param string                $getDataString Full content of getData-request e.g. "TSFE:id // field:title // field:uid
26
     * @param array                 $fields        Current field-array
27
     * @param string                $sectionValue  Currently examined section value of the getData request e.g. "field:title
28
     * @param string                $returnValue   Current returnValue that was processed so far by getData
29
     * @param ContentObjectRenderer $parentObject  Parent content object
30
     *
31
     * @return string Get data result
32
     */
33
    public function getDataExtension(
34
        $getDataString,
35
        array $fields,
36
        $sectionValue,
37
        $returnValue,
38
        ContentObjectRenderer &$parentObject
39
    ) {
40
        $parts = \explode(':', $getDataString);
41
        if (isset($parts[0], $parts[1]) && 'fp' === $parts[0]) {
42
            $fileObject = $parentObject->getCurrentFile();
43
            if (!($fileObject instanceof FileReference)) {
44
                return $returnValue;
45
            }
46
            $originalFile = $fileObject->getOriginalFile();
47
            switch ($parts[1]) {
48
                case 'x':
49
                case 'y':
50
                    $metaData = $originalFile->_getMetaData();
51
52
                    return $metaData['focus_point_' . $parts[1]] / 100;
53
                case 'xp':
54
                case 'yp':
55
                    $metaData = $originalFile->_getMetaData();
56
57
                    return (float) $metaData['focus_point_' . \mb_substr($parts[1], 0, 1)];
58
                case 'xp_positive':
59
                case 'yp_positive':
60
                    $metaData = $originalFile->_getMetaData();
61
                    if ('xp_positive' === $parts[1]) {
62
                        return (int) (\abs($metaData['focus_point_' . \mb_substr($parts[1], 0, 1)] + 100) / 2);
63
                    }
64
65
                    return (int) (\abs($metaData['focus_point_' . \mb_substr($parts[1], 0, 1)] - 100) / 2);
66
                case 'w':
67
                case 'h':
68
                    $fileName = GeneralUtility::getFileAbsFileName($fileObject->getPublicUrl(true));
69
                    if (\file_exists($fileName)) {
70
                        $sizes = \getimagesize($fileName);
71
72
                        return $sizes[('w' === $parts[1] ? 0 : 1)];
73
                    }
74
                    break;
75
            }
76
        }
77
78
        return $returnValue;
79
    }
80
}
81