Completed
Push — general_settings ( 4d280d...4fdc5c )
by
unknown
29:30 queued 14:05
created

Schematic::init()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 6
cts 6
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 31
nc 1
nop 0
crap 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\base\Plugin;
8
use yii\helpers\Console;
9
use NerdsAndCompany\Schematic\DataTypes\AssetTransformDataType;
10
use NerdsAndCompany\Schematic\DataTypes\CategoryGroupDataType;
11
use NerdsAndCompany\Schematic\DataTypes\ElementIndexDataType;
12
use NerdsAndCompany\Schematic\DataTypes\EmailSettingsDataType;
13
use NerdsAndCompany\Schematic\DataTypes\FieldDataType;
14
use NerdsAndCompany\Schematic\DataTypes\GeneralSettingsDataType;
15
use NerdsAndCompany\Schematic\DataTypes\GlobalSetDataType;
16
use NerdsAndCompany\Schematic\DataTypes\PluginDataType;
17
use NerdsAndCompany\Schematic\DataTypes\SectionDataType;
18
use NerdsAndCompany\Schematic\DataTypes\SiteDataType;
19
use NerdsAndCompany\Schematic\DataTypes\TagGroupDataType;
20
use NerdsAndCompany\Schematic\DataTypes\UserGroupDataType;
21
use NerdsAndCompany\Schematic\DataTypes\UserSettingsDataType;
22
use NerdsAndCompany\Schematic\DataTypes\VolumeDataType;
23
use NerdsAndCompany\Schematic\Mappers\ElementIndexMapper;
24
use NerdsAndCompany\Schematic\Mappers\EmailSettingsMapper;
25
use NerdsAndCompany\Schematic\Mappers\ModelMapper;
26
use NerdsAndCompany\Schematic\Mappers\PluginMapper;
27
use NerdsAndCompany\Schematic\Mappers\UserSettingsMapper;
28
use NerdsAndCompany\Schematic\Interfaces\ConverterInterface;
29
use NerdsAndCompany\Schematic\Interfaces\DataTypeInterface;
30
use NerdsAndCompany\Schematic\Interfaces\MapperInterface;
31
use NerdsAndCompany\Schematic\Events\ConverterEvent;
32
33
/**
34
 * Schematic.
35
 *
36
 * Sync Craft Setups.
37
 *
38
 * @author    Nerds & Company
39
 * @copyright Copyright (c) 2015-2018, Nerds & Company
40
 * @license   MIT
41
 *
42
 * @see      http://www.nerds.company
43
 *
44
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
45
 */
