Completed
Push — master ( 63772a...99c307 )
by
unknown
16s
created

Schematic::checkMapper()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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