ModelUtility::getModel()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.7537
c 0
b 0
f 0
ccs 0
cts 12
cp 0
cc 6
nc 4
nop 2
crap 42
1
<?php
2
3
/**
4
 * Utility to interact with the Model.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Utility;
9
10
use Doctrine\Common\Annotations\AnnotationReader;
11
use HDNET\Autoloader\Annotation\DatabaseTable;
12
use HDNET\Autoloader\Annotation\SmartExclude;
13
use HDNET\Autoloader\Service\SmartObjectInformationService;
14
use HDNET\Autoloader\SmartObjectRegister;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Extbase\Persistence\Generic\Session;
17
18
/**
19
 * Utility to interact with the Model.
20
 */
21
class ModelUtility
22
{
23
    /**
24
     * Get the table name by either reflection or model name.
25
     *
26
     * @param $modelClassName
27
     *
28
     * @return string
29
     */
30
    public static function getTableName($modelClassName)
31
    {
32
        $reflectionName = self::getTableNameByModelReflectionAnnotation($modelClassName);
33
34
        return '' !== $reflectionName ? $reflectionName : self::getTableNameByModelName($modelClassName);
35
    }
36
37
    /**
38
     * Get the table name by reflection.
39
     *
40
     * @param string $modelClassName
41
     *
42
     * @return string
43
     */
44
    public static function getTableNameByModelReflectionAnnotation($modelClassName)
45
    {
46
        /** @var AnnotationReader $annotationReader */
47
        $annotationReader = GeneralUtility::makeInstance(AnnotationReader::class);
48
        /** @var DatabaseTable $classAnnotation */
49
        $classAnnotation = $annotationReader->getClassAnnotation(new \ReflectionClass($modelClassName), DatabaseTable::class);
50
51
        if (null === $classAnnotation) {
52
            return '';
53
        }
54
55
        return (string)$classAnnotation->tableName;
56
    }
57
58
    /**
59
     * Resolve the table name for the given class name
60
     * Original method from extbase core to create the table name.
61
     *
62
     * @param string $className
63
     *
64
     * @return string The table name
65
     *
66
     * @see DataMapFactory->resolveTableName
67
     */
68
    public static function getTableNameByModelName($className)
69
    {
70
        $className = ltrim($className, '\\');
71
        if (false !== mb_strpos($className, '\\')) {
72
            $classNameParts = explode('\\', $className);
73
            // Skip vendor and product name for core classes
74
            if (0 === mb_strpos($className, 'TYPO3\\CMS\\')) {
75
                $classPartsToSkip = 2;
76
            } else {
77
                $classPartsToSkip = 1;
78
            }
79
            $classNameParts = \array_slice($classNameParts, $classPartsToSkip);
80
            $classNameParts = explode('\\', implode('\\', $classNameParts), 4);
81
            $tableName = 'tx_' . str_replace('\\', '_', mb_strtolower(implode('_', $classNameParts)));
82
        } else {
83
            $tableName = mb_strtolower($className);
84
        }
85
86
        return $tableName;
87
    }
88
89
    /**
90
     * get the smart exclude values e.g. language, workspace,
91
     * enableFields from the given model.
92
     */
93
    public static function getSmartExcludesByModelName(string $name): array
94
    {
95
        /** @var AnnotationReader $annotationReader */
96
        $annotationReader = GeneralUtility::makeInstance(AnnotationReader::class);
97
98
        $reflectionClass = new \ReflectionClass($name);
99
100
        $smartExcludes = $annotationReader->getClassAnnotation($reflectionClass, SmartExclude::class);
101
102
        return null === $smartExcludes ? [] : $smartExcludes->excludes;
103
    }
104
105
    /**
106
     * Get the base TCA for the given Model.
107
     *
108
     * @param string $modelClassName
109
     *
110
     * @return array
111
     */
112
    public static function getTcaInformation($modelClassName)
113
    {
114
        $informationService = SmartObjectInformationService::getInstance();
115
116
        return $informationService->getTcaInformation($modelClassName);
117
    }
118
119
    /**
120
     * Get the default TCA incl. smart object fields.
121
     * Add missing fields to the existing TCA structure.
122
     *
123
     * @param string $extensionKey
124
     * @param string $tableName
125
     *
126
     * @return array
127
     */
128
    public static function getTcaOverrideInformation($extensionKey, $tableName)
129
    {
130
        $return = $GLOBALS['TCA'][$tableName] ?? [];
131
        $classNames = SmartObjectRegister::getRegister();
132
        $informationService = SmartObjectInformationService::getInstance();
133
134
        foreach ($classNames as $className) {
135
            if (ClassNamingUtility::getExtensionKeyByModel($className) !== $extensionKey) {
136
                continue;
137
            }
138
            if (self::getTableNameByModelReflectionAnnotation($className) === $tableName) {
139
                $additionalTca = $informationService->getCustomModelFieldTca($className);
140
                foreach ($additionalTca as $fieldName => $configuration) {
141
                    if (!isset($return['columns'][$fieldName])) {
142
                        $return['columns'][$fieldName] = $configuration;
143
                    }
144
                }
145
            }
146
        }
147
148
        return $return;
149
    }
150
151
    /**
152
     * Get the target model.
153
     *
154
     * @param string $modelName
155
     * @param array  $data
156
     *
157
     * @return \TYPO3\CMS\Extbase\DomainObject\AbstractEntity|object
158
     */
159
    public static function getModel($modelName, $data)
160
    {
161
        // Base query
162
        $query = ExtendedUtility::getQuery($modelName);
163
        $settings = $query->getQuerySettings();
164
        $settings->setRespectStoragePage(false);
165
        $settings->setRespectSysLanguage(false);
166
        $query->matching($query->equals('uid', $data['uid']));
167
168
        if (TYPO3_MODE === 'BE') {
169
            GeneralUtility::makeInstance(Session::class)->destroy();
170
            $settings->setIgnoreEnableFields(true);
171
172
            if (isset($data['sys_language_uid']) && (int)$data['sys_language_uid'] > 0) {
173
                $_GET['L'] = (int)$data['sys_language_uid'];
174
175
                if (isset($data['l18n_parent']) && $data['l18n_parent'] > 0) {
176
                    $settings->setLanguageOverlayMode(false);
177
                    $settings->setLanguageMode(null);
178
                    $settings->setRespectSysLanguage(true);
179
                    $settings->setLanguageUid((int)$data['sys_language_uid']);
180
                }
181
                $object = $query->execute()->getFirst();
182
183
                return $object;
184
            }
185
        }
186
187
        $query->matching($query->equals('uid', $data['uid']));
188
189
        return $query->execute()
190
            ->getFirst()
191
        ;
192
    }
193
}
194