Demo::hookCliRouteList()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
3
/**
4
 * @package Demo
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\demo;
11
12
use gplcart\core\Module;
13
14
/**
15
 * Main class for Demo module
16
 */
17
class Demo extends Module
18
{
19
20
    /**
21
     * Constructor
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
    }
27
28
    /**
29
     * Implements hook "route.list"
30
     * @param array $routes
31
     */
32
    public function hookRouteList(array &$routes)
33
    {
34
        $routes['admin/tool/demo'] = array(
35
            'menu' => array('admin' => 'Demo content'),
36
            'access' => '__superadmin',
37
            'handlers' => array(
38
                'controller' => array('gplcart\\modules\\demo\\controllers\\Demo', 'editDemo')
39
            )
40
        );
41
    }
42
43
    /**
44
     * Implements hook "dashboard.intro"
45
     * @param array $items
46
     * @param \gplcart\core\controllers\backend\Dashboard $controller
47
     */
48
    public function hookDashboardIntro(array &$items, $controller)
49
    {
50
        if ($controller->isSuperadmin()) {
51
52
            $last = end($items);
53
            $count = count($items);
54
            $max = isset($last['weight']) && $last['weight'] > $count ? $last['weight'] : $count;
55
56
            $items['demo'] = array(
57
                'weight' => $max++,
58
                'rendered' => $controller->render('demo|intro')
59
            );
60
        }
61
    }
62
63
    /**
64
     * Implements hook "cli.route.list"
65
     * @param array $routes
66
     */
67
    public function hookCliRouteList(array &$routes)
68
    {
69
        // Create
70
        $routes['demo-create'] = array(
71
            'handlers' => array(
72
                'process' => array('gplcart\\modules\\demo\\controllers\\Cli', 'createCli')
73
            ),
74
            'help' => array(
75
                'description' => 'Populate a store with a demo content',
76
                'options' => array(
77
                    '--package' => 'Optional. A package ID used as an source. Defaults to "default"',
78
                    '--store' => 'Optional. A store ID to be populated with the demo content. Defaults to 1'
79
                )
80
            )
81
        );
82
83
        // Delete
84
        $routes['demo-delete'] = array(
85
            'handlers' => array(
86
                'process' => array('gplcart\\modules\\demo\\controllers\\Cli', 'deleteCli')
87
            ),
88
            'help' => array(
89
                'description' => 'Delete all created demo content from a store',
90
                'options' => array(
91
                    '--package' => 'Optional. A package ID that was used as an source. Defaults to "default"',
92
                    '--store' => 'Optional. A store ID that contains the demo content. Defaults to 1'
93
                )
94
            )
95
        );
96
    }
97
98
    /**
99
     * Implements hook "cli.install.finish"
100
     * @param mixed $result
101
     * @param string $message
102
     * @param \gplcart\core\controllers\cli\Install $controller
103
     */
104
    public function hookCliInstallFinish($result, &$message, $controller)
105
    {
106
        if ($result !== true) {
107
            return null;
108
        }
109
110
        /* @var $model \gplcart\modules\demo\models\Demo */
111
        $model = $this->getInstance('gplcart\\modules\\demo\\models\\Demo');
112
113
        $options = $this->getHandlerOptions($model, $controller);
114
115
        if (count($options) < 2) {
116
            return null;
117
        }
118
119
        $title = $controller->text('Would you like to create demo content? Enter a number of demo package');
120
        $input = (int) $controller->menu($options, 0, $title);
121
122
        if ($input < 2) {
123
            return null;
124
        }
125
126
        $created_result = $model->create(array_search($input, $options), 1);
127
128
        if ($created_result !== true) {
129
            $controller->line($created_result);
130
        }
131
    }
132
133
    /**
134
     * Returns an array of supported demo handlers
135
     * @param \gplcart\modules\demo\models\Demo $model
136
     * @param \gplcart\core\controllers\cli\Install $controller
137
     * @return array
138
     */
139
    protected function getHandlerOptions($model, $controller)
140
    {
141
        $options = array($controller->text('No demo'));
142
143
        foreach ($model->getHandlers() as $id => $handler) {
144
            $options[$id] = "$id - {$handler['title']}";
145
        }
146
147
        return $options;
148
    }
149
150
}
151