LanguageHandler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 85
rs 10
c 0
b 0
f 0
ccs 0
cts 28
cp 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 33 8
A getBestLanguageWriter() 0 22 5
1
<?php
2
3
/**
4
 * Handling of the language files.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Localization;
9
10
use HDNET\Autoloader\Localization\Writer\AbstractLocalizationWriter;
11
use TYPO3\CMS\Core\Localization\LanguageStore;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
14
15
/**
16
 * Handling of the language files.
17
 */
18
class LanguageHandler extends LanguageStore
19
{
20
    /**
21
     * Cache the created labels.
22
     *
23
     * @var array
24
     */
25
    protected static $createdLabelCache = [];
26
27
    /**
28
     * handler the adding of files.
29
     *
30
     * @param string $key                  key in the localization file
31
     * @param string $extensionName
32
     * @param string $default              default value of the label
33
     * @param array  $arguments            arguments are being passed over to vsprintf
34
     * @param string $overrideLanguageBase
35
     *
36
     * @return string|null
37
     */
38
    public function handle($key, $extensionName, $default, $arguments, $overrideLanguageBase = null)
39
    {
40
        // If we are called early in the TYPO3 bootstrap we mus return early with the default label
41
        if (empty($GLOBALS['TCA'])) {
42
            return $default;
43
        }
44
45
        if (TYPO3_MODE === 'BE' && !isset($GLOBALS['LANG'])) {
46
            $GLOBALS['LANG'] = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageService::class);
47
            $GLOBALS['LANG']->init($GLOBALS['BE_USER']->uc['lang']);
48
        }
49
50
        $value = LocalizationUtility::translate($key, $extensionName, $arguments);
51
52
        if (null !== $value) {
53
            return $value;
54
        }
55
56
        if (null === $default || '' === $default) {
57
            $default = $extensionName . ' ==> LLL:' . $key;
58
        }
59
60
        $handler = $this->getBestLanguageWriter($extensionName, $overrideLanguageBase);
61
        $handler->createFileIfNotExists($extensionName);
62
63
        $labelCacheKey = $extensionName . '|' . $key;
64
        if (!\in_array($labelCacheKey, self::$createdLabelCache, true)) {
65
            $handler->addLabel($extensionName, $key, $default);
66
            self::$createdLabelCache[] = $labelCacheKey;
67
        }
68
69
        return $default;
70
    }
71
72
    /**
73
     * Get the best language writer.
74
     *
75
     * @param string $extensionKey
76
     * @param string $overrideLanguageBase
77
     *
78
     * @return AbstractLocalizationWriter
79
     */
80
    protected function getBestLanguageWriter($extensionKey, $overrideLanguageBase = null)
81
    {
82
        $services = [];
83
        foreach ($this->getSupportedExtensions() as $serviceKey) {
84
            if (!isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['writer'][$serviceKey])) {
85
                continue;
86
            }
87
            $serviceName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['lang']['writer'][$serviceKey];
88
89
            /** @var AbstractLocalizationWriter $service */
90
            $service = GeneralUtility::makeInstance($serviceName);
91
            if (null !== $overrideLanguageBase) {
92
                $service->setLanguageBaseName($overrideLanguageBase);
93
            }
94
            if (is_file($service->getAbsoluteFilename($extensionKey))) {
95
                return $service;
96
            }
97
            $services[] = $service;
98
        }
99
100
        return $services[0];
101
    }
102
}
103