Completed
Pull Request — master (#24)
by ARCANEDEV
07:54
created

Migration::table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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