FileCachePathBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 8
c 1
b 0
f 0
dl 0
loc 39
rs 10

3 Methods

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