WriteAllDataObjects   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 21
eloc 105
c 5
b 0
f 0
dl 0
loc 182
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B performMigration() 0 48 7
C getExampleValue() 0 58 14
1
<?php
2
3
namespace Sunnysideup\MigrateData\Tasks;
4
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\ORM\DataObject;
7
8
class WriteAllDataObjects extends MigrateDataTaskBase
9
{
10
    protected $title = 'Creates one of each DataObjects and then deletes it again';
11
12
    protected $description = 'Use this task to find errors in your setup';
13
14
    protected $enabled = true;
15
16
    protected $listOfFieldTypesRaw = [];
17
18
    protected $listOfFieldTypesClean = [];
19
20
    /**
21
     * example:
22
     *      [
23
     *          'MySensitiveGuy',
24
     *          'MySensitiveGal',
25
     *      ].
26
     *
27
     * @var array
28
     */
29
    private static $objects_to_ignore_for_creation = [
30
        DataObject::class,
31
    ];
32
33
    /**
34
     * example:
35
     *      [
36
     *          'MySensitiveGuy',
37
     *          'MySensitiveGal',
38
     *      ].
39
     *
40
     * @var array
41
     */
42
    private static $objects_to_ignore_for_updates = [
43
        DataObject::class,
44
    ];
45
46
    /**
47
     * example:
48
     *      [
49
     *          'MySensitiveGuy',
50
     *          'MySensitiveGal',
51
     *      ].
52
     *
53
     * @var array
54
     */
55
    private static $fields_to_ignore_for_updates = [
56
        'ID',
57
        'Created',
58
        'LastEdited',
59
        'TempIDHash',
60
        'TempIDExpired',
61
        'Password',
62
        'AutoLoginHash',
63
        'AutoLoginExpired',
64
        'PasswordEncryption',
65
        'Salt',
66
        'ExtraMeta', //causes error, not sure why ...
67
    ];
68
69
    /**
70
     * example:
71
     *      [
72
     *          'MySensitiveGuyTwo' =>
73
     *          [
74
     *              'Title' => 'AAA',
75
     *          ]
76
     *      ].
77
     *
78
     * @var array
79
     */
80
    private static $required_defaults = [];
81
82
    public function getExampleValue($obj, $name, $type)
83
    {
84
        $value = null;
85
        $ignoreList = $this->Config()->get('fields_to_ignore_for_updates');
86
        if (isset($this->listOfFieldTypesRaw[$type])) {
87
            ++$this->listOfFieldTypesRaw[$type];
88
        } else {
89
            $this->listOfFieldTypesRaw[$type] = 1;
90
        }
91
        if (in_array($name, $ignoreList, true)) {
92
            $this->flushNow('... ... SKIPPING ' . $name);
93
        } else {
94
            $typeArray = explode('(', $type);
95
            $realType = $typeArray[0];
96
97
            switch ($realType) {
98
                case 'Varchar':
99
                case 'Text':
100
                    $value = 'TestValue';
101
102
                    break;
103
                case 'Boolean':
104
                    $value = 1;
105
106
                    break;
107
                case 'HTMLText':
108
                case 'HTMLFragment':
109
                    $value = '<p>Hello World';
110
111
                    break;
112
                case 'Int':
113
                    $value = 2;
114
115
                    break;
116
                case 'Enum':
117
                    $values = $obj->dbObject($name)->enumValues(false);
118
                    $value = key($values);
119
120
                    break;
121
                case 'DBFile':
122
                    break;
123
                case 'Datetime':
124
                    $value = date('Y-m-d h:i:s');
125
126
                    break;
127
                case 'Date':
128
                    $value = date('Y-m-d');
129
130
                    break;
131
            }
132
            if (isset($this->listOfFieldTypesClean[$realType])) {
133
                ++$this->listOfFieldTypesClean[$realType];
134
            } else {
135
                $this->listOfFieldTypesClean[$realType] = 1;
136
            }
137
        }
138
139
        return $value;
140
    }
141
142
    protected function performMigration()
143
    {
144
        $ignoreForCreationArray = $this->Config()->get('objects_to_ignore_for_creation');
145
        $ignoreForUpdatesArray = $this->Config()->get('objects_to_ignore_for_updates');
146
        $defaults = $this->Config()->get('objects_to_ignore');
147
148
        //make a list of all classes
149
        $objectClassNames = ClassInfo::subclassesFor(DataObject::class);
150
        $this->flushNow(' ');
151
        $this->flushNowLine();
152
        $this->flushNow('FOUND ' . count($objectClassNames) . ' classes');
153
        $this->flushNowLine();
154
        $this->flushNowLine();
155
        foreach ($objectClassNames as $objectClassName) {
156
            $this->flushNowLine();
157
            $this->flushNow($objectClassName . ': ');
158
            $this->flushNowLine();
159
            if (in_array($objectClassName, $ignoreForCreationArray, true)) {
160
                $this->flushNow('... IGNORING ');
161
            } else {
162
                $defaultFields = isset($defaults[$objectClassName]) ?
163
                    $defaults[$objectClassName] : [];
164
                $this->flushNow('... CREATING ');
165
                $obj = $objectClassName::create($defaultFields);
166
                $this->flushNow('... WRITING ');
167
                $obj->write();
168
                $fields = $obj->Config()->get('db');
169
                if (in_array($objectClassName, $ignoreForUpdatesArray, true)) {
170
                    $this->flushNow('... IGNORING UPDATE');
171
                } else {
172
                    $this->flushNow('... UPDATING ');
173
                    foreach ($fields as $name => $type) {
174
                        $value = $this->getExampleValue($obj, $name, $type);
175
                        if (null !== $value) {
176
                            $obj->{$name} = $value;
177
                        }
178
                    }
179
                    $obj->write();
180
                }
181
                $this->flushNow('... DELETING ');
182
                $obj->delete();
183
            }
184
        }
185
        $this->flushNowLine();
186
        $this->flushNow(print_r($this->listOfFieldTypesRaw, 1));
0 ignored issues
show
Bug introduced by
It seems like print_r($this->listOfFieldTypesRaw, 1) can also be of type true; however, parameter $message of Sunnysideup\MigrateData\...ataTaskBase::flushNow() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
        $this->flushNow(/** @scrutinizer ignore-type */ print_r($this->listOfFieldTypesRaw, 1));
Loading history...
187
        $this->flushNowLine();
188
        $this->flushNow(print_r($this->listOfFieldTypesClean, 1));
189
        $this->flushNowLine();
190
    }
191
}
192