Completed
Push — master ( eba45d...dc6b5b )
by Iurii
01:05
created

Backup   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 35
c 0
b 0
f 0
lcom 2
cbo 0
dl 0
loc 274
rs 9

13 Methods

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