FlushHelper::pathFor()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 2
b 0
f 1
nc 4
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Helpers;
4
5
use Silviooosilva\CacheerPhp\Enums\CacheStoreType;
6
7
/**
8
 * Class FlushHelper
9
 * 
10
 * @author Sílvio Silva <https://github.com/silviooosilva>
11
 * @package Silviooosilva\CacheerPhp
12
 * 
13
 * Builds deterministic file paths for last-flush timestamps per store type.
14
 */
15
class FlushHelper
16
{
17
    /**
18
     * Returns a path to store a last-flush timestamp.
19
     * @param CacheStoreType|string $storeType e.g., 'redis' or 'db'
20
     * @param string $identifier e.g., namespace or table name
21
     * @return string
22
     */
23
    public static function pathFor(CacheStoreType|string $storeType, string $identifier): string
24
    {
25
        $store = $storeType instanceof CacheStoreType ? $storeType->value : $storeType;
26
        $root = EnvHelper::getRootPath();
27
        $dir = $root . DIRECTORY_SEPARATOR . 'CacheerPHP' . DIRECTORY_SEPARATOR . 'Flush';
28
        if (!is_dir($dir)) {
29
            @mkdir($dir, 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

29
            /** @scrutinizer ignore-unhandled */ @mkdir($dir, 0755, true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
30
        }
31
        $safeId = preg_replace('/[^a-zA-Z0-9_-]+/', '_', $identifier);
32
        return $dir . DIRECTORY_SEPARATOR . $store . '_' . $safeId . '.time';
33
    }
34
}
35