CacheService::clearFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\ExtendedSelfInstantiateTrait;
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 ExtendedSelfInstantiateTrait;
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 $formClassName
89
     * @param string $formName
90
     * @return string
91
     */
92
    public function getFormCacheIdentifier($formClassName, $formName)
93
    {
94
        $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...
95
96
        return StringService::get()->sanitizeString($shortClassName . '-' . $formName);
97
    }
98
99
    /**
100
     * Function called when clearing TYPO3 caches. It will remove the temporary
101
     * asset files created by FormZ.
102
     *
103
     * @param array $parameters
104
     */
105
    public function clearCacheCommand($parameters)
106
    {
107
        if (in_array($parameters['cacheCmd'], ['all', 'system'])) {
108
            $files = $this->getFilesInPath(self::GENERATED_FILES_PATH . '*');
109
110
            foreach ($files as $file) {
111
                $this->clearFile($file);
112
            }
113
        }
114
    }
115
116
    /**
117
     * @param string $path
118
     * @return array
119
     */
120
    protected function getFilesInPath($path)
121
    {
122
        $files = glob(GeneralUtility::getFileAbsFileName($path));
123
124
        return (false === $files)
125
            ? []
126
            : $files;
127
    }
128
129
    /**
130
     * @param string $file
131
     */
132
    protected function clearFile($file)
133
    {
134
        touch($file, 0);
135
    }
136
137
    /**
138
     * @param CacheManager $cacheManager
139
     */
140
    public function injectCacheManager(CacheManager $cacheManager)
141
    {
142
        $this->cacheManager = $cacheManager;
143
    }
144
145
    /**
146
     * @param TypoScriptService $typoScriptService
147
     */
148
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
149
    {
150
        $this->typoScriptService = $typoScriptService;
151
    }
152
}
153