Completed
Push — master ( 76d278...63a119 )
by Rougin
03:46
created

Builder::type()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Rougin\Refinery;
4
5
use Rougin\Describe\Describe;
6
use Rougin\Describe\Column;
7
8
/**
9
 * Builder
10
 *
11
 * @package Refinery
12
 * @author  Rougin Royce Gutib <[email protected]>
13
 */
14
class Builder
15
{
16
    /**
17
     * @var \Rougin\Describe\Column
18
     */
19
    protected $column;
20
21
    /**
22
     * @var \Rougin\Describe\Describe
23
     */
24
    protected $describe;
25
26
    /**
27
     * @var \Rougin\Refinery\Parser
28
     */
29
    protected $parser;
30
31
    /**
32
     * Initializes the column instance.
33
     */
34 9
    public function __construct()
35
    {
36 9
        $column = new Column;
37
38 9
        $column->setDataType('integer');
39
40 9
        $column->setLength(10);
41
42 9
        $column->setAutoIncrement(true);
43
44 9
        $this->column = $column;
45 9
    }
46
47
    /**
48
     * Sets the column instance to be used.
49
     *
50
     * @param  \Rougin\Describe\Column $column
51
     * @return self
52
     */
53 3
    public function column(Column $column)
54
    {
55 3
        $this->column = $column;
56
57 3
        return $this;
58
    }
59
60
    /**
61
     * Sets the Describe instance.
62
     *
63
     * @param  \Rougin\Describe\Describe $describe
64
     * @return self
65
     */
66 3
    public function describe(Describe $describe)
67
    {
68 3
        $this->describe = $describe;
69
70 3
        return $this;
71
    }
72
73
    /**
74
     * Builds the migration file.
75
     *
76
     * @param  string $name
77
     * @return boolean
78
     */
79 9
    public function make($name)
80
    {
81 9
        $this->parser = new Parser((string) $name);
82
83 9
        $migration = __DIR__ . '/Templates/Migration.txt';
84
85 9
        $file = file_get_contents($migration);
86
87 9
        $file = str_replace('$NAME', $name, $file);
88
89 9
        if ($this->describe instanceof Describe) {
90 3
            $file = str_replace('$UP', $this->database(), $file);
91
92 3
            $down = $this->template('DeleteTable');
93
94 3
            return str_replace('$DOWN', (string) $down, $file);
95
        }
96
97 6
        return $this->prepare($file, $this->column);
98
    }
99
100
    /**
101
     * Returns a string value of the specified data type.
102
     *
103
     * @param  boolean|string $value
104
     * @return string
105
     */
106 9
    protected function type($value)
107
    {
108 9
        if (is_string($value) === true) {
109 9
            $value === 'string' && $value = 'varchar';
110
111 9
            return mb_strtoupper((string) $value);
112
        }
113
114 9
        return $value ? 'TRUE' : 'FALSE';
115
    }
116
117
    /**
118
     * Generates code based from the column instance.
119
     *
120
     * @param  \Rougin\Describe\Column $column
121
     * @return string
122
     */
123 9
    protected function create(Column $column, $file)
124
    {
125 9
        $increment = $this->type($column->isAutoIncrement());
126
127 9
        $unsigned = $this->type($column->isUnsigned());
128
129 9
        $file = str_replace('$DEFAULT', $column->getDefaultValue(), $file);
130
131 9
        $file = str_replace('$INCREMENT', $increment, $file);
132
133 9
        $file = str_replace('$LENGTH', $column->getLength(), $file);
134
135 9
        $file = str_replace('$NULL', $this->type($column->isNull()), $file);
136
137 9
        $file = str_replace('$NAME', $column->getField(), $file);
138
139 9
        $file = str_replace('$TYPE', $this->type($column->getDataType()), $file);
140
141 9
        return str_replace('$UNSIGNED', $unsigned, (string) $file);
142
    }
143
144
    /**
145
     * Creates a migration based from a database schema.
146
     *
147
     * @return string
148
     */
149 3
    protected function database()
150
    {
151 3
        $table = (string) $this->parser->table();
152
153 3
        $up = '$this->dbforge->create_table(\'' . $table . '\');';
154
155 3
        $columns = $this->describe->columns($table);
156
157 3
        foreach ($columns as $column) {
158 3
            $create = $this->template('CreateColumn');
159
160 3
            $data = $this->create($column, $create);
161
162 3
            $up = $data . "\r\n\r\n        " . $up;
163 2
        }
164
165 3
        return $up;
166
    }
167
168
    /**
169
     * Prepares the contents of the migration file.
170
     *
171
     * @param  string                  $file
172
     * @param  \Rougin\Describe\Column $column
173
     * @return string
174
     */
175 6
    protected function prepare($file, Column $column)
176
    {
177 6
        $text = $this->parser->column() ? 'Column' : 'Table';
178
179 6
        $up = $this->template('Create' . $text);
180
181 6
        $up = (string) $this->create($column, $up);
182
183 6
        $down = $this->template((string) 'Delete' . $text);
184
185 6
        $delete = array('delete', 'remove');
186
187 6
        $exists = in_array($this->parser->command(), $delete);
188
189 6
        $exists && list($up, $down) = array($down, $up);
190
191 6
        $file = (string) str_replace('$UP', $up, $file);
192
193 6
        return (string) str_replace('$DOWN', $down, $file);
194
    }
195
196
    /**
197
     * Creates a template based on given filename.
198
     *
199
     * @param  string $filename
200
     * @return string
201
     */
202 9
    protected function template($filename)
203
    {
204 9
        $file = __DIR__ . '/Templates/' . $filename . '.txt';
205
206 9
        $file = (string) file_get_contents((string) $file);
207
208 9
        $file = str_replace('$TABLE', $this->parser->table(), $file);
209
210 9
        $column = (string) $this->parser->column();
211
212 9
        $updated = str_replace('$NAME', $column, $file);
213
214 9
        return $column === '' ? $file : $updated;
215
    }
216
}
217