Completed
Push — master ( 98a062...441031 )
by Ryan
10:10
created

AssignmentSchema::changeColumn()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 3
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Assignment;
2
3
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
4
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
5
use Illuminate\Database\Schema\Blueprint;
6
use Illuminate\Database\Schema\Builder;
7
8
class AssignmentSchema
9
{
10
11
    /**
12
     * The schema builder object.
13
     *
14
     * @var Builder
15
     */
16
    protected $schema;
17
18
    /**
19
     * Create a new AssignmentSchema instance.
20
     */
21
    public function __construct()
22
    {
23
        $this->schema = app('db')->connection()->getSchemaBuilder();
24
    }
25
26
    /**
27
     * Add a column.
28
     *
29
     * @param                     $table
30
     * @param FieldType           $type
31
     * @param AssignmentInterface $assignment
32
     */
33 View Code Duplication
    public function addColumn($table, FieldType $type, AssignmentInterface $assignment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $schema = $type->getSchema();
36
37
        $this->schema->table(
38
            $table,
39
            function (Blueprint $table) use ($schema, $assignment) {
40
                $schema->addColumn($table, $assignment);
41
            }
42
        );
43
    }
44
45
    /**
46
     * Update a column.
47
     *
48
     * @param                     $table
49
     * @param FieldType           $type
50
     * @param AssignmentInterface $assignment
51
     */
52 View Code Duplication
    public function updateColumn($table, FieldType $type, AssignmentInterface $assignment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $schema = $type->getSchema();
55
56
        $this->schema->table(
57
            $table,
58
            function (Blueprint $table) use ($schema, $assignment) {
59
                $schema->updateColumn($table, $assignment);
60
            }
61
        );
62
    }
63
64
    /**
65
     * Rename a column.
66
     *
67
     * @param                     $table
68
     * @param FieldType           $type
69
     * @param AssignmentInterface $assignment
70
     */
71
    public function renameColumn($table, FieldType $type, AssignmentInterface $assignment)
72
    {
73
        $schema = $type->getSchema();
74
        $from   = $assignment->getFieldType(true);
75
76
        if ($from->getColumnName() === $type->getColumnName()) {
77
            return;
78
        }
79
80
        $this->schema->table(
81
            $table,
82
            function (Blueprint $table) use ($schema, $from) {
83
                $schema->renameColumn($table, $from);
84
            }
85
        );
86
    }
87
88
    /**
89
     * Change a column.
90
     *
91
     * @param                     $table
92
     * @param FieldType           $type
93
     * @param AssignmentInterface $assignment
94
     */
95
    public function changeColumn($table, FieldType $type, AssignmentInterface $assignment)
96
    {
97
        $schema = $type->getSchema();
98
        $from   = $assignment->getFieldType(true);
99
100
        if ($from->getColumnType() === false || $type->getColumnType() === false) {
101
            return;
102
        }
103
104
        if ($from->getColumnType() === $type->getColumnType()) {
105
            return;
106
        }
107
108
        $this->schema->table(
109
            $table,
110
            function (Blueprint $table) use ($schema, $assignment) {
111
                $schema->changeColumn($table, $assignment);
112
            }
113
        );
114
    }
115
116
    /**
117
     * Drop a column.
118
     *
119
     * @param           $table
120
     * @param FieldType $type
121
     */
122 View Code Duplication
    public function dropColumn($table, FieldType $type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        $schema = $type->getSchema();
125
126
        if (!$this->schema->hasTable($table)) {
127
            return;
128
        }
129
130
        $this->schema->table(
131
            $table,
132
            function (Blueprint $table) use ($schema) {
133
                $schema->dropColumn($table);
134
            }
135
        );
136
    }
137
138
    /**
139
     * Backup a column's data.
140
     *
141
     * @param           $table
142
     * @param FieldType $type
143
     */
144 View Code Duplication
    public function backupColumn($table, FieldType $type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        if (!$this->schema->hasTable($table)) {
147
            return;
148
        }
149
150
        $schema = $type->getSchema();
151
152
        $this->schema->table(
153
            $table,
154
            function (Blueprint $table) use ($schema) {
155
                $schema->backupColumn($table);
156
            }
157
        );
158
    }
159
160
    /**
161
     * Restore a column's data.
162
     *
163
     * @param           $table
164
     * @param FieldType $type
165
     */
166 View Code Duplication
    public function restoreColumn($table, FieldType $type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        if (!$this->schema->hasTable($table)) {
169
            return;
170
        }
171
172
        $schema = $type->getSchema();
173
174
        $this->schema->table(
175
            $table,
176
            function (Blueprint $table) use ($schema) {
177
                $schema->restoreColumn($table);
178
            }
179
        );
180
    }
181
}
182