Passed
Push — devel-3.0 ( 4622e9...e92637 )
by Rubén
03:18
created

FileCachePacked   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 163
rs 10
c 0
b 0
f 0
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 3
A set() 0 7 2
A save() 0 9 2
A load() 0 15 3
A delete() 0 6 1
A isExpiredDate() 0 6 1
A createPath() 0 4 3
A isExpired() 0 6 1
A saveData() 0 13 2
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Storage\File;
26
27
use RuntimeException;
28
29
/**
30
 * Class FileCachePacked
31
 *
32
 * @package SP\Storage\File;
33
 */
34
final class FileCachePacked implements FileStorageInterface
35
{
36
    /**
37
     * @var array
38
     */
39
    protected $data;
40
    /**
41
     * @var bool
42
     */
43
    protected $loaded = false;
44
45
    /**
46
     * @param string $path
47
     *
48
     * @return mixed
49
     * @throws \RuntimeException
50
     * @throws FileException
51
     */
52
    public function load($path)
53
    {
54
        $file = new FileHandler($path);
55
56
        if (!($data = gzuncompress($file->checkIsReadable()->readToString()))) {
57
            throw new FileException(sprintf(__('Error al descomprimir datos del archivo (%s)'), $path));
58
        }
59
60
        if (($this->data = unserialize($data)) === false) {
61
            throw new FileException(__('Error al obtener los datos'));
62
        }
63
64
        $this->loaded = true;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $path
71
     * @param mixed  $data
72
     *
73
     * @return FileStorageInterface
74
     * @throws FileException
75
     */
76
    public function save($path, $data = null)
77
    {
78
        if ($data === null) {
79
            $this->saveData($path, $this->data);
80
        } else {
81
            $this->saveData($path, $data);
82
        }
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param $path
89
     * @param $data
90
     *
91
     * @throws FileException
92
     */
93
    protected function saveData($path, $data)
94
    {
95
96
        $this->createPath(dirname($path));
97
98
        if (!($data = gzcompress(serialize($data)))) {
99
            throw new FileException(sprintf(__('Error al comprimir datos del archivo (%s)'), $path));
100
        }
101
102
        $file = new FileHandler($path);
103
        $file->checkIsWritable()
104
            ->write(gzcompress(serialize($data)))
105
            ->close();
106
    }
107
108
    /**
109
     * @param $path
110
     *
111
     * @throws FileException
112
     */
113
    public function createPath($path)
114
    {
115
        if (!is_dir($path) && mkdir($path, 0700, true) === false) {
116
            throw new FileException(sprintf(__('No es posible crear el directorio (%s)'), $path));
117
        }
118
    }
119
120
    /**
121
     * @param string $path
122
     *
123
     * @return FileStorageInterface
124
     * @throws FileException
125
     */
126
    public function delete($path)
127
    {
128
        $file = new FileHandler($path);
129
        $file->delete();
130
131
        return $this;
132
    }
133
134
    /**
135
     * Gets key data from cache
136
     *
137
     * @param $key
138
     *
139
     * @return mixed
140
     */
141
    public function get($key)
142
    {
143
        if (!$this->loaded) {
144
            throw new RuntimeException((__('Datos no cargados')));
145
        }
146
147
        return isset($this->data[$key]) ? $this->data[$key] : null;
148
    }
149
150
    /**
151
     * Sets key data into cache
152
     *
153
     * @param $key
154
     * @param $data
155
     */
156
    public function set($key, $data)
157
    {
158
        if (!$this->loaded) {
159
            $this->data = [];
160
        }
161
162
        $this->data[$key] = ['time' => time(), 'data' => serialize($data)];
163
    }
164
165
    /**
166
     * Returns whether the file is expired
167
     *
168
     * @param string $path
169
     * @param int    $time
170
     *
171
     * @return bool
172
     * @throws FileException
173
     */
174
    public function isExpired($path, $time = 86400): bool
175
    {
176
        $file = new FileHandler($path);
177
        $file->checkFileExists();
178
179
        return time() > $file->getFileTime() + $time;
180
    }
181
182
    /**
183
     * Returns if the file is expired adding time to modification date
184
     *
185
     * @param string $path
186
     * @param int    $date
187
     *
188
     * @return bool
189
     * @throws FileException
190
     */
191
    public function isExpiredDate($path, $date): bool
192
    {
193
        $file = new FileHandler($path);
194
        $file->checkFileExists();
195
196
        return (int)$date > $file->getFileTime();
197
    }
198
}