Completed
Push — master ( 2002ea...255b58 )
by ARCANEDEV
14s
created

Migration::createSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Arcanedev\Support\Database;
2
3
use Closure;
4
use Illuminate\Database\Migrations\Migration as IlluminateMigration;
5
use Illuminate\Support\Facades\Schema;
6
7
/**
8
 * Class     Migration
9
 *
10
 * @package  Arcanedev\Support\Bases
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
abstract class Migration extends IlluminateMigration
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * The table name.
22
     *
23
     * @var string|null
24
     */
25
    protected $table;
26
27
    /**
28
     * The table prefix.
29
     *
30
     * @var string|null
31
     */
32
    protected $prefix;
33
34
    /* -----------------------------------------------------------------
35
     |  Getters & Setters
36
     | -----------------------------------------------------------------
37
     */
38
39
    /**
40
     * Set the migration connection name.
41
     *
42
     * @param  string  $connection
43
     *
44
     * @return self
45
     */
46
    public function setConnection($connection)
47
    {
48
        $this->connection = $connection;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Get the table name.
55
     *
56
     * @return null|string
57
     */
58
    public function getTable()
59
    {
60
        return $this->table;
61
    }
62
63
    /**
64
     * Get the prefixed table name.
65
     *
66
     * @return string
67
     */
68
    public function getTableName()
69
    {
70
        return $this->hasPrefix()
71
             ? $this->getPrefix().$this->getTable()
72
             : $this->getTable();
73
    }
74
75
    /**
76
     * Set the table name.
77
     *
78
     * @param  string  $table
79
     *
80
     * @return self
81
     */
82
    public function setTable($table)
83
    {
84
        $this->table = $table;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Get the prefix name.
91
     *
92
     * @return null|string
93
     */
94
    public function getPrefix()
95
    {
96
        return $this->prefix;
97
    }
98
99
    /**
100
     * Set the prefix name.
101
     *
102
     * @param  string  $prefix
103
     *
104
     * @return self
105
     */
106
    public function setPrefix($prefix)
107
    {
108
        $this->prefix = $prefix;
109
110
        return $this;
111
    }
112
113
    /* -----------------------------------------------------------------
114
     |  Main Methods
115
     | -----------------------------------------------------------------
116
     */
117
118
    /**
119
     * Migrate to database.
120
     */
121
    abstract public function up();
122
123
    /**
124
     * Rollback the migration.
125
     */
126
    public function down()
127
    {
128
        if ( ! $this->hasConnection()) {
129
            Schema::dropIfExists($this->getTableName());
130
131
            return;
132
        }
133
134
        Schema::connection($this->getConnection())->dropIfExists($this->getTableName());
135
    }
136
137
    /**
138
     * Create Table Schema.
139
     *
140
     * @param  \Closure  $blueprint
141
     */
142
    protected function createSchema(Closure $blueprint)
143
    {
144
        if ($this->hasConnection()) {
145
            Schema::connection($this->getConnection())
146
                  ->create($this->getTableName(), $blueprint);
147
        }
148
        else {
149
            Schema::create($this->getTableName(), $blueprint);
150
        }
151
    }
152
153
    /* -----------------------------------------------------------------
154
     |  Check Methods
155
     | -----------------------------------------------------------------
156
     */
157
158
    /**
159
     * Check if connection exists.
160
     *
161
     * @return bool
162
     */
163
    protected function hasConnection()
164
    {
165
        return $this->isNotEmpty($this->getConnection());
166
    }
167
168
    /**
169
     * Check if table has prefix.
170
     *
171
     * @return bool
172
     */
173
    protected function hasPrefix()
174
    {
175
        return $this->isNotEmpty($this->getPrefix());
176
    }
177
178
    /**
179
     * Check if the value is not empty.
180
     *
181
     * @param  string  $value
182
     *
183
     * @return bool
184
     */
185
    private function isNotEmpty($value)
186
    {
187
        return ! (is_null($value) || empty($value));
188
    }
189
}
190