Completed
Push — master ( 566d92...8f78c0 )
by Iurii
01:40
created

Reset   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A editReset() 0 10 1
A setTitleEditReset() 0 4 1
A setBreadcrumbEditReset() 0 9 1
A submitReset() 0 6 3
A validateReset() 0 9 2
A doReset() 0 8 1
A doConfigReset() 0 5 1
A doDbTablesReset() 0 11 2
A outputEditReset() 0 4 1
1
<?php
2
3
/**
4
 * @package Reset
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\reset\controllers;
11
12
use gplcart\core\controllers\backend\Controller as BackendController;
13
14
/**
15
 * Handles incoming requests and outputs data related to Reset module
16
 */
17
class Reset extends BackendController
18
{
19
20
    /**
21
     * Constructor
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
    }
27
28
    /**
29
     * Route page callback
30
     */
31
    public function editReset()
32
    {
33
        $this->controlAccessSuperAdmin();
34
35
        $this->setTitleEditReset();
36
        $this->setBreadcrumbEditReset();
37
38
        $this->submitReset();
39
        $this->outputEditReset();
40
    }
41
42
    /**
43
     * Set title on the reset page
44
     */
45
    protected function setTitleEditReset()
46
    {
47
        $this->setTitle($this->text('Reset'));
48
    }
49
50
    /**
51
     * Set breadcrumbs on the reset page
52
     */
53
    protected function setBreadcrumbEditReset()
54
    {
55
        $breadcrumb = array(
56
            'text' => $this->text('Dashboard'),
57
            'url' => $this->url('admin')
58
        );
59
60
        $this->setBreadcrumb($breadcrumb);
61
    }
62
63
    /**
64
     * Handles submitted data
65
     */
66
    protected function submitReset()
67
    {
68
        if ($this->isPosted('submit') && $this->validateReset()) {
69
            $this->doReset();
70
        }
71
    }
72
73
    /**
74
     * Validates submitted data
75
     * @return boolean
76
     */
77
    protected function validateReset()
78
    {
79
        $this->setSubmitted('reset');
80
81
        if ($this->getSubmitted('confirm') !== $this->store('name')) {
82
            $this->setError('confirm', $this->text('Wrong confirmation word'));
83
        }
84
        return !$this->hasErrors();
85
    }
86
87
    /**
88
     * Update module settings
89
     */
90
    protected function doReset()
91
    {
92
        $this->controlAccessSuperAdmin();
93
        $this->doDbTablesReset();
94
        $this->doConfigReset();
95
96
        $this->url->redirect($this->url->get('install', array(), true, true));
97
    }
98
99
    /**
100
     * Delete config files created during installation
101
     */
102
    protected function doConfigReset()
103
    {
104
        chmod(GC_CONFIG_COMMON, 0777);
105
        unlink(GC_CONFIG_COMMON);
106
    }
107
108
    /**
109
     * Delete all tables from the database
110
     */
111
    public function doDbTablesReset()
112
    {
113
        $db = $this->config->getDb();
114
        $db->exec('SET FOREIGN_KEY_CHECKS = 0');
115
116
        foreach ($db->fetchColumnAll('SHOW TABLES') as $table) {
117
            $db->query("DROP TABLE $table");
118
        }
119
120
        $db->exec('SET FOREIGN_KEY_CHECKS = 1');
121
    }
122
123
    /**
124
     * Render and output the reset page
125
     */
126
    protected function outputEditReset()
127
    {
128
        $this->output('reset|reset');
129
    }
130
131
}
132