Completed
Push — master ( 48045c...bbcd87 )
by Iurii
01:36
created

Install::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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