Passed
Push — 3.0 ( 5e1ed3...5a5495 )
by Rubén
04:22
created

FileCachePacked::saveData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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
/**
28
 * Class FileCachePacked
29
 *
30
 * @package SP\Storage\File;
31
 */
32
final class FileCachePacked extends FileCacheBase
33
{
34
    /**
35
     * @return mixed
36
     * @throws \RuntimeException
37
     * @throws FileException
38
     */
39
    public function load()
40
    {
41
        $this->path->checkIsReadable();
42
        $dataUnpacked = gzuncompress($this->path->readToString());
43
44
        if ($dataUnpacked === false) {
45
            throw new FileException(sprintf(__('Error while decompressing the file data (%s)'), $this->path->getFile()));
46
        }
47
48
        $data = unserialize($dataUnpacked);
49
50
        if ($data === false) {
51
            throw new FileException(__('Error while retrieving the data'));
52
        }
53
54
        return $data;
55
    }
56
57
    /**
58
     * @param mixed  $data
59
     *
60
     * @return FileCacheInterface
61
     * @throws FileException
62
     */
63
    public function save($data)
64
    {
65
        $this->createPath();
66
67
        $data = gzcompress(serialize($data));
68
69
        if ($data === false) {
70
            throw new FileException(sprintf(__('Error while compressing the file data (%s)'), $this->path->getFile()));
71
        }
72
73
        $this->path->checkIsWritable()
74
            ->write($data)
75
            ->close();
76
77
        return $this;
78
    }
79
}