Completed
Push — master ( a67416...e43114 )
by ARCANEDEV
04:21
created

Migration::setConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php namespace Arcanedev\Support\Bases;
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
     * 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
     * Set the migration connection name.
39
     *
40
     * @param  string  $connection
41
     *
42
     * @return self
43
     */
44
    public function setConnection($connection)
45
    {
46
        $this->connection = $connection;
47
48
        return $this;
49
    }
50
51
    /**
52
     * Get the table name.
53
     *
54
     * @return null|string
55
     */
56
    public function getTable()
57
    {
58
        return $this->table;
59
    }
60
61
    /**
62
     * Get the prefixed table name.
63
     *
64
     * @return string
65
     */
66
    public function getTableName()
67
    {
68
        return $this->hasPrefix()
69
            ? $this->getPrefix() . $this->getTable()
70
            : $this->getTable();
71
    }
72
73
    /**
74
     * Set the table name.
75
     *
76
     * @param  string  $table
77
     *
78
     * @return self
79
     */
80
    public function setTable($table)
81
    {
82
        $this->table = $table;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Get the prefix name.
89
     *
90
     * @return null|string
91
     */
92
    public function getPrefix()
93
    {
94
        return $this->prefix;
95
    }
96
97
    /**
98
     * Set the prefix name.
99
     *
100
     * @param  string  $prefix
101
     *
102
     * @return self
103
     */
104
    public function setPrefix($prefix)
105
    {
106
        $this->prefix = $prefix;
107
108
        return $this;
109
    }
110
111
    /* ------------------------------------------------------------------------------------------------
112
     |  Main Functions
113
     | ------------------------------------------------------------------------------------------------
114
     */
115
    /**
116
     * Migrate to database.
117
     */
118
    abstract public function up();
119
120
    /**
121
     * Rollback the migration.
122
     */
123
    public function down()
124
    {
125
        if ( ! $this->hasConnection()) {
126
            Schema::dropIfExists($this->getTableName());
127
128
            return;
129
        }
130
131
        Schema::connection($this->getConnection())->dropIfExists($this->getTableName());
132
    }
133
134
    /**
135
     * Create Table Schema.
136
     *
137
     * @param  \Closure  $blueprint
138
     */
139
    protected function createSchema(Closure $blueprint)
140
    {
141
        if ($this->hasConnection()) {
142
            Schema::connection($this->getConnection())
143
                  ->create($this->getTableName(), $blueprint);
144
        }
145
        else {
146
            Schema::create($this->getTableName(), $blueprint);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Support\Facades\Schema. Did you maybe mean createFreshMockInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
147
        }
148
    }
149
150
    /* ------------------------------------------------------------------------------------------------
151
     |  Check Functions
152
     | ------------------------------------------------------------------------------------------------
153
     */
154
    /**
155
     * Check if connection exists.
156
     *
157
     * @return bool
158
     */
159
    protected function hasConnection()
160
    {
161
        return $this->isNotEmpty($this->getConnection());
162
    }
163
164
    /**
165
     * Check if table has prefix.
166
     *
167
     * @return bool
168
     */
169
    protected function hasPrefix()
170
    {
171
        return $this->isNotEmpty($this->getPrefix());
172
    }
173
174
    /**
175
     * Check if the value is not empty.
176
     *
177
     * @param  string  $value
178
     *
179
     * @return bool
180
     */
181
    private function isNotEmpty($value)
182
    {
183
        return ! (is_null($value) || empty($value));
184
    }
185
}
186