Completed
Push — unit-test-services ( f70ec7 )
by Romain
03:05
created

CacheService::getBackendCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Formz project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Service;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Exceptions\ClassNotFoundException;
18
use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait;
19
use TYPO3\CMS\Core\Cache\Backend\AbstractBackend;
20
use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
21
use TYPO3\CMS\Core\Cache\CacheManager;
22
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
23
use TYPO3\CMS\Core\SingletonInterface;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
class CacheService implements SingletonInterface
27
{
28
    use ExtendedFacadeInstanceTrait;
29
30
    const CACHE_IDENTIFIER = 'cache_formz';
31
    const GENERATED_FILES_PATH = 'typo3temp/Formz/';
32
33
    /**
34
     * @var TypoScriptService
35
     */
36
    protected $typoScriptService;
37
38
    /**
39
     * @var FrontendInterface
40
     */
41
    protected $cacheInstance;
42
43
    /**
44
     * Returns the type of backend cache defined in TypoScript at the path:
45
     * `settings.defaultBackendCache`.
46
     *
47
     * @return string
48
     * @throws \Exception
49
     */
50
    public function getBackendCache()
51
    {
52
        $backendCache = $this->typoScriptService->getExtensionConfigurationFromPath('settings.defaultBackendCache');
53
54
        if (false === class_exists($backendCache)) {
55
            throw new ClassNotFoundException(
56
                'The cache class name given in configuration "config.tx_formz.settings.defaultBackendCache" was not found (current value: "' . (string)$backendCache . '")',
57
                1488475103
58
            );
59
        }
60
61
        if (false === in_array(BackendInterface::class, class_implements($backendCache))) {
62
            throw new \Exception(
63
                'The cache class name given in configuration "config.tx_formz.settings.defaultBackendCache" must inherit "' . AbstractBackend::class . '" (current value: "' . (string)$backendCache . '")',
64
                1459251263
65
            );
66
        }
67
68
        return $backendCache;
69
    }
70
71
    /**
72
     * Returns the cache instance for this extension.
73
     *
74
     * @return FrontendInterface
75
     */
76
    public function getCacheInstance()
77
    {
78
        if (null === $this->cacheInstance) {
79
            /** @var $cacheManager CacheManager */
80
            $cacheManager = Core::instantiate(CacheManager::class);
81
82
            if ($cacheManager->hasCache(self::CACHE_IDENTIFIER)) {
83
                $this->cacheInstance = $cacheManager->getCache(self::CACHE_IDENTIFIER);
84
            }
85
        }
86
87
        return $this->cacheInstance;
88
    }
89
90
    /**
91
     * Generic cache identifier creation for usages in the extension.
92
     *
93
     * @param string $string
94
     * @param string $formClassName
95
     * @param int    $maxLength
96
     * @return string
97
     */
98
    public function getCacheIdentifier($string, $formClassName, $maxLength = 55)
99
    {
100
        $explodedClassName = explode('\\', $formClassName);
101
102
        $identifier = strtolower(
103
            $string .
104
            end($explodedClassName) .
105
            '-' .
106
            sha1($formClassName)
107
        );
108
109
        return substr($identifier, 0, $maxLength);
110
    }
111
112
    /**
113
     * @param TypoScriptService $typoScriptService
114
     */
115
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
116
    {
117
        $this->typoScriptService = $typoScriptService;
118
    }
119
120
    /**
121
     * Function called when clearing TYPO3 caches. It will remove the temporary
122
     * asset files created by Formz.
123
     *
124
     * @param array $parameters
125
     */
126
    public function clearCacheCommand($parameters)
127
    {
128
        if (false === in_array($parameters['cacheCmd'], ['all', 'system'])) {
129
            return;
130
        }
131
132
        $files = glob(GeneralUtility::getFileAbsFileName(self::GENERATED_FILES_PATH . '*'));
133
134
        if (false === $files) {
135
            return;
136
        }
137
138
        foreach ($files as $assetCacheFile) {
139
            unlink($assetCacheFile);
140
        }
141
    }
142
}
143