CacheRegistry   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 8
c 7
b 1
f 0
lcom 1
cbo 4
dl 0
loc 85
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getNonChangeableCaches() 0 4 1
A add() 0 6 1
A remove() 0 9 2
A getCurrent() 0 5 2
A getFileName() 0 4 1
1
<?php
2
/**
3
 * Class description
4
 *
5
 * @package CacheCheck\Service
6
 * @author  Julian Seitz
7
 */
8
9
namespace HDNET\CacheCheck\Service;
10
11
use TYPO3\CMS\Core\Cache\CacheManager;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Extbase\Reflection\ClassReflection;
14
15
/**
16
 * Class CacheRegistry
17
 */
18
class CacheRegistry extends AbstractService
19
{
20
21
    /**
22
     * Register cache file
23
     */
24
    const FILE_NAME = 'typo3temp/cache_check.txt';
25
26
    /**
27
     * Not changeable caches
28
     *
29
     * @var array
30
     */
31
    protected $nonChangeableCaches = [];
32
33
    /**
34
     * Collect non changeable caches
35
     */
36
    public function __construct()
37
    {
38
        $manager = GeneralUtility::makeInstance(CacheManager::class);
39
40
        $classReflection = new ClassReflection(get_class($manager));
41
        $this->nonChangeableCaches = array_keys($classReflection->getProperty('caches')
42
            ->getValue($manager));
43
    }
44
45
    /**
46
     * Get the not changeable caches
47
     *
48
     * @return array
49
     */
50
    public function getNonChangeableCaches()
51
    {
52
        return $this->nonChangeableCaches;
53
    }
54
55
    /**
56
     * Add the given cache to the registry
57
     *
58
     * @param string $cacheName
59
     */
60
    public function add($cacheName)
61
    {
62
        $entries = $this->getCurrent();
63
        $entries[] = $cacheName;
64
        GeneralUtility::writeFile($this->getFileName(), serialize($entries));
65
    }
66
67
    /**
68
     * Remove the cache with the given key
69
     *
70
     * @param string $cacheName
71
     */
72
    public function remove($cacheName)
73
    {
74
        $entries = $this->getCurrent();
75
        $key = array_search($cacheName, $entries);
76
        if ($key !== false) {
77
            unset($entries[$key]);
78
            GeneralUtility::writeFile($this->getFileName(), serialize($entries));
79
        }
80
    }
81
82
    /**
83
     * Get the current active caches
84
     *
85
     * @return array
86
     */
87
    public function getCurrent()
88
    {
89
        $activeCaches = unserialize(GeneralUtility::getUrl($this->getFileName()));
90
        return !is_array($activeCaches) ? [] : $activeCaches;
91
    }
92
93
    /**
94
     * returns absolute file name
95
     *
96
     * @return string
97
     */
98
    protected function getFileName()
99
    {
100
        return GeneralUtility::getFileAbsFileName(self::FILE_NAME);
101
    }
102
}
103