Completed
Pull Request — master (#114)
by Bart
03:44
created

Schematic   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 3
dl 0
loc 187
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 25 1
C getRecords() 0 38 11
A error() 0 4 1
A warning() 0 4 1
A info() 0 4 1
A importError() 0 9 3
1
<?php
2
3
namespace NerdsAndCompany\Schematic;
4
5
use Craft;
6
use craft\base\Model;
7
use yii\base\Module;
8
use yii\helpers\Console;
9
use NerdsAndCompany\Schematic\Mappers\ElementIndexMapper;
10
use NerdsAndCompany\Schematic\Mappers\ModelMapper;
11
use NerdsAndCompany\Schematic\Mappers\PluginMapper;
12
use NerdsAndCompany\Schematic\Mappers\UserSettingsMapper;
13
14
/**
15
 * Schematic.
16
 *
17
 * Sync Craft Setups.
18
 *
19
 * @author    Nerds & Company
20
 * @copyright Copyright (c) 2015-2018, Nerds & Company
21
 * @license   MIT
22
 *
23
 * @see      http://www.nerds.company
24
 */
25
class Schematic extends Module
26
{
27
    /**
28
     * @var string
29
     */
30
    public $controllerNamespace = 'NerdsAndCompany\Schematic\Controllers';
31
32
    /**
33
     * Initialize the module.
34
     */
35
    public function init()
36
    {
37
        Craft::setAlias('@NerdsAndCompany/Schematic', __DIR__);
38
39
        $config = [
40
            'components' => [
41
                'elementIndexMapper' => [
42
                    'class' => ElementIndexMapper::class,
43
                ],
44
                'modelMapper' => [
45
                    'class' => ModelMapper::class,
46
                ],
47
                'pluginMapper' => [
48
                    'class' => PluginMapper::class,
49
                ],
50
                'userSettingsMapper' => [
51
                    'class' => UserSettingsMapper::class,
52
                ],
53
            ],
54
        ];
55
56
        Craft::configure($this, $config);
57
58
        parent::init();
59
    }
60
61
    /**
62
     * The available datatypes.
63
     *
64
     * @TODO: Make data types and Mapper components configurable.
65
     *
66
     * @var array
67
     */
68
    const DATA_TYPES = [
69
        'sites' => [
70
            'mapper' => 'modelMapper',
71
        ],
72
        'volumes' => [
73
            'mapper' => 'modelMapper',
74
        ],
75
        'assetTransforms' => [
76
            'mapper' => 'modelMapper',
77
        ],
78
        'fields' => [
79
            'mapper' => 'modelMapper',
80
        ],
81
        'plugins' => [
82
            'mapper' => 'pluginMapper',
83
        ],
84
        'sections' => [
85
            'mapper' => 'modelMapper',
86
        ],
87
        'globalSets' => [
88
            'mapper' => 'modelMapper',
89
        ],
90
        'userGroups' => [
91
            'mapper' => 'modelMapper',
92
        ],
93
        'users' => [
94
            'mapper' => 'userSettingsMapper',
95
        ],
96
        'categoryGroups' => [
97
            'mapper' => 'modelMapper',
98
        ],
99
        'tagGroups' => [
100
            'mapper' => 'modelMapper',
101
        ],
102
        'elementIndexSettings' => [
103
            'mapper' => 'elementIndexMapper',
104
        ],
105
    ];
106
107
    /**
108
     * Get records for datatype.
109
     *
110
     * @TODO: Make this more dynamic
111
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
112
     *
113
     * @param string $datatype
114
     *
115
     * @return Model[]
116
     */
117
    public static function getRecords(string $datatype)
118
    {
119
        $records = [];
120
        switch ($datatype) {
121
            case 'assetTransforms':
122
                $records = Craft::$app->assetTransforms->getAllTransforms();
123
                break;
124
            case 'categoryGroups':
125
                $records = Craft::$app->categories->getAllGroups();
126
                break;
127
            case 'fields':
128
                $records = Craft::$app->fields->getAllFields();
129
                break;
130
            case 'globalSets':
131
                $records = Craft::$app->globals->getAllSets();
132
                break;
133
            case 'plugins':
134
                $records = Craft::$app->plugins->getAllPluginInfo();
135
                break;
136
            case 'sections':
137
                $records = Craft::$app->sections->getAllSections();
138
                break;
139
            case 'sites':
140
                $records = Craft::$app->sites->getAllSites();
141
                break;
142
            case 'userGroups':
143
                $records = Craft::$app->userGroups->getAllGroups();
144
                break;
145
            case 'volumes':
146
                $records = Craft::$app->volumes->getAllVolumes();
147
                break;
148
            case 'tagGroups':
149
                $records = Craft::$app->tags->getAllTagGroups();
150
                break;
151
        }
152
153
        return $records;
154
    }
155
156
    /**
157
     * Is force enabled?
158
     *
159
     * @var bool
160
     */
161
    public static $force = false;
162
163
    /**
164
     * Logs an error message.
165
     *
166
     * @param string|array $message the message to be logged. This can be a simple string or a more
167
     *                              complex data structure, such as array.
168
     */
169
    public static function error($message)
170
    {
171
        Craft::$app->controller->stdout($message.PHP_EOL, Console::FG_RED);
172
    }
173
174
    /**
175
     * Logs a warning message.
176
     *
177
     * @param string|array $message the message to be logged. This can be a simple string or a more
178
     *                              complex data structure, such as array.
179
     */
180
    public static function warning($message)
181
    {
182
        Craft::$app->controller->stdout($message.PHP_EOL, Console::FG_YELLOW);
183
    }
184
185
    /**
186
     * Logs an info message.
187
     *
188
     * @param string|array $message the message to be logged. This can be a simple string or a more
189
     *                              complex data structure, such as array.
190
     */
191
    public static function info($message)
192
    {
193
        Craft::$app->controller->stdout($message.PHP_EOL);
194
    }
195
196
    /**
197
     * Log an import error.
198
     *
199
     * @param Model  $record
200
     * @param string $handle
201
     */
202
    public static function importError(Model $record, string $handle)
203
    {
204
        static::warning('- Error importing '.get_class($record).' '.$handle);
205
        foreach ($record->getErrors() as $errors) {
206
            foreach ($errors as $error) {
207
                static::error('   - '.$error);
208
            }
209
        }
210
    }
211
}
212