Main   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hookRouteList() 0 8 1
A hookInstallHandlers() 0 4 1
A hookInstallBefore() 0 4 1
A hookTemplateRender() 0 4 1
A replaceTemplates() 0 6 3
A setInstallHandlers() 0 18 1
A checkRequiredModules() 0 13 4
A getModel() 0 6 1
1
<?php
2
3
/**
4
 * @package Base
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\base;
11
12
use gplcart\core\Config;
13
use gplcart\core\Container;
14
15
/**
16
 * Main class for Base module
17
 */
18
class Main
19
{
20
21
    /**
22
     * Config class instance
23
     * @var \gplcart\core\Config $config
24
     */
25
    protected $config;
26
27
    /**
28
     * @param Config $config
29
     */
30
    public function __construct(Config $config)
31
    {
32
        $this->config = $config;
33
    }
34
35
    /**
36
     * Implements hook "route.list"
37
     * @param array $routes
38
     */
39
    public function hookRouteList(array &$routes)
40
    {
41
        $routes['install/(\d+)'] = array(
42
            'handlers' => array(
43
                'controller' => array('gplcart\\modules\\base\\controllers\\Install', 'stepInstall')
44
            )
45
        );
46
    }
47
48
    /**
49
     * Implements hook "install.handlers"
50
     * @param array $handlers
51
     */
52
    public function hookInstallHandlers(array &$handlers)
53
    {
54
        $this->setInstallHandlers($handlers);
55
    }
56
57
    /**
58
     * Implements hook "install.before"
59
     * @param array $data
60
     * @param mixed $result
61
     */
62
    public function hookInstallBefore(array $data, &$result)
63
    {
64
        $this->checkRequiredModules($data, $result);
65
    }
66
67
    /**
68
     * Implements hook "template.render"
69
     * @param array $templates
70
     */
71
    public function hookTemplateRender(array &$templates)
72
    {
73
        $this->replaceTemplates($templates);
74
    }
75
76
    /**
77
     * Adds installation handlers
78
     * @param array $handlers
79
     */
80
    protected function setInstallHandlers(array &$handlers)
81
    {
82
        $handlers['base'] = array(
83
            'module' => 'base',
84
            'title' => gplcart_text('Base'),
85
            'steps' => array(
86
                1 => array('title' => gplcart_text('Configure modules')),
87
                2 => array('title' => gplcart_text('Create demo')),
88
                3 => array('title' => gplcart_text('Finish installation'))
89
            ),
90
            'handlers' => array(
91
                'install' => array('gplcart\\modules\\base\\handlers\\Install', 'install'),
92
                'install_1' => array('gplcart\\modules\\base\\handlers\\Install', 'installModules'),
93
                'install_2' => array('gplcart\\modules\\base\\handlers\\Install', 'installDemo'),
94
                'install_3' => array('gplcart\\modules\\base\\handlers\\Install', 'installFinish')
95
            )
96
        );
97
    }
98
99
    /**
100
     * Check if all required modules in place
101
     * @param array $data
102
     * @param array $result
103
     */
104
    protected function checkRequiredModules(array $data, &$result)
105
    {
106
        if ($data['installer'] === 'base'
107
            && empty($data['step'])
108
            && !$this->getModel()->hasAllRequiredModules()) {
109
110
            $result = array(
111
                'redirect' => '',
112
                'severity' => 'warning',
113
                'message' => gplcart_text('You cannot use this installer because some modules are missed in your distribution')
114
            );
115
        }
116
    }
117
118
    /**
119
     * Replace system templates
120
     * @param array $templates
121
     */
122
    protected function replaceTemplates(array &$templates)
123
    {
124
        if (substr($templates[0], -15) === 'dashboard/intro' && $this->config->get('installer') === 'base') {
125
            $templates[0] = __DIR__ . '/templates/intro';
126
        }
127
    }
128
129
    /**
130
     * Returns the module model
131
     * @return \gplcart\modules\base\models\Install
132
     */
133
    public function getModel()
134
    {
135
        /** @var \gplcart\modules\base\models\Install $instance */
136
        $instance = Container::get('gplcart\\modules\\base\\models\\Install');
137
        return $instance;
138
    }
139
140
}
141