Passed
Pull Request — master (#431)
by El
03:08
created

AbstractData::upgradePreV1Format()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
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.2.1
11
 */
12
13
namespace PrivateBin\Data;
14
15
use stdClass;
16
17
/**
18
 * AbstractData
19
 *
20
 * Abstract model for PrivateBin data access, implemented as a singleton.
21
 */
22
abstract class AbstractData
23
{
24
    /**
25
     * singleton instance
26
     *
27
     * @access protected
28
     * @static
29
     * @var AbstractData
30
     */
31
    protected static $_instance = null;
32
33
    /**
34
     * enforce singleton, disable constructor
35
     *
36
     * Instantiate using {@link getInstance()}, privatebin is a singleton object.
37
     *
38
     * @access protected
39
     */
40
    protected function __construct()
41
    {
42
    }
43
44
    /**
45
     * enforce singleton, disable cloning
46
     *
47
     * Instantiate using {@link getInstance()}, privatebin is a singleton object.
48
     *
49
     * @access private
50
     */
51
    private function __clone()
52
    {
53
    }
54
55
    /**
56
     * get instance of singleton
57
     *
58
     * @access public
59
     * @static
60
     * @param  array $options
61
     * @return AbstractData
62
     */
63
    public static function getInstance(array $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

63
    public static function getInstance(/** @scrutinizer ignore-unused */ array $options)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
    }
66
67
    /**
68
     * Create a paste.
69
     *
70
     * @access public
71
     * @param  string $pasteid
72
     * @param  array  $paste
73
     * @return bool
74
     */
75
    abstract public function create($pasteid, array $paste);
76
77
    /**
78
     * Read a paste.
79
     *
80
     * @access public
81
     * @param  string $pasteid
82
     * @return stdClass|false
83
     */
84
    abstract public function read($pasteid);
85
86
    /**
87
     * Delete a paste and its discussion.
88
     *
89
     * @access public
90
     * @param  string $pasteid
91
     */
92
    abstract public function delete($pasteid);
93
94
    /**
95
     * Test if a paste exists.
96
     *
97
     * @access public
98
     * @param  string $pasteid
99
     * @return bool
100
     */
101
    abstract public function exists($pasteid);
102
103
    /**
104
     * Create a comment in a paste.
105
     *
106
     * @access public
107
     * @param  string $pasteid
108
     * @param  string $parentid
109
     * @param  string $commentid
110
     * @param  array  $comment
111
     * @return bool
112
     */
113
    abstract public function createComment($pasteid, $parentid, $commentid, array $comment);
114
115
    /**
116
     * Read all comments of paste.
117
     *
118
     * @access public
119
     * @param  string $pasteid
120
     * @return array
121
     */
122
    abstract public function readComments($pasteid);
123
124
    /**
125
     * Test if a comment exists.
126
     *
127
     * @access public
128
     * @param  string $pasteid
129
     * @param  string $parentid
130
     * @param  string $commentid
131
     * @return bool
132
     */
133
    abstract public function existsComment($pasteid, $parentid, $commentid);
134
135
    /**
136
     * Returns up to batch size number of paste ids that have expired
137
     *
138
     * @access protected
139
     * @param  int $batchsize
140
     * @return array
141
     */
142
    abstract protected function _getExpiredPastes($batchsize);
143
144
    /**
145
     * Perform a purge of old pastes, at most the given batchsize is deleted.
146
     *
147
     * @access public
148
     * @param  int $batchsize
149
     */
150
    public function purge($batchsize)
151
    {
152
        if ($batchsize < 1) {
153
            return;
154
        }
155
        $pastes = $this->_getExpiredPastes($batchsize);
156
        if (count($pastes)) {
157
            foreach ($pastes as $pasteid) {
158
                $this->delete($pasteid);
159
            }
160
        }
161
    }
162
163
    /**
164
     * Get next free slot for comment from postdate.
165
     *
166
     * @access protected
167
     * @param  array $comments
168
     * @param  int|string $postdate
169
     * @return int|string
170
     */
171
    protected function getOpenSlot(array &$comments, $postdate)
172
    {
173
        if (array_key_exists($postdate, $comments)) {
174
            $parts = explode('.', $postdate, 2);
175
            if (!array_key_exists(1, $parts)) {
176
                $parts[1] = 0;
177
            }
178
            ++$parts[1];
179
            return $this->getOpenSlot($comments, implode('.', $parts));
180
        }
181
        return $postdate;
182
    }
183
184
    /**
185
     * Upgrade pre-version 1 pastes with attachment to version 1 format.
186
     *
187
     * @access protected
188
     * @static
189
     * @param  array $paste
190
     * @return array
191
     */
192
    protected static function upgradePreV1Format(array $paste)
193
    {
194
        if (array_key_exists('attachment', $paste['meta'])) {
195
            $paste['attachment'] = $paste['meta']['attachment'];
196
            unset($paste['meta']['attachment']);
197
            if (array_key_exists('attachmentname', $paste['meta'])) {
198
                $paste['attachmentname'] = $paste['meta']['attachmentname'];
199
                unset($paste['meta']['attachmentname']);
200
            }
201
        }
202
        return $paste;
203
    }
204
}
205