CacheRepository::findByName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * Cache Repository
4
 *
5
 * @package CacheCheck\Domain\Repository
6
 * @author  Tim Lochmüller
7
 */
8
9
namespace HDNET\CacheCheck\Domain\Repository;
10
11
use HDNET\CacheCheck\Domain\Model\Cache;
12
use HDNET\CacheCheck\Service\SortService;
13
use TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend;
14
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
15
16
/**
17
 * Cache Repository
18
 *
19
 * @author Tim Lochmüller
20
 */
21
class CacheRepository
22
{
23
24
    /**
25
     * Find all caches
26
     *
27
     * @return array
28
     */
29
    public function findAll()
30
    {
31
        $sortService = new SortService();
32
        $caches = [];
33
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] as $name => $configuration) {
34
            $caches[] = $this->mapCacheConfigurationIntoModel($name, $configuration);
35
        }
36
        return $sortService->sortArray($caches);
37
    }
38
39
    /**
40
     * Find the cache with the given name
41
     *
42
     * @param string $cacheName
43
     *
44
     * @return Cache|NULL
45
     */
46
    public function findByName($cacheName)
47
    {
48
        if (!isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheName])) {
49
            return null;
50
        }
51
        return $this->mapCacheConfigurationIntoModel($cacheName, $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheName]);
52
    }
53
54
    /**
55
     * Map the given configuration into a cache model
56
     *
57
     * @param string $cacheName
58
     * @param array  $configuration
59
     *
60
     * @return Cache
61
     */
62
    protected function mapCacheConfigurationIntoModel($cacheName, $configuration)
63
    {
64
        $cache = new Cache();
65
        $cache->setName($cacheName);
66
        $cache->setFrontend(isset($configuration['frontend']) && class_exists($configuration['frontend']) ? $configuration['frontend'] : VariableFrontend::class);
67
        $cache->setBackend(isset($configuration['backend']) && class_exists($configuration['backend']) ? $configuration['backend'] : Typo3DatabaseBackend::class);
68
        $cache->setOriginalBackend(isset($configuration['originalBackend']) && class_exists($configuration['originalBackend']) ? $configuration['originalBackend'] : '');
69
        $cache->setOptions(isset($configuration['options']) ? $configuration['options'] : []);
70
        $cache->setGroups(isset($configuration['groups']) ? $configuration['groups'] : ['all']);
71
        return $cache;
72
    }
73
}
74