Completed
Push — master ( 10fc3d...cbc6e7 )
by Richard
14s
created

FileStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
ccs 19
cts 20
cp 0.95
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A systemSecret() 0 7 1
A save() 0 8 3
A fileName() 0 4 1
A delete() 0 3 1
A fetch() 0 3 1
A exists() 0 3 1
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf\Key;
13
14
/**
15
 * Xmf\Key\StorageInterface
16
 *
17
 * load a database table
18
 *
19
 * @category  Xmf\Key\FileStorage
20
 * @package   Xmf
21
 * @author    Richard Griffith <[email protected]>
22
 * @copyright 2016 XOOPS Project (http://xoops.org)
23
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24
 * @link      http://xoops.org
25
 */
26
class FileStorage implements StorageInterface
27
{
28
29
    /**
30
     * Fetch key data by name
31
     *
32
     * @param string $name key name
33
     *
34
     * @return string file name
35
     */
36 7
    protected function fileName($name)
37
    {
38 7
        $syssec = $this->systemSecret();
39 7
        return XOOPS_VAR_PATH . "/data/{$syssec}-key-{$name}.php";
40
    }
41
42
    /**
43
     * Construct a string related to the system to make name less predictable
44
     *
45
     * @return string
46
     */
47 7
    protected function systemSecret()
48
    {
49 7
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
50 7
        $prefix = $db->prefix();
0 ignored issues
show
Deprecated Code introduced by
The function XoopsDatabase::prefix() has been deprecated: since version 2.6.0 - alpha 3. Switch to doctrine connector. ( Ignorable by Annotation )

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

50
        $prefix = /** @scrutinizer ignore-deprecated */ $db->prefix();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
51 7
        $secret = md5($prefix);
52 7
        $secret = substr($secret, 8, 8);
53 7
        return $secret;
54
    }
55
56
    /**
57
     * Save key data by name
58
     *
59
     * @param string $name key name
60
     * @param string $data key data, serialized to string if required
61
     *
62
     * @return boolean true if key saved, otherwise false
63
     */
64 7
    public function save($name, $data)
65
    {
66 7
        if (empty($data) || !is_string($data)) {
67
            throw new \DomainException('Invalid key data');
68
        }
69
        $fileContents = "<?php\n//**Warning** modifying this file will break things!\n"
70 7
            . "return '{$data}';\n";
71 7
        return (false !== file_put_contents($this->fileName($name), $fileContents));
72
    }
73
74
    /**
75
     * Fetch key data by name
76
     *
77
     * @param string $name key name
78
     *
79
     * @return string|false key data (possibly serialized) or false on error
80
     */
81 5
    public function fetch($name)
82
    {
83 5
        return include $this->fileName($name);
84
    }
85
86
    /**
87
     * Check if key data exists
88
     *
89
     * @param string $name key name
90
     *
91
     * @return boolean true if key exists, otherwise false
92
     */
93 5
    public function exists($name)
94
    {
95 5
        return file_exists($this->fileName($name));
96
    }
97
98
    /**
99
     * Delete a key
100
     *
101
     * @param string $name key name
102
     *
103
     * @return boolean true if key deleted, otherwise false
104
     */
105 7
    public function delete($name)
106
    {
107 7
        return unlink($this->fileName($name));
108
    }
109
}
110