Completed
Push — master ( ab64ac...2b316e )
by Iurii
01:05
created

Install::install()   B

Complexity

Conditions 3
Paths 5

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 5
nop 2
1
<?php
2
3
/**
4
 * @package Base
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\base\handlers;
11
12
use Exception;
13
use gplcart\core\handlers\install\Base as BaseInstaller;
14
use gplcart\core\Module;
15
use gplcart\modules\base\models\Install as ModuleInstallModel;
16
use UnexpectedValueException;
17
18
/**
19
 * Contains methods for installing Base profile
20
 */
21
class Install extends BaseInstaller
22
{
23
24
    /**
25
     * Base module installer model
26
     * @var \gplcart\modules\base\models\Install $installer
27
     */
28
    protected $install_model;
29
30
    /**
31
     * Module class instance
32
     * @var \gplcart\core\Module $module
33
     */
34
    protected $module;
35
36
    /**
37
     * @param Module $module
38
     * @param ModuleInstallModel $install_model
39
     */
40
    public function __construct(Module $module, ModuleInstallModel $install_model)
41
    {
42
        parent::__construct();
43
44
        $this->module = $module;
45
        $this->install_model = $install_model;
46
    }
47
48
    /**
49
     * Performs initial system installation. Step 0
50
     * @param array $data
51
     * @param \gplcart\core\Database $db
52
     * @return array
53
     */
54
    public function install(array $data, $db)
55
    {
56
        $this->db = $db;
57
        $this->data = $data;
58
        $this->data['step'] = 0;
59
60
        if (GC_CLI) {
61
            $this->installCli();
62
            return array();
63
        }
64
65
        try {
66
67
            $this->start();
68
            $this->process();
69
70
            return array(
71
                'message' => '',
72
                'severity' => 'success',
73
                'redirect' => 'install/' . ($this->data['step'] + 1)
74
            );
75
76
        } catch (Exception $ex) {
77
            return array(
78
                'redirect' => '',
79
                'severity' => 'warning',
80
                'message' => $ex->getMessage()
81
            );
82
        }
83
    }
84
85
    /**
86
     * Install in CLI mode
87
     */
88
    protected function installCli()
89
    {
90
        try {
91
92
            $this->process();
93
            $this->installCliStep1();
94
            $this->installCliStep2();
95
            $this->installCliStep3();
96
97
        } catch (Exception $ex) {
98
            $this->cli->error($ex->getMessage());
99
        }
100
    }
101
102
    /**
103
     * Process step 1 in CLI mode
104
     * @throws UnexpectedValueException
105
     */
106
    protected function installCliStep1()
107
    {
108
        $this->data['step'] = 1;
109
        $this->setCliMessage('Configuring modules...');
110
        $result = $this->installModules($this->data, $this->db);
111
112
        if ($result['severity'] !== 'success') {
113
            throw new UnexpectedValueException($result['message']);
114
        }
115
    }
116
117
    /**
118
     * Process step 2 in CLI mode
119
     * @throws UnexpectedValueException
120
     */
121
    protected function installCliStep2()
122
    {
123
        $title = $this->translation->text('Please select a demo content package (enter a number)');
124
        $this->data['demo_handler_id'] = $this->cli->menu($this->getDemoOptions(), '', $title);
125
126
        if (!empty($this->data['demo_handler_id'])) {
127
128
            $this->data['step'] = 2;
129
            $this->setCliMessage('Installing demo content...');
130
            $result = $this->installDemo($this->data, $this->db);
131
132
            if ($result['severity'] !== 'success') {
133
                throw new UnexpectedValueException($result['message']);
134
            }
135
136
            $this->setContext('demo_handler_id', $this->data['demo_handler_id']);
137
        }
138
    }
139
140
    /**
141
     * Precess step 3 in CLI mode
142
     * @throws UnexpectedValueException
143
     */
144
    protected function installCliStep3()
145
    {
146
        $this->data['step'] = 3;
147
        $result = $this->installFinish($this->data, $this->db);
148
149
        if ($result['severity'] !== 'success') {
150
            throw new UnexpectedValueException($result['message']);
151
        }
152
    }
153
154
    /**
155
     * Installs required modules. Step 1
156
     * @param array $data
157
     * @param \gplcart\core\Database $db
158
     * @return array
159
     */
160
    public function installModules(array $data, $db)
161
    {
162
        $this->db = $db;
163
        $this->data = $data;
164
165
        try {
166
167
            $this->install_model->installModules();
168
            $this->configureModules();
169
170
            return array(
171
                'message' => '',
172
                'severity' => 'success',
173
                'redirect' => 'install/' . ($this->data['step'] + 1)
174
            );
175
176
        } catch (Exception $ex) {
177
178
            $this->setContextError($this->data['step'], $ex->getMessage());
179
180
            return array(
181
                'redirect' => '',
182
                'severity' => 'danger',
183
                'message' => $ex->getMessage()
184
            );
185
        }
186
    }
187
188
    /**
189
     * Install a demo-content. Step 2
190
     * @param array $data
191
     * @param \gplcart\core\Database $db
192
     * @return array
193
     */
194
    public function installDemo(array $data, $db)
195
    {
196
        set_time_limit(0);
197
198
        $this->db = $db;
199
        $this->data = $data;
200
201
        $success_result = array(
202
            'message' => '',
203
            'severity' => 'success',
204
            'redirect' => 'install/' . ($this->data['step'] + 1)
205
        );
206
207
        if (empty($data['demo_handler_id'])) {
208
            return $success_result;
209
        }
210
211
        $result = $this->install_model->getDemoModule()->create($this->getContext('store_id'), $data['demo_handler_id']);
212
213
        if ($result !== true) {
214
            $this->setContextError($this->data['step'], $result);
215
        }
216
217
        return $success_result;
218
    }
219
220
    /**
221
     * Performs final tasks. Step 3
222
     * @param array $data
223
     * @param \gplcart\core\Database $db
224
     * @return array
225
     */
226
    public function installFinish(array $data, $db)
227
    {
228
        $this->db = $db;
229
        $this->data = $data;
230
231
        $result = $this->finish();
232
        $errors = $this->getContextErrors();
233
234
        if (empty($errors)) {
235
            if ($this->getContext('demo_handler_id')) {
236
                $store_id = $this->getContext('store_id');
237
                $this->getStoreModel()->update($store_id, array('status' => 1));
238
            }
239
        } else {
240
            $result['severity'] = 'warning';
241
            $result['message'] = implode(PHP_EOL, $errors);
242
        }
243
244
        return $result;
245
    }
246
247
    /**
248
     * Returns an array of demo content options
249
     * @return array
250
     */
251
    protected function getDemoOptions()
252
    {
253
        $options = array('' => $this->translation->text('No demo'));
254
255
        foreach ($this->install_model->getDemoHandlers() as $id => $handler) {
256
            $options[$id] = $handler['title'];
257
        }
258
259
        return $options;
260
    }
261
262
    /**
263
     * Configure module settings
264
     */
265
    protected function configureModules()
266
    {
267
        $this->configureModuleDevice();
268
        $this->configureModuleGaReport();
269
    }
270
271
    /**
272
     * Configure Device module settings
273
     */
274
    protected function configureModuleDevice()
275
    {
276
        $store_id = $this->getContext('store_id');
277
278
        $settings = array();
279
        $settings['theme'][$store_id]['mobile'] = 'mobile';
280
        $settings['theme'][$store_id]['tablet'] = 'mobile';
281
282
        $this->module->setSettings('device', $settings);
283
    }
284
285
    /**
286
     * Configure Google Analytics Report module settings
287
     */
288
    protected function configureModuleGaReport()
289
    {
290
        $info = $this->module->getInfo('ga_report');
291
292
        $info['settings']['dashboard'] = array(
293
            'visit_date',
294
            'pageview_date',
295
            'content_statistic',
296
            'top_pages',
297
            'source',
298
            'keyword',
299
            'audience'
300
        );
301
302
        $this->module->setSettings('ga_report', $info['settings']);
303
    }
304
305
}
306