FileCachePathBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\CacheStore\Support;
4
5
use Silviooosilva\CacheerPhp\CacheStore\CacheManager\FileCacheManager;
6
use Silviooosilva\CacheerPhp\Exceptions\CacheFileException;
7
8
/**
9
 * Class FileCachePathBuilder
10
 * @author Sílvio Silva <https://github.com/silviooosilva>
11
 * @package Silviooosilva\CacheerPhp
12
 */
13
class FileCachePathBuilder
14
{
15
    /**
16
     * FileCachePathBuilder constructor.
17
     *
18
     * @param FileCacheManager $fileManager
19
     * @param string $baseDir
20
     */
21
    public function __construct(private FileCacheManager $fileManager, private string $baseDir)
22
    {
23
    }
24
25
    /**
26
     * Builds the full path for a cache item based on its key and namespace.
27
     *
28
     * @param string $cacheKey
29
     * @param string $namespace
30
     * @return string
31
     * @throws CacheFileException
32
     */
33
    public function build(string $cacheKey, string $namespace = ''): string
34
    {
35
        $dir = $this->namespaceDir($namespace);
36
        if (!empty($namespace)) {
37
            $this->fileManager->createDirectory($dir);
38
        }
39
        return $dir . md5($cacheKey) . '.cache';
40
    }
41
42
    /**
43
     * Builds the directory path for a given namespace.
44
     *
45
     * @param string $namespace
46
     * @return string
47
     */
48
    public function namespaceDir(string $namespace = ''): string
49
    {
50
        $namespace = $namespace ? md5($namespace) . '/' : '';
51
        $cacheDir = rtrim($this->baseDir, '/') . '/';
52
        return $cacheDir . $namespace;
53
    }
54
}
55