Completed
Push — unit-test-services ( a7c404...337105 )
by Romain
02:24
created

CacheService::getFormCacheIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 3
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\Exceptions\ClassNotFoundException;
17
use Romm\Formz\Exceptions\InvalidOptionValueException;
18
use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait;
19
use TYPO3\CMS\Core\Cache\Backend\BackendInterface;
20
use TYPO3\CMS\Core\Cache\CacheManager;
21
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
22
use TYPO3\CMS\Core\SingletonInterface;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
class CacheService implements SingletonInterface
26
{
27
    use ExtendedFacadeInstanceTrait;
28
29
    const CACHE_IDENTIFIER = 'cache_formz';
30
    const CONFIGURATION_OBJECT_CACHE_IDENTIFIER = 'cache_formz_configuration_object';
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
     * @var CacheManager
45
     */
46
    protected $cacheManager;
47
48
    /**
49
     * Returns the type of backend cache defined in TypoScript at the path:
50
     * `settings.defaultBackendCache`.
51
     *
52
     * @return string
53
     * @throws ClassNotFoundException
54
     * @throws InvalidOptionValueException
55
     */
56
    public function getBackendCache()
57
    {
58
        $backendCache = $this->typoScriptService->getExtensionConfigurationFromPath('settings.defaultBackendCache');
59
60
        if (false === class_exists($backendCache)) {
61
            throw ClassNotFoundException::backendCacheClassNameNotFound($backendCache);
62
        }
63
64
        if (false === in_array(BackendInterface::class, class_implements($backendCache))) {
65
            throw InvalidOptionValueException::wrongBackendCacheType($backendCache);
66
        }
67
68
        return $backendCache;
69
    }
70
71
    /**
72
     * Returns the cache instance used by this extension.
73
     *
74
     * @return FrontendInterface
75
     */
76
    public function getCacheInstance()
77
    {
78
        if (null === $this->cacheInstance) {
79
            $this->cacheInstance = $this->cacheManager->getCache(self::CACHE_IDENTIFIER);
80
        }
81
82
        return $this->cacheInstance;
83
    }
84
85
    /**
86
     * Generic cache identifier creation for usages in the extension.
87
     *
88
     * @param string $string
89
     * @param string $formClassName
90
     * @param int    $maxLength
91
     * @return string
92
     */
93
    public function getFormCacheIdentifier($string, $formClassName, $maxLength = 55)
94
    {
95
        $shortClassName = end(explode('\\', $formClassName));
0 ignored issues
show
Bug introduced by
explode('\\', $formClassName) cannot be passed to end() as the parameter $array expects a reference.
Loading history...
96
97
        $identifier = strtolower(
98
            $string .
99
            $shortClassName .
100
            '-' .
101
            sha1($formClassName)
102
        );
103
104
        return substr($identifier, 0, $maxLength);
105
    }
106
107
    /**
108
     * Function called when clearing TYPO3 caches. It will remove the temporary
109
     * asset files created by FormZ.
110
     *
111
     * @param array $parameters
112
     */
113
    public function clearCacheCommand($parameters)
114
    {
115
        if (in_array($parameters['cacheCmd'], ['all', 'system'])) {
116
            $files = $this->getFilesInPath(self::GENERATED_FILES_PATH . '*');
117
118
            foreach ($files as $file) {
119
                $this->clearFile($file);
120
            }
121
        }
122
    }
123
124
    /**
125
     * @param string $path
126
     * @return array
127
     */
128
    protected function getFilesInPath($path)
129
    {
130
        $files = glob(GeneralUtility::getFileAbsFileName($path));
131
132
        return (false === $files)
133
            ? []
134
            : $files;
135
    }
136
137
    /**
138
     * @param string $file
139
     */
140
    protected function clearFile($file)
141
    {
142
        touch($file, 0);
143
    }
144
145
    /**
146
     * @param CacheManager $cacheManager
147
     */
148
    public function injectCacheManager(CacheManager $cacheManager)
149
    {
150
        $this->cacheManager = $cacheManager;
151
    }
152
153
    /**
154
     * @param TypoScriptService $typoScriptService
155
     */
156
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
157
    {
158
        $this->typoScriptService = $typoScriptService;
159
    }
160
}
161