Passed
Push — main ( 47f44b...c607be )
by Dimitri
12:45
created

FileVarExportHandler::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 5
c 1
b 1
f 0
nc 4
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Cache\Handlers;
13
14
use DateInterval;
15
16
final class FileVarExportHandler extends BaseHandler
17
{
18
    /**
19
     * Dossier de stockage des fichiers mis en cache.
20
     */
21
	private string $path;
22
23
    /**
24
     * Constructeur
25
     */
26
    public function __construct(?string $path = null)
27
    {
28
        if (null === $path) {
29
            $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'blitz-php' . DIRECTORY_SEPARATOR . 'cache';
30
        }
31
32
        $this->path = rtrim($path, '/\\');
33
34
        if (!is_dir($this->path)) {
35
            mkdir($this->path, 0777, true);
36
        }
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function isSupported(): bool
43
    {
44
        return true;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function init(array $config = []): bool
51
    {
52
        return true;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
	 *
58
	 * @param array|bool|float|int|object|string|null $value
59
     */
60
    public function set(string $key, mixed $value, null|DateInterval|int $ttl = null): bool
61
    {
62
        $value = var_export($value, true);
63
64
        // Écrire d'abord dans le fichier temporaire pour assurer l'atomicité
65
        $tmp = $this->path . "/{$key}." . uniqid('', true) . '.tmp';
66
        file_put_contents($tmp, '<?php return ' . $value . ';', LOCK_EX);
67
68
        return rename($tmp, $this->path . "/{$key}");
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function setMultiple(iterable $values, null|DateInterval|int $ttl = null): bool
75
    {
76
        return true;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
	 *
82
	 * @return array|bool|float|int|object|string|null
83
     */
84
    public function get(string $key, mixed $default = null): mixed
85
    {
86
		return @include $this->path . "/{$key}";
87
	}
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function getMultiple(iterable $keys, mixed $default = null): iterable
93
    {
94
        return [];
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    public function increment(string $key, int $offset = 1)
101
    {
102
        return 1;
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function decrement(string $key, int $offset = 1)
109
    {
110
        return 0;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function delete(string $key): bool
117
    {
118
		return @unlink($this->path . "/{$key}");
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function deleteMultiple(iterable $keys): bool
125
    {
126
        return true;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function clear(): bool
133
    {
134
		if (! is_dir($this->path)) {
135
			return false;
136
		}
137
138
		$files = glob($this->path . '/*');
139
		foreach ($files as $file) {
140
			if (is_file($file)) {
141
				unlink($file);
142
			} elseif (is_dir($file)) {
143
				$this->clear($file);
0 ignored issues
show
Unused Code introduced by
The call to BlitzPHP\Cache\Handlers\...rExportHandler::clear() has too many arguments starting with $file. ( Ignorable by Annotation )

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

143
				$this->/** @scrutinizer ignore-call */ 
144
           clear($file);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
144
			}
145
		}
146
147
		return rmdir($this->path);
148
	}
149
150
    /**
151
     * {@inheritDoc}
152
     */
153
    public function clearGroup(string $group): bool
154
    {
155
        return true;
156
    }
157
}
158