Completed
Pull Request — master (#3)
by
unknown
05:51 queued 01:31
created

ConfigurationServicesUtility   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 8 2
A addCacheServiceToServiceFactory() 0 6 1
A getBackendCache() 0 18 4
1
<?php
2
/*
3
 * 2016 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\Configuration;
15
16
use Romm\ConfigurationObject\Service\Items\Cache\CacheService;
17
use Romm\ConfigurationObject\Service\ServiceFactory;
18
use Romm\ConfigurationObject\Service\ServiceInterface;
19
use Romm\Formz\Core\Core;
20
use TYPO3\CMS\Core\Cache\Backend\AbstractBackend;
21
use TYPO3\CMS\Core\SingletonInterface;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
class ConfigurationServicesUtility implements SingletonInterface
25
{
26
27
    /**
28
     * @var ConfigurationServicesUtility
29
     */
30
    protected static $instance;
31
32
    /**
33
     * @var string
34
     */
35
    protected $backendCache;
36
37
    /**
38
     * Returns an instance of this class.
39
     *
40
     * @return ConfigurationServicesUtility
41
     */
42
    public static function getInstance()
43
    {
44
        if (null === self::$instance) {
45
            self::$instance = GeneralUtility::makeInstance(self::class);
46
        }
47
48
        return self::$instance;
49
    }
50
51
    /**
52
     * Will add the cache service configured with the TypoScript backend cache
53
     * type parameter.
54
     *
55
     * @param ServiceFactory $serviceFactory
56
     */
57
    public function addCacheServiceToServiceFactory(ServiceFactory $serviceFactory)
58
    {
59
        $serviceFactory->attach(ServiceInterface::SERVICE_CACHE)
60
            ->with(ServiceInterface::SERVICE_CACHE)
61
            ->setOption(CacheService::OPTION_CACHE_BACKEND, $this->getBackendCache());
62
    }
63
64
    /**
65
     * Returns the backend cache type configured in TypoScript at the path
66
     * `settings.defaultBackendCache`.
67
     *
68
     * @return string
69
     * @throws \Exception
70
     */
71
    protected function getBackendCache()
72
    {
73
        if (null === $this->backendCache) {
74
            $backendCache = Core::getTypoScriptUtility()
75
                ->getExtensionConfigurationFromPath('settings.defaultBackendCache');
76
77
            if (false === class_exists($backendCache)
78
                && false === in_array(AbstractBackend::class, class_parents($backendCache))
79
            ) {
80
                throw new \Exception(
81
                    'The cache class name given in configuration "config.tx_formz.settings.defaultBackendCache" must inherit "' . AbstractBackend::class . '" (current value: "' . (string)$backendCache . '")',
82
                    1459251263
83
                );
84
            }
85
        }
86
87
        return $this->backendCache;
88
    }
89
}
90