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

Schematic::warning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
48
     *
49
     * @param string $datatype
50
     *
51
     * @return Model[]
52
     */
53
    public static function getRecords(string $datatype)
54
    {
55
        $records = [];
56
        switch ($datatype) {
57
            case 'assetTransforms':
58
                $records = Craft::$app->assetTransforms->getAllTransforms();
59
                break;
60
            case 'categoryGroups':
61
                $records = Craft::$app->categories->getAllGroups();
62
                break;
63
            case 'fields':
64
                $records = Craft::$app->fields->getAllFields();
65
                break;
66
            case 'globalSets':
67
                $records = Craft::$app->globals->getAllSets();
68
                break;
69
            case 'sections':
70
                $records = Craft::$app->sections->getAllSections();
71
                break;
72
            case 'sites':
73
                $records = Craft::$app->sites->getAllSites();
74
                break;
75
            case 'userGroups':
76
                $records = Craft::$app->userGroups->getAllGroups();
77
                break;
78
            case 'volumes':
79
                $records = Craft::$app->volumes->getAllVolumes();
80
                break;
81
            case 'tagGroups':
82
                $records = Craft::$app->tags->getAllTagGroups();
83
                break;
84
        }
85
86
        return $records;
87
    }
88
89
    /**
90
     * Is force enabled?
91
     *
92
     * @var bool
93
     */
94
    public static $force = false;
95
96
    /**
97
     * Logs an error message.
98
     *
99
     * @param string|array $message the message to be logged. This can be a simple string or a more
100
     *                              complex data structure, such as array.
101
     */
102
    public static function error($message)
103
    {
104
        Craft::error($message, 'schematic');
105
    }
106
107
    /**
108
     * Logs a warning message.
109
     *
110
     * @param string|array $message the message to be logged. This can be a simple string or a more
111
     *                              complex data structure, such as array.
112
     */
113
    public static function warning($message)
114
    {
115
        Craft::warning($message, 'schematic');
116
    }
117
118
    /**
119
     * Logs an info message.
120
     *
121
     * @param string|array $message the message to be logged. This can be a simple string or a more
122
     *                              complex data structure, such as array.
123
     */
124
    public static function info($message)
125
    {
126
        Craft::info($message, 'schematic');
127
    }
128
129
    /**
130
     * Log an import error.
131
     *
132
     * @param Model  $record
133
     * @param string $handle
134
     */
135
    public static function importError(Model $record, string $handle)
136
    {
137
        static::warning('- Error importing '.get_class($record).' '.$handle);
138
        foreach ($record->getErrors() as $errors) {
139
            foreach ($errors as $error) {
140
                static::error('   - '.$error);
141
            }
142
        }
143
    }
144
}
145