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

FileCachePathBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

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

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
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