Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

FileStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
c 1
b 1
f 0
lcom 1
cbo 2
dl 0
loc 84
rs 10
ccs 10
cts 20
cp 0.5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fileName() 0 5 1
A systemSecret() 0 8 1
A save() 0 9 3
A fetch() 0 4 1
A exists() 0 4 1
A delete() 0 4 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
 * @version   Release: 1.0
25
 * @link      http://xoops.org
26
 */
27
class FileStorage implements StorageInterface
28
{
29
30
    /**
31
     * Fetch key data by name
32
     *
33
     * @param string $name key name
34
     *
35
     * @return string file name
36
     */
37
    protected function fileName($name)
38
    {
39
        $syssec = $this->systemSecret();
40
        return XOOPS_VAR_PATH . "/data/{$syssec}-key-{$name}.php";
41
    }
42
43
    /**
44
     * Construct a string related to the system to make name less predictable
45
     *
46
     * @return string
47
     */
48
    protected function systemSecret()
49
    {
50
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
51
        $prefix = $db->prefix();
0 ignored issues
show
Deprecated Code introduced by
The method XoopsDatabase::prefix() has been deprecated with message: since version 2.6.0 - alpha 3. Switch to doctrine connector.

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

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

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