Completed
Pull Request — master (#114)
by Bart
08:10
created

Schematic   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 2
dl 0
loc 125
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
D getRecords() 0 35 10
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
8
/**
9
 * Schematic.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2018, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class Schematic
20
{
21
    /**
22
     * The available datatypes.
23
     *
24
     * @TODO: Make data types and processor components configurable.
25
     *
26
     * @var array
27
     */
28
    const DATA_TYPES = [
29
        'sites' => Services\ModelProcessor::class,
30
        'volumes' => Services\ModelProcessor::class,
31
        'assetTransforms' => Services\ModelProcessor::class,
32
        'fields' => Services\ModelProcessor::class,
33
        'plugins' => Services\Plugins::class,
34
        'sections' => Services\ModelProcessor::class,
35
        'globalSets' => Services\ModelProcessor::class,
36
        'userGroups' => Services\ModelProcessor::class,
37
        'users' => Services\Users::class,
38
        'categoryGroups' => Services\ModelProcessor::class,
39
        'tagGroups' => Services\ModelProcessor::class,
40
        'elementIndexSettings' => Services\ElementIndexSettings::class,
41
    ];
42
43
    /**
44
     * Get records for datatype.
45
     *
46
     * @TODO: Make this more dynamic
47
     *
48
     * @param string $datatype
49
     *
50
     * @return Model[]
51
     */
52
    public static function getRecords(string $datatype)
53
    {
54
        $records = [];
55
        switch ($datatype) {
56
            case 'assetTransforms':
57
                $records = Craft::$app->assetTransforms->getAllTransforms();
58
                break;
59
            case 'categoryGroups':
60
                $records = Craft::$app->categories->getAllGroups();
61
                break;
62
            case 'fields':
63
                $records = Craft::$app->fields->getAllFields();
64
                break;
65
            case 'globalSets':
66
                $records = Craft::$app->globals->getAllSets();
67
                break;
68
            case 'sections':
69
                $records = Craft::$app->sections->getAllSections();
70
                break;
71
            case 'sites':
72
                $records = Craft::$app->sites->getAllSites();
73
                break;
74
            case 'userGroups':
75
                $records = Craft::$app->userGroups->getAllGroups();
76
                break;
77
            case 'volumes':
78
                $records = Craft::$app->volumes->getAllVolumes();
79
                break;
80
            case 'tagGroups':
81
                $records = Craft::$app->tags->getAllTagGroups();
82
                break;
83
        }
84
85
        return $records;
86
    }
87
88
    /**
89
     * Is force enabled?
90
     *
91
     * @var bool
92
     */
93
    public static $force = false;
94
95
    /**
96
     * Logs an error message.
97
     *
98
     * @param string|array $message the message to be logged. This can be a simple string or a more
99
     *                              complex data structure, such as array.
100
     */
101
    public static function error($message)
102
    {
103
        Craft::error($message, 'schematic');
104
    }
105
106
    /**
107
     * Logs a warning message.
108
     *
109
     * @param string|array $message the message to be logged. This can be a simple string or a more
110
     *                              complex data structure, such as array.
111
     */
112
    public static function warning($message)
113
    {
114
        Craft::warning($message, 'schematic');
115
    }
116
117
    /**
118
     * Logs an info message.
119
     *
120
     * @param string|array $message the message to be logged. This can be a simple string or a more
121
     *                              complex data structure, such as array.
122
     */
123
    public static function info($message)
124
    {
125
        Craft::info($message, 'schematic');
126
    }
127
128
    /**
129
     * Log an import error.
130
     *
131
     * @param Model  $record
132
     * @param string $handle
133
     */
134
    public static function importError(Model $record, string $handle)
135
    {
136
        static::warning('- Error importing '.get_class($record).' '.$handle);
137
        foreach ($record->getErrors() as $errors) {
138
            foreach ($errors as $error) {
139
                static::error('   - '.$error);
140
            }
141
        }
142
    }
143
}
144