Passed
Pull Request — main (#48)
by Sílvio
03:09
created

FileCachePathBuilder::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\CacheStore\Support;
4
5
use Silviooosilva\CacheerPhp\CacheStore\CacheManager\FileCacheManager;
6
7
class FileCachePathBuilder
8
{
9
    public function __construct(private FileCacheManager $fileManager, private string $baseDir)
10
    {
11
    }
12
13
    public function build(string $cacheKey, string $namespace = ''): string
14
    {
15
        $dir = $this->namespaceDir($namespace);
16
        if (!empty($namespace)) {
17
            $this->fileManager->createDirectory($dir);
18
        }
19
        return $dir . md5($cacheKey) . '.cache';
20
    }
21
22
    public function namespaceDir(string $namespace = ''): string
23
    {
24
        $namespace = $namespace ? md5($namespace) . '/' : '';
25
        $cacheDir = rtrim($this->baseDir, '/') . '/';
26
        return $cacheDir . $namespace;
27
    }
28
}
29