Completed
Push — master ( 05e14c...30f1e1 )
by Maarten
21s queued 12s
created

CreatesMigrations::parseColumnType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Actions\Migration\Traits;
4
5
use Illuminate\Support\Facades\Artisan;
6
use Illuminate\Support\Facades\Cache;
7
use Illuminate\Support\Str;
8
9
trait CreatesMigrations
10
{
11
    public
12
        $autoMigrate,
13
        $filename,
14
        $path;
15
16
    public function __construct()
17
    {
18
        $this->autoMigrate = config('schematics.auto-migrate');
19
        $this->path = database_path('migrations');
20
    }
21
22
    public function __destruct()
23
    {
24
        if ($this->autoMigrate) {
25
            try {
26
                Artisan::call('migrate');
27
            } catch (\Throwable $e) {
28
                Cache::put('schematics-exception', [
29
                    'title' => get_class($e),
30
                    'message' => $e->getMessage(),
31
                ], 1440);
32
            }
33
        }
34
    }
35
36
    /**
37
     * @param $request
38
     * @return string
39
     */
40
    protected function getLocalKey($request): string
41
    {
42
        return empty($request['method']['localKey'])
43
            ? 'id' : $request['method']['localKey'];
44
    }
45
46
    /**
47
     * @param $request
48
     * @return string
49
     */
50
    protected function getForeignKey($request): string
51
    {
52
        return empty($request['method']['foreignKey'])
53
            ? strtolower(
54
                Str::snake(
55
                    substr(strrchr($request['target'], "\\"), 1) . '_id'
56
                )
57
            )
58
            : $request['method']['foreignKey'];
59
    }
60
61
    /**
62
     * @param $fields
63
     * @return array
64
     */
65
    protected function getFields($fields): array
66
    {
67
        return array_map(static function ($field) {
68
            return [
69
                $field['name'] => empty($field['type']) ? 'string|max:255' : $field['type']
70
            ];
71
        }, $fields);
72
    }
73
74
75
    /**
76
     * Parsing column type to migration rule
77
     *
78
     * @param $columnType
79
     * @return string
80
     */
81
    protected function parseColumnType($columnType): string
82
    {
83
        $max = (int)preg_replace('/\D/', '', $columnType);
84
        $type = str_replace(' ', '',
85
            preg_replace(
86
                '/[^a-zA-Z]+/',
87
                '',
88
                explode(' ', $columnType, 2)[0]
89
            )
90
        );
91
92
        $type = key(array_filter([
93
            'date' => [
94
                'date'
95
            ],
96
            'dateTime' => [
97
                'timestamp'
98
            ],
99
            'string' => [
100
                'varchar',
101
                'text'
102
            ],
103
            'integer' => [
104
                'int',
105
                'bigint',
106
                'tinyint'
107
            ],
108
        ], static function($values) use($type) {
109
            return in_array(strtolower($type), $values, true);
110
        }, ARRAY_FILTER_USE_BOTH));
111
112
        if ($max > 0) {
113
            $type .= "|max:{$max}";
114
        }
115
116
        return $type ?? 'string';
117
    }
118
119
}
120