Issues (157)

src/Blocks/MigrationsAbstract.php (3 issues)

1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use SoliDry\Exceptions\AttributesException;
6
use SoliDry\Helpers\Console;
7
use SoliDry\ApiGenerator;
8
use SoliDry\Types\ModelsInterface;
9
use SoliDry\Types\PhpInterface;
10
use SoliDry\Types\ApiInterface;
11
12
/**
13
 * @property ApiGenerator generator
14
 * @property string sourceCode
15
 */
16
abstract class MigrationsAbstract
17
{
18
    use ContentManager, MigrationsTrait;
0 ignored issues
show
The trait SoliDry\Blocks\ContentManager requires some properties which are not provided by SoliDry\Blocks\MigrationsAbstract: $options, $version, $modulesDir, $className
Loading history...
19
20
    public static $counter = 10;
21
22
    public const PATTERN_TIME = 'Y_m_d_Hi';
23
24
    private $signedIntergerMap = [
25
        ModelsInterface::INT_DIGITS_TINY => ModelsInterface::MIGRATION_METHOD_TINY_INTEGER,
26
        ModelsInterface::INT_DIGITS_SMALL => ModelsInterface::MIGRATION_METHOD_SMALL_INTEGER,
27
        ModelsInterface::INT_DIGITS_MEDIUM => ModelsInterface::MIGRATION_METHOD_MEDIUM_INTEGER,
28
        ModelsInterface::INT_DIGITS_INT => ModelsInterface::MIGRATION_METHOD_INTEGER,
29
        ModelsInterface::INT_DIGITS_BIGINT => ModelsInterface::MIGRATION_METHOD_BIG_INTEGER,
30
    ];
31
32
    private $unsignedIntergerMap = [
33
        ModelsInterface::INT_DIGITS_TINY => ModelsInterface::MIGRATION_METHOD_UTINYINT,
34
        ModelsInterface::INT_DIGITS_SMALL => ModelsInterface::MIGRATION_METHOD_USMALLINT,
35
        ModelsInterface::INT_DIGITS_MEDIUM => ModelsInterface::MIGRATION_METHOD_UMEDIUMINT,
36
        ModelsInterface::INT_DIGITS_INT => ModelsInterface::MIGRATION_METHOD_UINT,
37
        ModelsInterface::INT_DIGITS_BIGINT => ModelsInterface::MIGRATION_METHOD_UBIGINT,
38
    ];
39
40
    /**
41
     *  Sets rows of migration with their description and options
42
     */
43
    protected function setRows(): void
44
    {
45
        $attrs = $this->getEntityAttributes();
46
        foreach ($attrs as $attrKey => $attrVal) {
47
            if (is_array($attrVal)) {
48
                if (empty($attrVal[ApiInterface::RAML_TYPE]) === false) {
49
                    $this->setDescription($attrVal);
50
                    $type = $attrVal[ApiInterface::RAML_TYPE];
51
                    // create migration fields depending on Types
52
                    $this->setRowContent($attrVal, $type, $attrKey);
53
                } else {// non-standard Types aka enum
54
                    if (empty($attrVal[ApiInterface::RAML_ENUM]) === false) {
55
                        $this->setRowContent($attrVal, ApiInterface::RAML_ENUM, $attrKey);
56
                    }
57
                }
58
59
                try {
60
                    $this->setIndex($attrVal, $attrKey);
61
                    $this->setCompositeIndex($attrVal);
62
                } catch (AttributesException $e) {
63
                    echo $e->getTraceAsString();
64
                }
65
            }
66
        }
67
        // created_at/updated_at created for every table
68
        $this->setRow(ModelsInterface::MIGRATION_METHOD_TIMESTAMPS, '', null, null, false);
69
    }
70
71
    /**
72
     *  Sets rows of migration with their description and options
73
     * @param array $attrs
74
     */
75
    protected function setAddRows(array $attrs): void
76
    {
77
        foreach ($attrs as $attrKey => $attrVal) {
78
            if (is_array($attrVal)) {
79
                if (empty($attrVal[ApiInterface::RAML_TYPE]) === false) {
80
                    $this->setDescription($attrVal);
81
                    $type = $attrVal[ApiInterface::RAML_TYPE];
82
                    // create migration fields depending on Types
83
                    $this->setRowContent($attrVal, $type, $attrKey);
84
                } else {// non-standard Types aka enum
85
                    if (empty($attrVal[ApiInterface::RAML_ENUM]) === false) {
86
                        $this->setRowContent($attrVal, ApiInterface::RAML_ENUM, $attrKey);
87
                    }
88
                }
89
                try {
90
                    $this->setIndex($attrVal, $attrKey);
91
                    $this->setCompositeIndex($attrVal);
92
                } catch (AttributesException $e) {
93
                    echo $e->getTraceAsString();
94
                }
95
            }
96
        }
97
    }
98
99
    /**
100
     * Sets row content with opts
101
     * @param array $attrVal
102
     * @param string $type
103
     * @param string $attrKey
104
     */
105
    private function setRowContent(array $attrVal, string $type, string $attrKey): void
106
    {
107
        // create migration fields depending on Types
108
        switch ($type) {
109
            case ApiInterface::RAML_TYPE_STRING:
110
                $this->setStringRow($attrKey, $attrVal);
111
                break;
112
            case ApiInterface::RAML_TYPE_INTEGER:
113
                // create an auto_incremented primary key
114
                $this->setIntegerRow($attrKey, $attrVal, $type);
115
                break;
116
            case ApiInterface::RAML_TYPE_BOOLEAN:
117
                $this->setRow(ModelsInterface::MIGRATION_METHOD_UTINYINT, $attrKey);
118
                break;
119
            case ApiInterface::RAML_TYPE_NUMBER:
120
                $this->setNumberRow($attrVal, $attrKey);
121
                break;
122
            case ApiInterface::RAML_ENUM:
123
                $this->setRow(ModelsInterface::MIGRATION_METHOD_ENUM, $attrKey,
124
                    $this->getArrayParam($attrVal[ModelsInterface::MIGRATION_METHOD_ENUM]));
125
                break;
126
            case ApiInterface::RAML_DATE:
127
                $this->setRow(ModelsInterface::MIGRATION_METHOD_DATE, $attrKey);
128
                break;
129
            case ApiInterface::RAML_TIME:
130
                $this->setRow(ModelsInterface::MIGRATION_METHOD_TIME, $attrKey);
131
                break;
132
            case ApiInterface::RAML_DATETIME:
133
                if ($attrKey === ModelsInterface::COLUMN_DEL_AT) {
134
                    $this->setRow(ModelsInterface::MIGRATION_METHOD_SOFT_DEL, '', null, null, false);
135
                } else {
136
                    $this->setRow(ModelsInterface::MIGRATION_METHOD_DATETIME, $attrKey);
137
                }
138
                break;
139
        }
140
    }
141
142
    /**
143
     * @param array $attrs
144
     */
145
    public function dropRows(array $attrs): void
146
    {
147
        foreach ($attrs as $attrKey => $attrVal) {
148
            $this->setRow(ModelsInterface::MIGRATION_DROP_COLUMN, $attrKey);
149
        }
150
    }
151
152
    /**
153
     *  Sets index for particular column if facets was declared
154
     * @param array $attrVal
155
     * @param string $attrKey
156
     *
157
     * @throws AttributesException
158
     */
159
    public function setIndex(array $attrVal, string $attrKey): void
160
    {
161
        if (empty($attrVal[ApiInterface::RAML_FACETS][ApiInterface::RAML_INDEX]) === false) {
162
            $facets = $attrVal[ApiInterface::RAML_FACETS][ApiInterface::RAML_INDEX];
163
            foreach ($facets as $k => $v) {
164
                switch ($v) {
165
                    case ModelsInterface::INDEX_TYPE_INDEX:
166
                        $this->setRow(ModelsInterface::INDEX_TYPE_INDEX, $attrKey, $this->quoteParam($k));
167
                        break;
168
                    case ModelsInterface::INDEX_TYPE_PRIMARY:
169
                        $this->setRow(ModelsInterface::INDEX_TYPE_PRIMARY, $attrKey, $this->quoteParam($k));
170
                        break;
171
                    case ModelsInterface::INDEX_TYPE_UNIQUE:
172
                        $this->setRow(ModelsInterface::INDEX_TYPE_UNIQUE, $attrKey, $this->quoteParam($k));
173
                        break;
174
                    case ModelsInterface::INDEX_TYPE_FOREIGN:
175
                        $this->setForeignIndex($facets, $attrKey, $k);
176
                        break;
177
                }
178
            }
179
        }
180
    }
181
182
    /**
183
     * @param string $key
184
     * @param int $max
185
     * @param bool $signed
186
     */
187
    private function setIntegerDigit(string $key, int $max = null, bool $signed = false): void
0 ignored issues
show
The method setIntegerDigit() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
188
    {
189
        if ($signed) {
190
            foreach ($this->signedIntergerMap as $digits => $method) {
191
                $next = next($this->signedIntergerMap);
192
                if ($digits >= $max && ($next === false || ($next !== false && $max < key($this->signedIntergerMap)))) {
193
                    $this->setRow($method, $key);
194
                    break;
195
                }
196
            }
197
        } else {
198
            foreach ($this->unsignedIntergerMap as $digits => $method) {
199
                $next = next($this->unsignedIntergerMap);
200
                if ($digits >= $max && ($next === false || ($next !== false && $max < key($this->unsignedIntergerMap)))) {
201
                    $this->setRow($method, $key);
202
                    break;
203
                }
204
            }
205
        }
206
    }
207
208
    /**
209
     * @param $relationEntity
210
     */
211
    protected function setPivotRows($relationEntity): void
212
    {
213
        $this->setRow(ModelsInterface::MIGRATION_METHOD_INCREMENTS, ApiInterface::RAML_ID);
214
        $attrs = [
215
            strtolower($this->generator->objectName) . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID => $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ID]],
216
            $relationEntity . PhpInterface::UNDERSCORE . ApiInterface::RAML_ID                          => $this->generator->types[$this->generator->types[ucfirst($relationEntity)][ApiInterface::RAML_PROPS][ApiInterface::RAML_ID]],
217
        ];
