AbstractLocalizationWriter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 85
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createFileIfNotExists() 0 9 2
A getLanguageBaseName() 0 4 1
A setLanguageBaseName() 0 4 1
A clearCache() 0 19 4
A wrapCdata() 0 8 2
1
<?php
2
3
/**
4
 * Abstraction of the Writer.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Localization\Writer;
9
10
use HDNET\Autoloader\Utility\FileUtility;
11
use TYPO3\CMS\Core\Cache\CacheManager;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
14
/**
15
 * Abstraction of the Writer.
16
 */
17
abstract class AbstractLocalizationWriter implements LocalizationWriterInterface
18
{
19
    /**
20
     * Language file base name.
21
     *
22
     * @var string
23
     */
24
    protected $languageBaseName = 'locallang';
25
26
    /**
27
     * Create default file.
28
     *
29
     * @param string $extensionKey
30
     *
31
     * @return bool
32
     */
33
    public function createFileIfNotExists($extensionKey)
34
    {
35
        $fileName = $this->getAbsoluteFilename($extensionKey);
36
        if (is_file($fileName)) {
37
            return true;
38
        }
39
40
        return FileUtility::writeFileAndCreateFolder($fileName, $this->getBaseFileContent($extensionKey));
41
    }
42
43
    /**
44
     * Get language base name.
45
     *
46
     * @return string
47
     */
48
    public function getLanguageBaseName()
49
    {
50
        return $this->languageBaseName;
51
    }
52
53
    /**
54
     * Set language base name.
55
     *
56
     * @param string $languageBaseName
57
     */
58
    public function setLanguageBaseName($languageBaseName): void
59
    {
60
        $this->languageBaseName = $languageBaseName;
61
    }
62
63
    /**
64
     * flush the l10n caches.
65
     */
66
    protected function clearCache(): void
67
    {
68
        $caches = [
69
            't3lib_l10n',
70
            'l10n',
71
        ];
72
        /** @var CacheManager $cacheManager */
73
        $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
74
        foreach ($caches as $name) {
75
            try {
76
                $cache = $cacheManager->getCache($name);
77
                if ($cache) {
78
                    $cache->flush();
79
                }
80
            } catch (\Exception $ex) {
81
                // Catch is empty, because we want to avoid exceptions in this process
82
            }
83
        }
84
    }
85
86
    /**
87
     * Wrap CDATA.
88
     *
89
     * @param string $content
90
     *
91
     * @return string
92
     */
93
    protected function wrapCdata($content)
94
    {
95
        if (htmlentities($content, ENT_NOQUOTES) !== $content) {
96
            $content = '<![CDATA[' . $content . ']]>';
97
        }
98
99
        return $content;
100
    }
101
}
102