SimpleFileBackend::countEntries()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Stats service for simple files
4
 *
5
 * @package CacheCheck\Service\Statistics
6
 * @author  Tim Lochmüller
7
 */
8
9
namespace HDNET\CacheCheck\Service\Statistics;
10
11
use HDNET\CacheCheck\Domain\Model\Cache;
12
use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
15
/**
16
 * Stats service for simple files
17
 *
18
 * @author Tim Lochmüller
19
 */
20
class SimpleFileBackend implements StatisticsInterface
21
{
22
23
    /**
24
     * Get the number of entries
25
     *
26
     * @param Cache $cache
27
     *
28
     * @return int
29
     */
30
    public function countEntries(Cache $cache)
31
    {
32
        return sizeof(glob(GeneralUtility::getFileAbsFileName($this->getCacheDirectory($cache) . '*')));
33
    }
34
35
    /**
36
     * Get the size of the cache in byte
37
     *
38
     * @param Cache $cache
39
     *
40
     * @return float
41
     */
42
    public function getSize(Cache $cache)
43
    {
44
        if ($this->countEntries($cache) === 0) {
45
            return 0.0;
46
        }
47
48
        return $this->getFolderSize(GeneralUtility::getFileAbsFileName($this->getCacheDirectory($cache)));
49
    }
50
51
    /**
52
     * Get the number of tags
53
     *
54
     * @param Cache $cache
55
     *
56
     * @return int
57
     */
58
    public function countTags(Cache $cache)
59
    {
60
        return 0;
61
    }
62
63
    /**
64
     * Get the foldersize
65
     *
66
     * @param string $path
67
     *
68
     * @return int
69
     */
70
    protected function getFolderSize($path)
71
    {
72
        $totalSize = 0;
73
        $files = scandir($path);
74
        foreach ($files as $t) {
75
            if ($t !== '.' && $t !== '..') {
76
                $currentFile = $path . $t;
77
                if (is_dir($currentFile)) {
78
                    $totalSize += $this->getFolderSize($currentFile);
79
                } else {
80
                    $totalSize += (int)filesize($currentFile);
81
                }
82
            }
83
        }
84
85
        return $totalSize;
86
    }
87
88
    /**
89
     * Get the cache directory
90
     *
91
     * @param Cache $cache
92
     *
93
     * @return string
94
     */
95
    protected function getCacheDirectory(Cache $cache)
96
    {
97
        $codeOrData = $cache->getFrontend() === PhpFrontend::class ? 'Code' : 'Data';
98
        return 'typo3temp/Cache/' . $codeOrData . '/' . $cache->getName() . '/';
99
    }
100
101
    /**
102
     * Returns the age of found entries in seconds
103
     *
104
     * @param Cache $cache
105
     *
106
     * @return int|null
107
     */
108
    public function getAge(Cache $cache)
109
    {
110
        $cacheFiles = glob(GeneralUtility::getFileAbsFileName($this->getCacheDirectory($cache) . '*'));
111
        if (!$cacheFiles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cacheFiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
112
            return null;
113
        }
114
        foreach ($cacheFiles as $key => $cacheFile) {
115
            $cacheFiles[$key] = time() - filectime($cacheFile);
116
        }
117
        return intval(array_sum($cacheFiles) / count($cacheFiles));
118
    }
119
120
    /**
121
     * Returns the left lifetime of the cache entry
122
     *
123
     * @param Cache $cache
124
     *
125
     * @return int|null
126
     */
127
    public function getExpires(Cache $cache)
128
    {
129
        return null;
130
    }
131
}
132