Completed
Push — master ( cb1113...b09b3a )
by Maarten
16s queued 11s
created

CreatesMigrations::getForeignKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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
        switch (strtolower($type)) {
93
            case 'date':
94
                $type = 'date';
95
                break;
96
            case 'timestamp':
97
                $type = 'dateTime';
98
                break;
99
            case 'int':
100
            case 'tinyint':
101
            case 'bigint':
102
                $type = 'integer';
103
                break;
104
            case 'varchar':
105
            case 'text':
106
            default:
107
                $type = 'string';
108
        }
109
110
        if ($max > 0) {
111
            $type .= "|max:{$max}";
112
        }
113
114
        return $type;
115
    }
116
117
}
118