46
class Schematic extends Plugin
47
{
48
    const EVENT_RESOLVE_CONVERTER = 'resolve_converter';
49
50
    /**
51
     * @var string
52
     */
53
    public $controllerNamespace = 'NerdsAndCompany\Schematic\Controllers';
54
55
    /**
56
     * @var array
57
     */
58
    public $dataTypes = [];
59
60
    /**
61
     * Initialize the module.
62
     */
63 10
    public function init()
64
    {
65 10
        Craft::setAlias('@NerdsAndCompany/Schematic', __DIR__);
66
67
        $config = [
68 10
            'components' => [
69
                'elementIndexMapper' => [
70
                    'class' => ElementIndexMapper::class,
71
                ],
72
                'emailSettingsMapper' => [
73
                    'class' => EmailSettingsMapper::class,
74
                ],
75
                'modelMapper' => [
76
                    'class' => ModelMapper::class,
77
                ],
78
                'pluginMapper' => [
79
                    'class' => PluginMapper::class,
80
                ],
81
                'userSettingsMapper' => [
82
                    'class' => UserSettingsMapper::class,
83
                ],
84
            ],
85
            'dataTypes' => [
86
                'plugins' => PluginDataType::class,
87
                'sites' => SiteDataType::class,
88
                'volumes' => VolumeDataType::class,
89
                'assetTransforms' => AssetTransformDataType::class,
90
                'emailSettings' => EmailSettingsDataType::class,
91
                'fields' => FieldDataType::class,
92
                'generalSettings' => GeneralSettingsDataType::class,
93
                'sections' => SectionDataType::class,
94
                'globalSets' => GlobalSetDataType::class,
95
                'categoryGroups' => CategoryGroupDataType::class,
96
                'tagGroups' => TagGroupDataType::class,
97
                'userGroups' => UserGroupDataType::class,
98
                'userSettings' => UserSettingsDataType::class,
99
                'elementIndexSettings' => ElementIndexDataType::class,
100
            ],
101
        ];
102
103 10
        Craft::configure($this, $config);
104
105 10
        parent::init();
106 10
    }
107
108
    /**
109
     * Get datatype by handle.
110
     *
111
     * @param string $dataTypeHandle
112
     *
113
     * @return DateTypeInterface|null
114
     */
115 4
    public function getDataType(string $dataTypeHandle)
116
    {
117 4
        if (!isset($this->dataTypes[$dataTypeHandle])) {
118 1
            Schematic::error('DataType '.$dataTypeHandle.' is not registered');
119
120 1
            return null;
121
        }
122
123 3
        $dataTypeClass = $this->dataTypes[$dataTypeHandle];
124 3
        if (!class_exists($dataTypeClass)) {
125 1
            Schematic::error('Class '.$dataTypeClass.' does not exist');
126
127 1
            return null;
128
        }
129
130 2
        $dataType = new $dataTypeClass();
131 2
        if (!$dataType instanceof DataTypeInterface) {
132 1
            Schematic::error($dataTypeClass.' does not implement DataTypeInterface');
133
134 1
            return null;
135
        }
136
137 1
        return $dataType;
138
    }
139
140
    /**
141
     * Check mapper handle is valid.
142
     *
143
     * @param string $mapper
144
     *
145
     * @return bool
146
     */
147 3
    public function checkMapper(string $mapper): bool
148
    {
149 3
        if (!isset($this->$mapper)) {
150 1
            Schematic::error('Mapper '.$mapper.' not found');
151
152 1
            return false;
153
        }
154 2
        if (!$this->$mapper instanceof MapperInterface) {
155 1
            Schematic::error(get_class($this->$mapper).' does not implement MapperInterface');
156
157 1
            return false;
158
        }
159
160 1
        return true;
161
    }
162
163
    /**
164
     * Find converter for model class.
165
     *
166
     * @param string $modelClass
167
     *
168
     * @return ConverterInterface|null
169
     */
170 3
    public function getConverter(string $modelClass, string $originalClass = '')
171
    {
172 3
        if ('' === $originalClass) {
173 3
            $originalClass = $modelClass;
174
        }
175
176 3
        $converterClass = 'NerdsAndCompany\\Schematic\\Converters\\'.ucfirst(str_replace('craft\\', '', $modelClass));
177 3
        $event = new ConverterEvent([
178 3
            'modelClass' => $modelClass,
179 3
            'converterClass' => $converterClass,
180
        ]);
181
182 3
        $this->trigger(self::EVENT_RESOLVE_CONVERTER, $event);
183 3
        $converterClass = $event->converterClass;
184
185 3
        if (class_exists($converterClass)) {
186 2
            $converter = new $converterClass();
187 2
            if ($converter instanceof ConverterInterface) {
188 2
                return $converter;
189
            }
190
        }
191
192 2
        $parentClass = get_parent_class($modelClass);
193 2
        if (!$parentClass) {
194 1
            Schematic::error('No converter found for '.$originalClass);
195
196 1
            return null;
197
        }
198
199 2
        return $this->getConverter($parentClass, $originalClass);
200
    }
201
202
    /**
203
     * Is force enabled?
204
     *
205
     * @var bool
206
     */
207
    public static $force = false;
208
209
    /**
210
     * Logs an error message.
211
     *
212
     * @param string|array $message the message to be logged. This can be a simple string or a more
213
     *                              complex data structure, such as array.
214
     */
215 12
    public static function error($message)
216
    {
217 12
        Craft::$app->controller->stdout($message.PHP_EOL, Console::FG_RED);
218 12
    }
219
220
    /**
221
     * Logs a warning message.
222
     *
223
     * @param string|array $message the message to be logged. This can be a simple string or a more
224
     *                              complex data structure, such as array.
225
     */
226 11
    public static function warning($message)
227
    {
228 11
        Craft::$app->controller->stdout($message.PHP_EOL, Console::FG_YELLOW);
229 11
    }
230
231
    /**
232
     * Logs an info message.
233
     *
234
     * @param string|array $message the message to be logged. This can be a simple string or a more
235
     *                              complex data structure, such as array.
236
     */
237 13
    public static function info($message)
238
    {
239 13
        Craft::$app->controller->stdout($message.PHP_EOL);
240 13
    }
241
242
    /**
243
     * Log an import error.
244
     *
245
     * @param Model  $record
246
     * @param string $handle
247
     */
248 7
    public static function importError(Model $record, string $handle)
249
    {
250 7
        static::warning('- Error importing '.get_class($record).' '.$handle);
251 7
        $errors = $record->getErrors();
252 7
        if (!is_array($errors)) {
253 2
            static::error('   - An unknown error has occurred');
254
255 2
            return;
256
        }
257 5
        foreach ($errors as $subErrors) {
258 1
            foreach ($subErrors as $error) {
259 1
                static::error('   - '.$error);
260
            }
261
        }
262 5
    }
263
}
264