Completed
Pull Request — master (#166)
by Josh
12:20
created

SchemaParser   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 265
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 80.6%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 0
dl 0
loc 265
ccs 54
cts 67
cp 0.806
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSchemas() 0 8 2
A toArray() 0 4 1
A up() 0 4 1
A addRelationColumn() 0 8 1
A addColumn() 0 16 4
A removeColumn() 0 8 2
A getColumn() 0 4 1
A getAttributes() 0 6 2
A hasCustomAttribute() 0 4 1
A getCustomAttribute() 0 4 1
A parse() 0 16 2
A render() 0 10 2
A down() 0 11 2
A createField() 0 14 3
1
<?php
2
3
namespace Nwidart\Modules\Support\Migrations;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
7
class SchemaParser implements Arrayable
8
{
9
    /**
10
     * The array of custom attributes.
11
     *
12
     * @var array
13
     */
14
    protected $customAttributes = [
15
        'remember_token' => 'rememberToken()',
16
        'soft_delete' => 'softDeletes()',
17
    ];
18
19
    /**
20
     * The migration schema.
21
     *
22
     * @var string
23
     */
24
    protected $schema;
25
26
    /**
27
     * The relationship keys.
28
     *
29
     * @var array
30
     */
31
    protected $relationshipKeys = [
32
        'belongsTo',
33
    ];
34
35
    /**
36
     * Create new instance.
37
     *
38
     * @param string|null $schema
39
     */
40 9
    public function __construct($schema = null)
41
    {
42 9
        $this->schema = $schema;
43 9
    }
44
45
    /**
46
     * Parse a string to array of formatted schema.
47
     *
48
     * @param string $schema
49
     *
50
     * @return array
51
     */
52 9
    public function parse($schema)
53
    {
54 9
        $this->schema = $schema;
55
56 9
        $parsed = [];
57
58 9
        foreach ($this->getSchemas() as $schemaArray) {
59 3
            $column = $this->getColumn($schemaArray);
60
61 3
            $attributes = $this->getAttributes($column, $schemaArray);
62
63 3
            $parsed[$column] = $attributes;
64 9
        }
65
66 9
        return $parsed;
67
    }
68
69
    /**
70
     * Get array of schema.
71
     *
72
     * @return array
73
     */
74 9
    public function getSchemas()
75
    {
76 9
        if (is_null($this->schema)) {
77 6
            return [];
78
        }
79
80 3
        return explode(',', str_replace(' ', '', $this->schema));
81
    }
82
83
    /**
84
     * Convert string migration to array.
85
     *
86
     * @return array
87
     */
88 9
    public function toArray()
89
    {
90 9
        return $this->parse($this->schema);
91
    }
92
93
    /**
94
     * Render the migration to formatted script.
95
     *
96
     * @return string
97
     */
98 8
    public function render()
99
    {
100 8
        $results = '';
101
102 8
        foreach ($this->toArray() as $column => $attributes) {
103 2
            $results .= $this->createField($column, $attributes);
104 8
        }
105
106 8
        return $results;
107
    }
108
109
    /**
110
     * Render up migration fields.
111
     *
112
     * @return string
113
     */
114 3
    public function up()
115
    {
116 3
        return $this->render();
117
    }
118
119
    /**
120
     * Render down migration fields.
121
     *
122
     * @return string
123
     */
124 3
    public function down()
125
    {
126 3
        $results = '';
127
128 3
        foreach ($this->toArray() as $column => $attributes) {
129 1
            $attributes = [head($attributes)];
130 1
            $results .= $this->createField($column, $attributes, 'remove');
131 3
        }
132
133 3
        return $results;
134
    }
135
136
    /**
137
     * Create field.
138
     *
139
     * @param string $column
140
     * @param array  $attributes
141
     *
142
     * @return string
143
     */
144 3
    public function createField($column, $attributes, $type = 'add')
145
    {
146 3
        $results = "\t\t\t" . '$table';
147
148 3
        foreach ($attributes as $key => $field) {
149 3
            if (in_array($column, $this->relationshipKeys)) {
150
                $results .= $this->addRelationColumn($key, $field, $column);
151
            } else {
152 3
                $results .= $this->{"{$type}Column"}($key, $field, $column);
153
            }
154 3
        }
155
156 3
        return $results .= ';' . PHP_EOL;
157
    }
158
159
    /**
160
     * Add relation column.
161
     *
162
     * @param int    $key
163
     * @param string $field
164
     * @param string $column
165
     *
166
     * @return string
167
     */
168
    protected function addRelationColumn($key, $field, $column)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $column is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
169
    {
170
        $relatedColumn = snake_case(class_basename($field)) . '_id';
171
172
        $method = 'integer';
173
174
        return "->{$method}('{$relatedColumn}')";
175
    }
176
177
    /**
178
     * Format field to script.
179
     *
180
     * @param int    $key
181
     * @param string $field
182
     * @param string $column
183
     *
184
     * @return string
185
     */
186 2
    protected function addColumn($key, $field, $column)
187
    {
188 2
        if ($this->hasCustomAttribute($column)) {
189
            return '->' . $field;
190
        }
191
192 2
        if ($key == 0) {
193 2
            return '->' . $field . "('" . $column . "')";
194
        }
195
196
        if (str_contains($field, '(')) {
197
            return '->' . $field;
198
        }
199
200
        return '->' . $field . '()';
201
    }
202
203
    /**
204
     * Format field to script.
205
     *
206
     * @param int    $key
207
     * @param string $field
208
     * @param string $column
209
     *
210
     * @return string
211
     */
212 1
    protected function removeColumn($key, $field, $column)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
213
    {
214 1
        if ($this->hasCustomAttribute($column)) {
215
            return '->' . $field;
216
        }
217
218 1
        return '->dropColumn(' . "'" . $column . "')";
219
    }
220
221
    /**
222
     * Get column name from schema.
223
     *
224
     * @param string $schema
225
     *
226
     * @return string
227
     */
228 3
    public function getColumn($schema)
229
    {
230 3
        return array_get(explode(':', $schema), 0);
231
    }
232
233
    /**
234
     * Get column attributes.
235
     *
236
     * @param string $column
237
     * @param string $schema
238
     *
239
     * @return array
240
     */
241 3
    public function getAttributes($column, $schema)
242
    {
243 3
        $fields = str_replace($column . ':', '', $schema);
244
245 3
        return $this->hasCustomAttribute($column) ? $this->getCustomAttribute($column) : explode(':', $fields);
246
    }
247
248
    /**
249
     * Determine whether the given column is exist in customAttributes array.
250
     *
251
     * @param string $column
252
     *
253
     * @return bool
254
     */
255 3
    public function hasCustomAttribute($column)
256
    {
257 3
        return array_key_exists($column, $this->customAttributes);
258
    }
259
260
    /**
261
     * Get custom attributes value.
262
     *
263
     * @param string $column
264
     *
265
     * @return array
266
     */
267
    public function getCustomAttribute($column)
268
    {
269
        return (array) $this->customAttributes[$column];
270
    }
271
}
272