Completed
Push — master ( 58937a...81ac23 )
by El
07:13
created

DataStore::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * PrivateBin
4
 *
5
 * a zero-knowledge paste bin
6
 *
7
 * @link      https://github.com/PrivateBin/PrivateBin
8
 * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
9
 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
10
 * @version   1.1
11
 */
12
13
namespace PrivateBin\Persistence;
14
15
use Exception;
16
use PrivateBin\Json;
17
18
/**
19
 * DataStore
20
 *
21
 * Handles data storage for Data\Filesystem.
22
 */
23
class DataStore extends AbstractPersistence
24
{
25
    /**
26
     * First line in JSON files, to hide contents
27
     *
28
     * @const string
29
     */
30
    const PROTECTION_LINE = '<?php http_response_code(403); /*';
31
32
    /**
33
     * store the data
34
     *
35
     * @access public
36
     * @static
37
     * @param  string $filename
38
     * @param  array  $data
39
     * @return bool
40
     */
41 45
    public static function store($filename, $data)
42
    {
43 45
        $path = self::getPath();
44 45
        if (strpos($filename, $path) === 0) {
45 45
            $filename = substr($filename, strlen($path));
46
        }
47
        try {
48 45
            self::_store($filename, self::PROTECTION_LINE . PHP_EOL . Json::encode($data));
49 44
            return true;
50 2
        } catch (Exception $e) {
51 2
            return false;
52
        }
53
    }
54
55
    /**
56
     * get the data
57
     *
58
     * @access public
59
     * @static
60
     * @param  string $filename
61
     * @return array  $data
62
     */
63 33
    public static function get($filename)
64
    {
65 33
        return json_decode(substr(file_get_contents($filename), strlen(self::PROTECTION_LINE . PHP_EOL)));
66
    }
67
}
68