Backup   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 291
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 35
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 291
rs 9

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
D getList() 0 60 12
B add() 0 28 5
A get() 0 5 1
A delete() 0 18 3
A deleteZip() 0 11 3
A backup() 0 4 1
A restore() 0 4 1
A exists() 0 5 1
A getHandlers() 0 12 2
A getHandler() 0 5 2
A callHandler() 0 9 2
A getDefaultHandlers() 0 12 1
1
<?php
2
3
/**
4
 * @package Backup
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2015, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3
8
 */
9
10
namespace gplcart\modules\backup\models;
11
12
use Exception;
13
use gplcart\core\Config;
14
use gplcart\core\Handler;
15
use gplcart\core\Hook;
16
use gplcart\core\models\Translation;
17
use gplcart\core\models\User;
18
19
/**
20
 * Manages basic behaviors and data related to Backup model
21
 */
22
class Backup
23
{
24
25
    /**
26
     * Database class instance
27
     * @var \gplcart\core\Database $db
28
     */
29
    protected $db;
30
31
    /**
32
     * Hook class instance
33
     * @var \gplcart\core\Hook $hook
34
     */
35
    protected $hook;
36
37
    /**
38
     * Translation UI model instance
39
     * @var \gplcart\core\models\Translation $translation
40
     */
41
    protected $translation;
42
43
    /**
44
     * User model instance
45
     * @var \gplcart\core\models\User $user
46
     */
47
    protected $user;
48
49
    /**
50
     * Backup constructor.
51
     * @param Hook $hook
52
     * @param Config $config
53
     * @param User $user
54
     * @param Translation $translation
55
     */
56
    public function __construct(Hook $hook, Config $config, User $user, Translation $translation)
57
    {
58
        $this->hook = $hook;
59
        $this->user = $user;
60
        $this->db = $config->getDb();
61
        $this->translation = $translation;
62
    }
63
64
    /**
65
     * Returns an array of backups or counts them
66
     * @param array $data
67
     * @return array|integer
68
     */
69
    public function getList(array $data = array())
70
    {
71
        $sql = 'SELECT b.*, u.name AS user_name';
72
73
        if (!empty($data['count'])) {
74
            $sql = 'SELECT COUNT(b.backup_id)';
75
        }
76
77
        $sql .= ' FROM backup b
78
                  LEFT JOIN user u ON(b.user_id = u.user_id)
79
                  WHERE b.backup_id > 0';
80
81
        $where = array();
82
83
        if (isset($data['user_id'])) {
84
            $sql .= ' AND b.user_id = ?';
85
            $where[] = $data['user_id'];
86
        }
87
88
        if (isset($data['id'])) {
89
            $sql .= ' AND b.id = ?';
90
            $where[] = $data['id'];
91
        }
92
93
        if (isset($data['version'])) {
94
            $sql .= ' AND b.version = ?';
95
            $where[] = $data['version'];
96
        }
97
98
        if (isset($data['name'])) {
99
            $sql .= ' AND b.name LIKE ?';
100
            $where[] = "%{$data['name']}%";
101
        }
102
103
        $allowed_order = array('asc', 'desc');
104
        $allowed_sort = array('name', 'user_id', 'version',
105
            'id', 'backup_id', 'type', 'created');
106
107
        if (isset($data['sort'])
108
            && in_array($data['sort'], $allowed_sort)
109
            && isset($data['order'])
110
            && in_array($data['order'], $allowed_order)
111
        ) {
112
            $sql .= " ORDER BY b.{$data['sort']} {$data['order']}";
113
        } else {
114
            $sql .= ' ORDER BY b.created DESC';
115
        }
116
117
        if (!empty($data['limit'])) {
118
            $sql .= ' LIMIT ' . implode(',', array_map('intval', $data['limit']));
119
        }
120
121
        if (!empty($data['count'])) {
122
            return (int) $this->db->fetchColumn($sql, $where);
123
        }
124
125
        $results = $this->db->fetchAll($sql, $where, array('index' => 'backup_id'));
126
        $this->hook->attach('module.backup.list', $results, $this);
127
        return $results;
128
    }
129
130
    /**
131
     * Adds a backup to the database
132
     * @param array $data
133
     * @return boolean|integer
134
     */
135
    public function add(array $data)
136
    {
137
        $result = null;
138
        $this->hook->attach('module.backup.add.before', $data, $result, $this);
139
140
        if (isset($result)) {
141
            return $result;
142
        }
143
144
        $version = null;
145
        if (isset($data['version'])) {
146
            $version = $data['version'];
147
        }
148
149
        if ($this->exists($data['id'], $version)) {
150
            return false;
151
        }
152
153
        if (empty($data['user_id'])) {
154
            $data['user_id'] = $this->user->getId();
155
        }
156
157
        $data['created'] = GC_TIME;
158
        $result = $this->db->insert('backup', $data);
159
160
        $this->hook->attach('module.backup.add.after', $data, $result, $this);
161
        return $result;
162
    }
163
164
    /**
165
     * Loads a backup from the database
166
     * @param integer $id
167
     * @return array
168
     */
169
    public function get($id)
170
    {
171
        $sql = 'SELECT * FROM backup WHERE backup_id=?';
172
        return $this->db->fetch($sql, array($id));
173
    }
174
175
    /**
176
     * Deletes a backup from disk and database
177
     * @param integer $id
178
     * @return boolean
179
     */
180
    public function delete($id)
181
    {
182
        $result = null;
183
        $this->hook->attach('module.backup.delete.before', $id, $this);
184
185
        if (isset($result)) {
186
            return $result;
187
        }
188
189
        $result = $this->deleteZip($id);
190
191
        if ($result) {
192
            $this->db->delete('backup', array('backup_id' => $id));
193
        }
194
195
        $this->hook->attach('module.backup.delete.after', $id, $result, $this);
196
        return (bool) $result;
197
    }
198
199
    /**
200
     * Deletes a backup ZIP archive
201
     * @param integer $backup_id
202
     * @return boolean
203
     */
204
    protected function deleteZip($backup_id)
205
    {
206
        $backup = $this->get($backup_id);
207
208
        if (empty($backup['path'])) {
209
            return false;
210
        }
211
212
        $file = gplcart_file_absolute($backup['path']);
213
        return file_exists($file) && unlink($file);
214
    }
215
216
    /**
217
     * Performs backup operation
218
     * @param string $handler_id
219
     * @param array $data
220
     * @return boolean|string
221
     */
222
    public function backup($handler_id, $data)
223
    {
224
        return $this->callHandler($handler_id, 'backup', array($data, $this));
225
    }
226
227
    /**
228
     * Performs restore operation
229
     * @param string $handler_id
230
     * @param array $data
231
     * @return boolean|string
232
     */
233
    public function restore($handler_id, $data)
234
    {
235
        return $this->callHandler($handler_id, 'restore', array($data, $this));
236
    }
237
238
    /**
239
     * Whether a backup already exists
240
     * @param string $id
241
     * @param null|string $version
242
     * @return bool
243
     */
244
    public function exists($id, $version = null)
245
    {
246
        $list = $this->getList(array('id' => $id, 'version' => $version));
247
        return !empty($list);
248
    }
249
250
    /**
251
     * Returns an array of backup handlers
252
     * @return array
253
     */
254
    public function getHandlers()
255
    {
256
        $handlers = &gplcart_static(__METHOD__);
257
258
        if (isset($handlers)) {
259
            return $handlers;
260
        }
261
262
        $handlers = $this->getDefaultHandlers();
263
        $this->hook->attach('module.backup.handlers', $handlers, $this);
264
        return $handlers;
265
    }
266
267
    /**
268
     * Returns a single handler
269
     * @param string $handler_id
270
     * @return array
271
     */
272
    public function getHandler($handler_id)
273
    {
274
        $handlers = $this->getHandlers();
275
        return empty($handlers[$handler_id]) ? array() : $handlers[$handler_id];
276
    }
277
278
    /**
279
     * Cal a handler
280
     * @param string $handler_id
281
     * @param string $method
282
     * @param array $arguments
283
     * @return mixed
284
     */
285
    protected function callHandler($handler_id, $method, array $arguments)
286
    {
287
        try {
288
            $handlers = $this->getHandlers();
289
            return Handler::call($handlers, $handler_id, $method, $arguments);
290
        } catch (Exception $ex) {
291
            return $ex->getMessage();
292
        }
293
    }
294
295
    /**
296
     * Returns an array of default backup handlers
297
     * @return array
298
     */
299
    protected function getDefaultHandlers()
300
    {
301
        $handlers = array();
302
303
        $handlers['module'] = array(
304
            'name' => $this->translation->text('Module'),
305
            'handlers' => array(
306
                'backup' => array('gplcart\\modules\\backup\\handlers\\Module', 'backup')
307
            ));
308
309
        return $handlers;
310
    }
311
312
}
313