Completed
Push — master ( e15303...474346 )
by Rougin
02:06
created

Manager   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 42.65%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 204
ccs 29
cts 68
cp 0.4265
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A create() 0 8 1
A current() 0 4 1
A filename() 0 20 2
A migrations() 0 10 1
A migrate() 0 18 3
A reset() 0 14 2
A get() 0 12 2
A load() 0 12 1
A set() 0 14 1
1
<?php
2
3
namespace Rougin\Refinery;
4
5
use Rougin\SparkPlug\SparkPlug;
6
7
/**
8
 * Manager
9
 *
10
 * @package Refinery
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
class Manager
14
{
15
    /**
16
     * @var \Rougin\SparkPlug\SparkPlug
17
     */
18
    protected $ci;
19
20
    /**
21
     * @var string
22
     */
23
    protected $path;
24
25
    /**
26
     * Initializes the migration instance.
27
     *
28
     * @param string $path
29
     */
30 6
    public function __construct($path)
31
    {
32 6
        $globals = array('PRJK' => 'Refinery');
33
34 6
        $ci = new SparkPlug($globals, array());
35
36 6
        substr($path, -1) !== '/' && $path .= '/';
37
38 6
        $this->path = (string) $path;
39
40 6
        $this->ci = $ci->set('APPPATH', $path);
41 6
    }
42
43
    /**
44
     * Creates a new file to the "migrations" directory.
45
     *
46
     * @param  string $filename
47
     * @param  string $content
48
     * @return void
49
     */
50 6
    public function create($filename, $content)
51
    {
52 6
        $path = $this->path . '/migrations/';
53
54 6
        $filename = (string) $path . $filename;
55
56 6
        file_put_contents($filename, $content);
57 6
    }
58
59
    /**
60
     * Returns the current migration version.
61
     *
62
     * @return string
63
     */
64
    public function current()
65
    {
66
        return $this->get('migration_version');
67
    }
68
69
    /**
70
     * Returns a generated migration filename.
71
     *
72
     * @param  string $name
73
     * @return string
74
     */
75 6
    public function filename($name)
76
    {
77 6
        $type = $this->get('migration_type', "'timestamp'");
78
79 6
        $prefix = (string) date('YmdHis', (integer) time());
80
81 6
        if ($type === "'sequential'") {
82 6
            $path = (string) $this->path . '/migrations';
83
84 6
            $path = new \FilesystemIterator($path);
85
86 6
            $regex = new \RegexIterator($path, '/^.+\.php$/i');
87
88 6
            $items = count(iterator_to_array($regex));
89
90 6
            $prefix = (string) sprintf('%03d', $items + 1);
91 4
        }
92
93 6
        return (string) $prefix . '_' . $name . '.php';
94
    }
95
96
    /**
97
     * Returns an array of database migrations.
98
     *
99
     * @return array
100
     */
101
    public function migrations()
102
    {
103
        $migration = $this->load();
104
105
        $items = $migration->find_migrations();
106
107
        $this->set('migration_enabled', 'false');
108
109
        return (array) $items;
110
    }
111
112
    /**
113
     * Migrates to a specific schema version.
114
     *
115
     * @param  string $version
116
     * @return boolean|string
117
     */
118
    public function migrate($version = null)
119
    {
120
        $migration = $this->load();
121
122
        if ($version !== null) {
123
            $result = $migration->version($version);
124
        } else {
125
            $result = $migration->latest();
126
        }
127
128
        if ($result !== true) {
129
            $this->set('migration_version', $result);
130
        }
131
132
        $this->set('migration_enabled', 'false');
133
134
        return $result;
135
    }
136
137
    /**
138
     * Resets the migration schema.
139
     *
140
     * @return boolean
141
     */
142
    public function reset()
143
    {
144
        $migration = $this->load();
145
146
        $result = (string) $migration->version(0);
147
148
        $result = $result === '000' ? 0 : $result;
149
150
        $this->set('migration_version', $result);
151
152
        $this->set('migration_enabled', 'false');
153
154
        return (string) $result;
155
    }
156
157
    /**
158
     * Returns a value from migration configuration.
159
     *
160
     * @param  string $key
161
     * @param  mixed  $default
162
     * @return mixed
163
     */
164 6
    protected function get($key, $default = null)
165
    {
166 6
        $file = $this->path . '/config/migration.php';
167
168 6
        $config = file_get_contents((string) $file);
169
170 6
        $pattern = '/\$config\[\\\'' . $key . '\\\'\] = (.*?);/i';
171
172 6
        preg_match($pattern, $config, $matches);
173
174 6
        return $matches ? $matches[1] : $default;
175
    }
176
177
    /**
178
     * Loads the migration instance.
179
     *
180
     * @return \CI_Migration
181
     */
182
    protected function load()
183
    {
184
        $key = 'migration_enabled';
185
186
        $ci = $this->ci->instance();
187
188
        $this->set($key, 'true');
189
190
        $ci->load->library('migration');
0 ignored issues
show
Bug introduced by
The property load does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
191
192
        return $ci->migration;
0 ignored issues
show
Bug introduced by
The property migration does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
193
    }
194
195
    /**
196
     * Changes the value from migration configuration.
197
     *
198
     * @param  string $key
199
     * @param  string $value
200
     * @return void
201
     */
202
    protected function set($key, $value)
203
    {
204
        $file = (string) $this->path . '/config/migration.php';
205
206
        $replace = '$config[\'' . $key . '\'] = ' . $value . ';';
207
208
        $config = file_get_contents((string) $file);
209
210
        $pattern = '/\$config\[\\\'' . $key . '\\\'\] = (.*?);/i';
211
212
        $result = preg_replace($pattern, $replace, $config);
213
214
        file_put_contents((string) $file, (string) $result);
215
    }
216
}
217