RenameColumn::handle()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\FieldGenerator\Listeners;
4
5
use Illuminate\Support\Facades\Schema;
6
use Hechoenlaravel\JarvisFoundation\FieldGenerator\Events\FieldWasEdited;
7
8
class RenameColumn
9
{
10
    /**
11
     * Once the field has been edited in the DB Record
12
     * The column in the entity table will be edited.
13
     * @param FieldWasCreated $event
14
     */
15
    public function handle(FieldWasEdited $event)
16
    {
17
        $field = $event->field;
18
        $rename = $event->rename;
19
        $oldSlug = $event->oldSlug;
20
        Schema::table($event->field->entity->getTableName(), function ($table) use ($field, $rename, $oldSlug) {
21
            if ($rename) {
22
                $table->renameColumn($oldSlug, $field->slug);
23
            }
24
        });
25
        Schema::table($event->field->entity->getTableName(), function ($table) use ($field) {
26
            $table->{$field->getType()->getColumnType()}($field->slug)->nullable()->default($field->default)->change();
27
        });
28
    }
29
}
30