Completed
Push — cleanup-service ( 407da3...1eee72 )
by Romain
02:14
created

CacheService::getBackendCache()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
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\Service\Traits\ExtendedFacadeInstanceTrait;
18
use TYPO3\CMS\Core\Cache\Backend\AbstractBackend;
19
use TYPO3\CMS\Core\Cache\CacheManager;
20
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
21
use TYPO3\CMS\Core\SingletonInterface;
22
23
class CacheService implements SingletonInterface
24
{
25
    use ExtendedFacadeInstanceTrait;
26
27
    const CACHE_IDENTIFIER = 'cache_formz';
28
29
    /**
30
     * @var TypoScriptService
31
     */
32
    protected $typoScriptService;
33
34
    /**
35
     * @var FrontendInterface
36
     */
37
    protected $cacheInstance;
38
39
    /**
40
     * Returns the type of backend cache defined in TypoScript at the path:
41
     * `settings.defaultBackendCache`.
42
     *
43
     * @return string
44
     * @throws \Exception
45
     */
46
    public function getBackendCache()
47
    {
48
        $backendCache = $this->typoScriptService->getExtensionConfigurationFromPath('settings.defaultBackendCache');
49
50
        if (false === class_exists($backendCache)
51
            && false === in_array(AbstractBackend::class, class_parents($backendCache))
52
        ) {
53
            throw new \Exception(
54
                'The cache class name given in configuration "config.tx_formz.settings.defaultBackendCache" must inherit "' . AbstractBackend::class . '" (current value: "' . (string)$backendCache . '")',
55
                1459251263
56
            );
57
        }
58
59
        return $backendCache;
60
    }
61
62
    /**
63
     * Returns the cache instance for this extension.
64
     *
65
     * @return FrontendInterface
66
     */
67
    public function getCacheInstance()
68
    {
69
        if (null === $this->cacheInstance) {
70
            /** @var $cacheManager CacheManager */
71
            $cacheManager = Core::instantiate(CacheManager::class);
72
73
            if ($cacheManager->hasCache(self::CACHE_IDENTIFIER)) {
74
                $this->cacheInstance = $cacheManager->getCache(self::CACHE_IDENTIFIER);
75
            }
76
        }
77
78
        return $this->cacheInstance;
79
    }
80
81
    /**
82
     * @param FrontendInterface $cacheInstance
83
     */
84
    public function setCacheInstance(FrontendInterface $cacheInstance)
85
    {
86
        $this->cacheInstance = $cacheInstance;
87
    }
88
89
    /**
90
     * Generic cache identifier creation for usages in the extension.
91
     *
92
     * @param string $string
93
     * @param string $formClassName
94
     * @param int    $maxLength
95
     * @return string
96
     */
97
    public function getCacheIdentifier($string, $formClassName, $maxLength = 55)
98
    {
99
        $explodedClassName = explode('\\', $formClassName);
100
101
        $identifier = strtolower(
102
            $string .
103
            end($explodedClassName) .
104
            '-' .
105
            sha1($formClassName)
106
        );
107
108
        return substr($identifier, 0, $maxLength);
109
    }
110
111
    /**
112
     * @param TypoScriptService $typoScriptService
113
     */
114
    public function injectTypoScriptService(TypoScriptService $typoScriptService)
115
    {
116
        $this->typoScriptService = $typoScriptService;
117
    }
118
}
119