Completed
Branch feature/pre-split (e801ec)
by Anton
03:11
created

FileStore::isAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Cache\Stores;
10
11
use Spiral\Cache\Prototypes\CacheStore;
12
use Spiral\Files\FilesInterface;
13
14
/**
15
 * Serializes data to file. Usually points to runtime directory.
16
 */
17
class FileStore extends CacheStore
18
{
19
    /**
20
     * @invisible
21
     *
22
     * @var FilesInterface
23
     */
24
    protected $files;
25
26
    /**
27
     * @var string
28
     */
29
    private $directory;
30
31
    /**
32
     * @var string
33
     */
34
    private $extension;
35
36
    /**
37
     * @param FilesInterface $files
38
     * @param string         $directory
39
     * @param string         $extension
40
     */
41
    public function __construct(
42
        FilesInterface $files,
43
        string $directory,
44
        string $extension = 'cache'
45
    ) {
46
        $this->directory = $files->normalizePath($directory, true);
47
        $this->extension = $extension;
48
49
        $this->files = $files;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function isAvailable(): bool
56
    {
57
        return true;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 View Code Duplication
    public function has(string $name): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        if (!$this->files->exists($filename = $this->makeFilename($name))) {
66
            return false;
67
        }
68
69
        $cacheData = unserialize($this->files->read($filename));
70
        if (!empty($cacheData[0]) && $cacheData[0] < time()) {
71
            $this->delete($name);
72
73
            //Expired
74
            return false;
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     *
83
     * @param int $expiration Current expiration time value in seconds (reference).
84
     */
85 View Code Duplication
    public function get(string $name, int &$expiration = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        if (!$this->files->exists($filename = $this->makeFilename($name))) {
88
            return null;
89
        }
90
91
        $cacheData = unserialize($this->files->read($filename));
92
        if (!empty($cacheData[0]) && $cacheData[0] < time()) {
93
            $this->delete($name);
94
95
            return null;
96
        }
97
98
        return $cacheData[1];
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function set(string $name, $data, $ttl = null)
105
    {
106
        return $this->files->write(
107
            $this->makeFilename($name),
108
            serialize([$this->lifetime($ttl, 0, time()), $data])
109
        );
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function delete(string $name)
116
    {
117
        $this->files->delete($this->makeFilename($name));
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 View Code Duplication
    public function inc(string $name, int $delta = 1): int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $value = $this->get($name, $expiration) + $delta;
126
127
        if (empty($expiration)) {
128
            $this->set($name, $value);
129
        } else {
130
            $this->set($name, $value, $expiration - time());
131
        }
132
133
        return $value;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 View Code Duplication
    public function dec(string $name, int $delta = 1): int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $value = $this->get($name, $expiration) - $delta;
142
143
        if (empty($expiration)) {
144
            $this->set($name, $value);
145
        } else {
146
            $this->set($name, $value, $expiration - time());
147
        }
148
149
        return $value;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function clear()
156
    {
157
        foreach ($this->files->getFiles($this->directory, $this->extension) as $filename) {
158
            $this->files->delete($filename);
159
        }
160
    }
161
162
    /**
163
     * Create filename using cache name.
164
     *
165
     * @param string $name
166
     *
167
     * @return string Filename.
168
     */
169
    protected function makeFilename(string $name): string
170
    {
171
        return $this->directory . md5($name) . '.' . $this->extension;
172
    }
173
}
174