extensionHasSmartObjects()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * ExtensionTypoScriptSetup.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Loader;
9
10
use Doctrine\Common\Annotations\AnnotationReader;
11
use HDNET\Autoloader\Annotation\DatabaseTable;
12
use HDNET\Autoloader\Annotation\ParentClass;
13
use HDNET\Autoloader\Annotation\RecordType;
14
use HDNET\Autoloader\Loader;
15
use HDNET\Autoloader\LoaderInterface;
16
use HDNET\Autoloader\SmartObjectRegister;
17
use HDNET\Autoloader\Utility\ClassNamingUtility;
18
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * ExtensionTypoScriptSetup.
23
 */
24
class ExtensionTypoScriptSetup implements LoaderInterface
25
{
26
    /**
27
     * Get all the complex data for the loader.
28
     * This return value will be cached and stored in the database
29
     * There is no file monitoring for this cache.
30
     */
31
    public function prepareLoader(Loader $loader, int $type): array
32
    {
33
        // We don't have to prepare anything if the extension has no smart objects
34
        if (!$this->extensionHasSmartObjects($loader->getExtensionKey())) {
35
            return [];
36
        }
37
38
        return $this->generateTypoScriptSetup($loader->getExtensionKey());
39
    }
40
41
    /**
42
     * Run the loading process for the ext_tables.php file.
43
     */
44
    public function loadExtensionTables(Loader $loader, array $loaderInformation): void
45
    {
46
        $this->addTypoScript($loaderInformation);
47
    }
48
49
    /**
50
     * Run the loading process for the ext_localconf.php file.
51
     */
52
    public function loadExtensionConfiguration(Loader $loader, array $loaderInformation): void
53
    {
54
        $this->addTypoScript($loaderInformation);
55
    }
56
57
    /**
58
     * Add the given loader information as TypoScript.
59
     */
60
    protected function addTypoScript(array $loaderInformation): void
61
    {
62
        if (!empty($loaderInformation)) {
63
            ExtensionManagementUtility::addTypoScriptSetup(LF . implode(LF, $loaderInformation) . LF);
64
        }
65
    }
66
67
    /**
68
     * Generate the TypoScript setup for the smart objects defined
69
     * within the extension.
70
     *
71
     * @param string $extensionKey
72
     *
73
     * @return array
74
     */
75
    private function generateTypoScriptSetup($extensionKey)
76
    {
77
        /** @var AnnotationReader $annotationReader */
78
        $annotationReader = GeneralUtility::makeInstance(AnnotationReader::class);
79
80
        $setup = [];
81
        foreach ($this->getSmartObjectsForExtensionKey($extensionKey) as $className) {
82
            $reflectionClass = new \ReflectionClass($className);
83
84
            $table = (string)$annotationReader->getClassAnnotation($reflectionClass, DatabaseTable::class);
85
            $recordType = (string)$annotationReader->getClassAnnotation($reflectionClass, RecordType::class);
86
            $parentClass = (string)$annotationReader->getClassAnnotation($reflectionClass, ParentClass::class);
87
            if ('' !== $table) {
88
                $setup[] = 'config.tx_extbase.persistence.classes.' . $className . '.mapping.tableName = ' . $table;
89
            }
90
            if ('' !== $recordType) {
91
                $setup[] = 'config.tx_extbase.persistence.classes.' . $className . '.mapping.recordType = ' . $recordType;
92
            }
93
            if ('' !== $parentClass) {
94
                $setup[] = 'config.tx_extbase.persistence.classes.' . $parentClass . '.subclasses.' . $className . ' = ' . $className;
95
            }
96
        }
97
98
        return $setup;
99
    }
100
101
    /**
102
     * Check if the extension has smart objects.
103
     *
104
     * @param string $extensionKey
105
     *
106
     * @return bool
107
     */
108
    private function extensionHasSmartObjects($extensionKey)
109
    {
110
        if ($this->getSmartObjectsForExtensionKey($extensionKey)) {
111
            return true;
112
        }
113
114
        return false;
115
    }
116
117
    /**
118
     * Get the smart objects for the given extension.
119
     *
120
     * @param $extensionKey
121
     *
122
     * @return mixed
123
     */
124
    private function getSmartObjectsForExtensionKey($extensionKey)
125
    {
126
        $smartObjects = SmartObjectRegister::getRegister();
127
        $extensionObjects = [];
128
        foreach ($smartObjects as $className) {
129
            $objectExtension = ClassNamingUtility::getExtensionKeyByModel($className);
130
            if ($objectExtension === $extensionKey) {
131
                $extensionObjects[] = $className;
132
            }
133
        }
134
135
        return $extensionObjects;
136
    }
137
}
138