LocalCropScaleMaskHelper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Local crop scale mask helper (overwrite).
5
 */
6
7
namespace HDNET\Focuspoint\Xclass;
8
9
use HDNET\Focuspoint\Service\DimensionService;
10
use HDNET\Focuspoint\Service\FocusCropService;
11
use TYPO3\CMS\Backend\Utility\BackendUtility;
12
use TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor;
13
use TYPO3\CMS\Core\Resource\Processing\TaskInterface;
14
use TYPO3\CMS\Core\Resource\ResourceFactory;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
17
18
/**
19
 * Local crop scale mask helper (overwrite).
20
 */
21
class LocalCropScaleMaskHelper extends \TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper
22
{
23
    /**
24
     * If set to true, the pocess is running and no addinal calculation are needed.
25
     *
26
     * @var bool
27
     */
28
    protected static $deepCheck = false;
29
30
    /**
31
     * Dimension service.
32
     *
33
     * @var DimensionService
34
     */
35
    protected $dimensionService;
36
37
    /**
38
     * focus crop service.
39
     *
40
     * @var FocusCropService
41
     */
42
    protected $focusCropService;
43
44
    /**
45
     * Build up the object.
46
     *
47
     * @param LocalImageProcessor $processor
48
     */
49
    public function __construct(LocalImageProcessor $processor)
50
    {
51
        $this->dimensionService = GeneralUtility::makeInstance(DimensionService::class);
52
        $this->focusCropService = GeneralUtility::makeInstance(FocusCropService::class);
53
        parent::__construct($processor);
54
    }
55
56
    /**
57
     * Processing the focus point crop (fallback to LocalCropScaleMaskHelper).
58
     *
59
     * @param TaskInterface $task
60
     *
61
     * @return array|null
62
     */
63
    public function process(TaskInterface $task)
64
    {
65
        $configuration = $task->getConfiguration();
66
        $crop = $configuration['crop'] ? \json_decode($configuration['crop']) : null;
67
        if ($crop instanceof \stdClass && isset($crop->x)) {
68
            // if crop is enable release the process
69
            return parent::process($task);
70
        }
71
72
        $sourceFile = $task->getSourceFile();
73
        try {
74
            if (false === self::$deepCheck) {
75
                self::$deepCheck = true;
76
                $ratio = $this->getCurrentRatioConfiguration();
77
                $this->dimensionService->getRatio($ratio);
78
79
                $newFile = $this->focusCropService->getCroppedImageSrcByFile($sourceFile, $ratio);
80
                if (null === $newFile) {
81
                    return parent::process($task);
82
                }
83
                $file = ResourceFactory::getInstance()
84
                    ->retrieveFileOrFolderObject($newFile);
85
86
                $targetFile = $task->getTargetFile();
87
                ObjectAccess::setProperty($targetFile, 'originalFile', $file, true);
88
                ObjectAccess::setProperty($targetFile, 'originalFileSha1', $file->getSha1(), true);
89
                ObjectAccess::setProperty($targetFile, 'storage', $file->getStorage(), true);
90
                ObjectAccess::setProperty($task, 'sourceFile', $file, true);
91
                ObjectAccess::setProperty($task, 'targetFile', $targetFile, true);
92
            }
93
        } catch (\Exception $ex) {
94
            // not handled
95
        }
96
        self::$deepCheck = false;
97
98
        return parent::process($task);
99
    }
100
101
    /**
102
     * Find the current ratio configuration.
103
     *
104
     * @throws \Exception
105
     *
106
     * @return string
107
     */
108
    protected function getCurrentRatioConfiguration(): string
109
    {
110
        $currentRecord = $GLOBALS['TSFE']->currentRecord;
111
        $parts = GeneralUtility::trimExplode(':', $currentRecord);
112
        if (2 !== \count($parts)) {
113
            throw new \Exception('Invalid count of current record parts', 12367);
114
        }
115
        if ('tt_content' !== $parts[0]) {
116
            throw new \Exception('Invalid part 0. part 0 have to be tt_content', 127383);
117
        }
118
        $record = BackendUtility::getRecord($parts[0], (int) $parts[1]);
119
        if (!isset($record['image_ratio'])) {
120
            throw new \Exception('No image_ratio found in the current record', 324672);
121
        }
122
123
        return \trim((string) $record['image_ratio']);
124
    }
125
}
126