218
        foreach ($attrs as $attrKey => $attrVal) {
219
            $this->setRowContent($attrVal, $attrVal[ApiInterface::RAML_TYPE], $attrKey);
220
        }
221
        $this->setRow(ModelsInterface::MIGRATION_METHOD_TIMESTAMPS, '', null, null, false);
222
    }
223
224
    private function getEntityAttributes()
225
    {
226
        $attrsArray = [ApiInterface::RAML_ID => $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ID]]] +
227
            $this->generator->types[$this->generator->objectProps[ApiInterface::RAML_ATTRS]][ApiInterface::RAML_PROPS];
228
229
        return $attrsArray;
230
    }
231
232
    /**
233
     * @param $attrVal
234
     * @param $attrKey
235
     * @param $type
236
     */
237
    private function setId($attrVal, $attrKey, $type): void
0 ignored issues
show
The method setId() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
238
    {
239
        // set incremented id int
240
        if ($type === ApiInterface::RAML_TYPE_INTEGER && empty($attrVal[ApiInterface::RAML_INTEGER_MAX]) === false) {
241
            if ($attrVal[ApiInterface::RAML_INTEGER_MAX] > ModelsInterface::ID_MAX_INCREMENTS) {
242
                $this->setRow(ModelsInterface::MIGRATION_METHOD_BIG_INCREMENTS, $attrKey);
243
244
                return;
245
            }
246
        }
247
        $this->setRow(ModelsInterface::MIGRATION_METHOD_INCREMENTS, $attrKey);
248
    }
249
250
    /**
251
     * Creates migration file with time mask
252
     *
253
     * @param string $migrationName
254
     */
255
    protected function createMigrationFile(string $migrationName): void
256
    {
257
        $migrationMask = date(self::PATTERN_TIME) . self::$counter;
258
259
        self::$counter += 2; // 2-step pace for pivots
260
        $file = $this->generator->formatMigrationsPath() . $migrationMask . PhpInterface::UNDERSCORE .
261
            $migrationName . PhpInterface::PHP_EXT;
262
263
        // if migration file with the same name ocasionally exists we do not override it
264
        $isCreated = FileManager::createFile($file, $this->sourceCode);
265
        if ($isCreated) {
266
            Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN);
267
        }
268
    }
269
}