Completed
Push — master ( 804cbb...e964af )
by Ryan
06:57
created

AssignmentSchema::dropColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
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
/**
9
 * Class AssignmentSchema
10
 *
11
 * @link    http://anomaly.is/streams-platform
12
 * @author  AnomalyLabs, Inc. <[email protected]>
13
 * @author  Ryan Thompson <[email protected]>
14
 * @package Anomaly\Streams\Platform\Assignment
15
 */
16
class AssignmentSchema
17
{
18
19
    /**
20
     * The schema builder object.
21
     *
22
     * @var Builder
23
     */
24
    protected $schema;
25
26
    /**
27
     * Create a new AssignmentSchema instance.
28
     */
29
    public function __construct()
30
    {
31
        $this->schema = app('db')->connection()->getSchemaBuilder();
32
    }
33
34
    /**
35
     * Add a column.
36
     *
37
     * @param                     $table
38
     * @param FieldType           $type
39
     * @param AssignmentInterface $assignment
40
     */
41 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...
42
    {
43
        $schema = $type->getSchema();
44
45
        $this->schema->table(
46
            $table,
47
            function (Blueprint $table) use ($schema, $assignment) {
48
                $schema->addColumn($table, $assignment);
49
            }
50
        );
51
    }
52
53
    /**
54
     * Update a column.
55
     *
56
     * @param                     $table
57
     * @param FieldType           $type
58
     * @param AssignmentInterface $assignment
59
     */
60 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...
61
    {
62
        $schema = $type->getSchema();
63
64
        $this->schema->table(
65
            $table,
66
            function (Blueprint $table) use ($schema, $assignment) {
67
                $schema->updateColumn($table, $assignment);
68
            }
69
        );
70
    }
71
72
    /**
73
     * Rename a column.
74
     *
75
     * @param                     $table
76
     * @param FieldType           $type
77
     * @param AssignmentInterface $assignment
78
     */
79
    public function renameColumn($table, FieldType $type, AssignmentInterface $assignment)
80
    {
81
        $schema = $type->getSchema();
82
        $from   = $assignment->getFieldType(true);
83
84
        if ($from->getColumnName() === $type->getColumnName()) {
85
            return;
86
        }
87
88
        $this->schema->table(
89
            $table,
90
            function (Blueprint $table) use ($schema, $from) {
91
                $schema->renameColumn($table, $from);
92
            }
93
        );
94
    }
95
96
    /**
97
     * Drop a column.
98
     *
99
     * @param           $table
100
     * @param FieldType $type
101
     */
102 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...
103
    {
104
        $schema = $type->getSchema();
105
106
        if (!$this->schema->hasTable($table)) {
107
            return;
108
        }
109
110
        $this->schema->table(
111
            $table,
112
            function (Blueprint $table) use ($schema) {
113
                $schema->dropColumn($table);
114
            }
115
        );
116
    }
117
118
    /**
119
     * Backup a column's data.
120
     *
121
     * @param           $table
122
     * @param FieldType $type
123
     */
124 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...
125
    {
126
        if (!$this->schema->hasTable($table)) {
127
            return;
128
        }
129
130
        $schema = $type->getSchema();
131
132
        $this->schema->table(
133
            $table,
134
            function (Blueprint $table) use ($schema) {
135
                $schema->backupColumn($table);
136
            }
137
        );
138
    }
139
140
    /**
141
     * Restore a column's data.
142
     *
143
     * @param           $table
144
     * @param FieldType $type
145
     */
146 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...
147
    {
148
        if (!$this->schema->hasTable($table)) {
149
            return;
150
        }
151
152
        $schema = $type->getSchema();
153
154
        $this->schema->table(
155
            $table,
156
            function (Blueprint $table) use ($schema) {
157
                $schema->restoreColumn($table);
158
            }
159
        );
160
    }
161
}
162