Backup   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 151
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A listBackup() 0 14 1
A setFilterListBackup() 0 5 1
A downloadListBackup() 0 16 3
B actionListBackup() 0 17 5
A setTitleListBackup() 0 4 1
A setBreadcrumbListBackup() 0 9 1
A outputListBackup() 0 4 1
A getListBackup() 0 6 1
A setPagerListBackup() 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\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
use gplcart\modules\backup\models\Backup as BackupModel;
14
15
/**
16
 * Handles incoming requests and outputs data related to Backup module
17
 */
18
class Backup extends Controller
19
{
20
21
    /**
22
     * @var \gplcart\modules\backup\models\Backup $backup
23
     */
24
    protected $backup;
25
26
    /**
27
     * Pager limit
28
     * @var array
29
     */
30
    protected $data_limit;
31
32
    /**
33
     * Backup constructor.
34
     * @param BackupModel $backup
35
     */
36
    public function __construct(BackupModel $backup)
37
    {
38
        parent::__construct();
39
40
        $this->backup = $backup;
41
    }
42
43
    /**
44
     * Displays the backup overview page
45
     */
46
    public function listBackup()
47
    {
48
        $this->downloadListBackup();
49
        $this->actionListBackup();
50
        $this->setTitleListBackup();
51
        $this->setBreadcrumbListBackup();
52
        $this->setFilterListBackup();
53
        $this->setPagerListBackup();
54
55
        $this->setData('backups', $this->getListBackup());
56
        $this->setData('handlers', $this->backup->getHandlers());
57
58
        $this->outputListBackup();
59
    }
60
61
    /**
62
     * Sets filter parameters
63
     */
64
    protected function setFilterListBackup()
65
    {
66
        $allowed = array('created', 'name', 'user_id', 'type', 'version', 'id', 'backup_id');
67
        $this->setFilter($allowed);
68
    }
69
70
    /**
71
     * Downloads a backup
72
     */
73
    protected function downloadListBackup()
74
    {
75
        $backup_id = $this->getQuery('download');
76
77
        if (empty($backup_id)) {
78
            return null;
79
        }
80
81
        $this->controlAccess('backup_download');
82
83
        $backup = $this->backup->get($backup_id);
84
85
        if (!empty($backup['path'])) {
86
            $this->download(gplcart_file_absolute($backup['path']));
87
        }
88
    }
89
90
    /**
91
     * Applies an action to the selected backups
92
     */
93
    protected function actionListBackup()
94
    {
95
        list($selected, $action) = $this->getPostedAction();
96
97
        $deleted = 0;
98
99
        foreach ($selected as $id) {
100
            if ($action === 'delete' && $this->access('backup_delete')) {
101
                $deleted += (int) $this->backup->delete($id);
102
            }
103
        }
104
105
        if ($deleted > 0) {
106
            $message = $this->text('Deleted %num item(s)', array('%num' => $deleted));
107
            $this->setMessage($message, 'success');
108
        }
109
    }
110
111
    /**
112
     * Sets title on the backup overview page
113
     */
114
    protected function setTitleListBackup()
115
    {
116
        $this->setTitle($this->text('Backups'));
117
    }
118
119
    /**
120
     * Sets breadcrumbs on the backup overview page
121
     */
122
    protected function setBreadcrumbListBackup()
123
    {
124
        $breadcrumb = array(
125
            'url' => $this->url('admin'),
126
            'text' => $this->text('Dashboard')
127
        );
128
129
        $this->setBreadcrumb($breadcrumb);
130
    }
131
132
    /**
133
     * Render and output the backup overview page
134
     */
135
    protected function outputListBackup()
136
    {
137
        $this->output('backup|list');
138
    }
139
140
    /**
141
     * Returns an array of backups
142
     * @return array
143
     */
144
    protected function getListBackup()
145
    {
146
        $options = $this->query_filter;
147
        $options['limit'] = $this->data_limit;
148
        return $this->backup->getList($options);
149
    }
150
151
    /**
152
     * Sets pager
153
     * @return array
154
     */
155
    protected function setPagerListBackup()
156
    {
157
        $options = $this->query_filter;
158
        $options['count'] = true;
159
160
        $pager = array(
161
            'query' => $this->query_filter,
162
            'total' => (int) $this->backup->getList($options)
163
        );
164
165
        return $this->data_limit = $this->setPager($pager);
166
    }
167
168
}
169