Completed
Push — master ( c9d315...7f8584 )
by Iurii
01:14
created

Backup::getList()   D

Complexity

Conditions 12
Paths 256

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 4.7667
c 0
b 0
f 0
cc 12
eloc 35
nc 256
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        if (empty($data['user_id'])) {
126
            $data['user_id'] = $this->user->getId();
127
        }
128
129
        $data['created'] = GC_TIME;
130
        $result = $this->db->insert('backup', $data);
131
132
        $this->hook->attach('module.backup.add.after', $data, $result, $this);
133
        return $result;
134
    }
135
136
    /**
137
     * Loads a backup from the database
138
     * @param integer $id
139
     * @return array
140
     */
141
    public function get($id)
142
    {
143
        $sql = 'SELECT * FROM backup WHERE backup_id=?';
144
        return $this->db->fetch($sql, array($id));
145
    }
146
147
    /**
148
     * Deletes a backup from disk and database
149
     * @param integer $id
150
     * @return boolean
151
     */
152
    public function delete($id)
153
    {
154
        $result = null;
155
        $this->hook->attach('module.backup.delete.before', $id, $this);
156
157
        if (isset($result)) {
158
            return $result;
159
        }
160
161
        $result = (bool) $this->db->delete('backup', array('backup_id' => $id));
162
163
        if ($result) {
164
            $this->deleteZip($id);
165
        }
166
167
        $this->hook->attach('module.backup.delete.after', $id, $result, $this);
168
        return (bool) $result;
169
    }
170
171
    /**
172
     * Deletes a backup ZIP archive
173
     * @param integer $backup_id
174
     * @return boolean
175
     */
176
    protected function deleteZip($backup_id)
177
    {
178
        $backup = $this->get($backup_id);
179
180
        if (isset($backup['path']) && file_exists(GC_FILE_DIR . "/{$backup['path']}")) {
181
            return unlink(GC_FILE_DIR . "/{$backup['path']}");
182
        }
183
184
        return false;
185
    }
186
187
    /**
188
     * Performs backup operation
189
     * @param string $handler_id
190
     * @param array $data
191
     * @return boolean|string
192
     */
193
    public function backup($handler_id, $data)
194
    {
195
        $handlers = $this->getHandlers();
196
        return Handler::call($handlers, $handler_id, 'backup', array($data, $this));
197
    }
198
199
    /**
200
     * Performs restore operation
201
     * @param string $handler_id
202
     * @param array $data
203
     * @return boolean|string
204
     */
205
    public function restore($handler_id, $data)
206
    {
207
        $handlers = $this->getHandlers();
208
        return Handler::call($handlers, $handler_id, 'restore', array($data, $this));
209
    }
210
211
    /**
212
     * Whether a backup already exists
213
     * @param string $id
214
     * @param null|string $version
215
     * @return bool
216
     */
217
    public function exists($id, $version = null)
218
    {
219
        $list = $this->getList(array('id' => $id, 'version' => $version));
220
        return !empty($list);
221
    }
222
223
    /**
224
     * Returns an array of backup handlers
225
     * @return array
226
     */
227
    public function getHandlers()
228
    {
229
        $handlers = &gplcart_static(__METHOD__);
230
231
        if (isset($handlers)) {
232
            return $handlers;
233
        }
234
235
        $handlers = $this->getDefaultHandlers();
236
        $this->hook->attach('module.backup.handlers', $handlers, $this);
237
        return $handlers;
238
    }
239
240
    /**
241
     * Returns a single handler
242
     * @param string $handler_id
243
     * @return array
244
     */
245
    public function getHandler($handler_id)
246
    {
247
        $handlers = $this->getHandlers();
248
        return empty($handlers[$handler_id]) ? array() : $handlers[$handler_id];
249
    }
250
251
    /**
252
     * Returns an array of default backup handlers
253
     * @return array
254
     */
255
    protected function getDefaultHandlers()
256
    {
257
        $handlers = array();
258
259
        $handlers['module'] = array(
260
            'name' => $this->language->text('Module'),
261
            'handlers' => array(
262
                'backup' => array('gplcart\\modules\\backup\\handlers\\Module', 'backup')
263
        ));
264
265
        return $handlers;
266
    }
267
268
}
269