Main   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 1
dl 0
loc 159
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hookModuleInstallBefore() 0 13 3
A hookModuleUninstallAfter() 0 4 1
A hookUserRolePermissions() 0 6 1
A hookRouteList() 0 12 1
A backup() 0 4 1
A restore() 0 4 1
A getHandlers() 0 4 1
A exists() 0 4 1
A getModel() 0 6 1
A getTranslationModel() 0 6 1
A getDbScheme() 0 17 1
1
<?php
2
3
/**
4
 * @package Backup
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\backup;
11
12
use Exception;
13
use gplcart\core\Config;
14
use gplcart\core\Container;
15
16
/**
17
 * Main class for Backup module
18
 */
19
class Main
20
{
21
22
    /**
23
     * Database class instance
24
     * @var \gplcart\core\Database $db
25
     */
26
    protected $db;
27
28
    /**
29
     * @param Config $config
30
     */
31
    public function __construct(Config $config)
32
    {
33
        $this->db = $config->getDb();
34
        $this->db->addScheme($this->getDbScheme());
35
    }
36
37
    /**
38
     * Implements hook "module.install.before"
39
     * @param null|string $result
40
     */
41
    public function hookModuleInstallBefore(&$result)
42
    {
43
        if (!class_exists('ZipArchive')) {
44
            $result = $this->getTranslationModel()->text('Class ZipArchive does not exist');
45
        } else {
46
47
            try {
48
                $this->db->importScheme('backup', $this->getDbScheme());
49
            } catch (Exception $ex) {
50
                $result = $ex->getMessage();
51
            }
52
        }
53
    }
54
55
    /**
56
     * Implements hook "module.uninstall.after"
57
     */
58
    public function hookModuleUninstallAfter()
59
    {
60
        $this->db->deleteTable('backup');
61
    }
62
63
    /**
64
     * Implements hook "user.role.permissions"
65
     * @param array $permissions
66
     */
67
    public function hookUserRolePermissions(array &$permissions)
68
    {
69
        $permissions['backup'] = 'Backup: access'; // @text
70
        $permissions['backup_delete'] = 'Backup: delete'; // @text
71
        $permissions['backup_download'] = 'Backup: download'; // @text
72
    }
73
74
    /**
75
     * Implements hook "route.list"
76
     * @param array $routes
77
     */
78
    public function hookRouteList(array &$routes)
79
    {
80
        $routes['admin/report/backup'] = array(
81
            'access' => 'backup',
82
            'menu' => array(
83
                'admin' => 'Backups' // @text
84
            ),
85
            'handlers' => array(
86
                'controller' => array('gplcart\\modules\\backup\\controllers\\Backup', 'listBackup')
87
            )
88
        );
89
    }
90
91
    /**
92
     * Performs backup operation
93
     * @param string $handler_id
94
     * @param array $data
95
     * @return boolean|string
96
     */
97
    public function backup($handler_id, array $data)
98
    {
99
        return $this->getModel()->backup($handler_id, $data);
100
    }
101
102
    /**
103
     * Performs restore operation
104
     * @param string $handler_id
105
     * @param array $data
106
     * @return boolean|string
107
     */
108
    public function restore($handler_id, array $data)
109
    {
110
        return $this->getModel()->restore($handler_id, $data);
111
    }
112
113
    /**
114
     * Returns an array of defined handlers
115
     * @return array
116
     */
117
    public function getHandlers()
118
    {
119
        return $this->getModel()->getHandlers();
120
    }
121
122
    /**
123
     * Whether a backup already exists
124
     * @param string $id
125
     * @param null|string $version
126
     * @return bool
127
     */
128
    public function exists($id, $version = null)
129
    {
130
        return $this->getModel()->exists($id, $version);
131
    }
132
133
    /**
134
     * Returns backup model
135
     * @return \gplcart\modules\backup\models\Backup
136
     */
137
    protected function getModel()
138
    {
139
        /** @var \gplcart\modules\backup\models\Backup $instance */
140
        $instance = Container::get('gplcart\\modules\\backup\\models\\Backup');
141
        return $instance;
142
    }
143
144
    /**
145
     * Translation UI model instance
146
     * @return \gplcart\core\models\Translation
147
     */
148
    protected function getTranslationModel()
149
    {
150
        /** @var \gplcart\core\models\Translation $instance */
151
        $instance = Container::get('gplcart\\core\\models\\Translation');
152
        return $instance;
153
    }
154
155
    /**
156
     * Returns an array of database scheme
157
     * @return array
158
     */
159
    protected function getDbScheme()
160
    {
161
        return array(
162
            'backup' => array(
163
                'fields' => array(
164
                    'backup_id' => array('type' => 'int', 'length' => 10, 'auto_increment' => true, 'primary' => true),
165
                    'created' => array('type' => 'int', 'length' => 10, 'not_null' => true),
166
                    'name' => array('type' => 'varchar', 'length' => 255, 'not_null' => true),
167
                    'path' => array('type' => 'varchar', 'length' => 255, 'not_null' => true),
168
                    'user_id' => array('type' => 'int', 'length' => 10, 'not_null' => true, 'default' => 0),
169
                    'type' => array('type' => 'varchar', 'length' => 50, 'not_null' => true, 'default' => ''),
170
                    'version' => array('type' => 'varchar', 'length' => 50, 'not_null' => true, 'default' => ''),
171
                    'id' => array('type' => 'varchar', 'length' => 50, 'not_null' => true, 'default' => '')
172
                )
173
            )
174
        );
175
    }
176
177
}
178