Test Failed
Push — feat/cache-warming ( 037623...493f4d )
by Chema
04:16
created

CacheManager::cacheFileExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Application\CacheWarm;
6
7
use Gacela\Framework\ClassResolver\Cache\ClassNamePhpCache;
8
use Gacela\Framework\Config\Config;
9
10
use function file_exists;
11
use function sprintf;
12
13
final class CacheManager
14
{
15
    public function clearCache(): void
16
    {
17
        $cacheFile = $this->getCacheFilePath();
18
19
        if (file_exists($cacheFile)) {
20
            unlink($cacheFile);
21
        }
22
    }
23
24
    public function getCacheFilePath(): string
25
    {
26
        $cacheDir = Config::getInstance()->getCacheDir();
27
        return $cacheDir . DIRECTORY_SEPARATOR . ClassNamePhpCache::FILENAME;
28
    }
29
30
    public function cacheFileExists(): bool
31
    {
32
        return file_exists($this->getCacheFilePath());
33
    }
34
35
    public function getCacheFileSize(): int
36
    {
37
        $cacheFile = $this->getCacheFilePath();
38
39
        if (!file_exists($cacheFile)) {
40
            return 0;
41
        }
42
43
        return (int) filesize($cacheFile);
44
    }
45
46
    public function getFormattedCacheFileSize(): string
47
    {
48
        $bytes = $this->getCacheFileSize();
49
50
        if ($bytes < 1024) {
51
            return sprintf('%d B', $bytes);
52
        }
53
54
        if ($bytes < 1048576) {
55
            return sprintf('%.2f KB', $bytes / 1024);
56
        }
57
58
        return sprintf('%.2f MB', $bytes / 1048576);
59
    }
60
}
61