Migration_Manager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Manager for handling migrations.
7
 *
8
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
 *
20
 * @author Glynn Quelch <[email protected]>
21
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
22
 * @package PinkCrab\DB_Migration
23
 */
24
25
namespace PinkCrab\DB_Migration;
26
27
use wpdb;
28
use PinkCrab\Table_Builder\Builder;
29
use PinkCrab\DB_Migration\Database_Migration;
30
use PinkCrab\DB_Migration\Log\Migration_Log_Manager;
31
use PinkCrab\Table_Builder\Exception\Engine_Exception;
32
33
class Migration_Manager {
34
35
	/**
36
	 * All Schemas to be migrated/seeded
37
	 *
38
	 * @var array<Database_Migration>
39
	 */
40
	protected $migrations = array();
41
42
	/**
43
	 * The table builder.
44
	 *
45
	 * @var Builder
46
	 */
47
	protected $builder;
48
49
	/**
50
	 * Access to wpdb
51
	 *
52
	 * @var wpdb
53
	 */
54
	protected $wpdb;
55
56
	/**
57
	 * The log keeps for the migrations.
58
	 *
59
	 * @var Migration_Log_Manager
60
	 */
61
	protected $migration_log;
62
63
	public function __construct( Builder $builder, wpdb $wpdb, ?string $migration_log_key = null ) {
64
		$this->builder       = $builder;
65
		$this->wpdb          = $wpdb;
66
		$this->migration_log = new Migration_Log_Manager( $migration_log_key );
67
68
	}
69
70
	/**
71
	 * Returns access to the migration log.
72
	 *
73
	 * @return \PinkCrab\DB_Migration\Log\Migration_Log_Manager
74
	 * @deprecated 1.0.2 Due to a typo
75
	 * @codeCoverageIgnore
76
	 */
77
	public function migation_log(): Migration_Log_Manager {
78
		return $this->migration_log;
79
	}
80
81
	/**
82
	 * Returns access to the migration log.
83
	 *
84
	 * Replaces the migration_log() method which has a typo
85
	 *
86
	 * @return \PinkCrab\DB_Migration\Log\Migration_Log_Manager
87
	 * @since 1.0.2
88
	 */
89
	public function migration_log(): Migration_Log_Manager {
90
		return $this->migration_log;
91
	}
92
93
	/**
94
	 * Adds a migration to the collection.
95
	 *
96
	 * @param  \PinkCrab\DB_Migration\Database_Migration $migration
97
	 * @return self
98
	 */
99
	public function add_migration( Database_Migration $migration ): self {
100
		$this->migrations[ $migration->get_table_name() ] = $migration;
101
		return $this;
102
	}
103
104
	/**
105
	 * Returns all the migrations held in collection.
106
	 *
107
	 * @return array<Database_Migration>
108
	 */
109
	public function get_migrations(): array {
110
		return $this->migrations;
111
	}
112
113
	/**
114
	 * Creates all the tables in collection except those passed as excluded
115
	 *
116
	 * @param string ...$excluded_table Table names to exclude.
117
	 * @return self
118
	 */
119
	public function create_tables( string ...$excluded_table ): self {
120
121
		// Remove excluded tables.
122
		$to_create = array_filter(
123
			$this->migrations,
124
			function( Database_Migration $migration ) use ( $excluded_table ): bool {
125
				return ! in_array( $migration->get_table_name(), $excluded_table, true )
126
				&& $this->migration_log->can_migrate( $migration->get_schema() );
127
			}
128
		);
129
130
		// Upsert Tables.
131
		foreach ( $to_create as $migration ) {
132
			$result = $this->builder->create_table( $migration->get_schema() );
133
			if ( $result === true ) {
134
				$this->migration_log->upsert_migration( $migration->get_schema() );
135
			}
136
		}
137
138
		return $this;
139
140
	}
141
142
	/**
143
	 * Seeds all the tables in collection except those passed as excluded
144
	 *
145
	 * @param string ...$excluded_table Table names to exclude.
146
	 * @return self
147
	 */
148
	public function seed_tables( string ...$excluded_table ): self {
149
150
		// Remove excluded tables.
151
		$to_seed = array_filter(
152
			$this->migrations,
153
			function( Database_Migration $migration ) use ( $excluded_table ): bool {
154
				return ! in_array( $migration->get_table_name(), $excluded_table, true )
155
				&& ! $this->migration_log->is_seeded( $migration->get_schema() );
156
			}
157
		);
158
159
		$seeder = new Migration_Seeder( $this->wpdb );
160
161
		foreach ( $to_seed as $migration ) {
162
			$row = $seeder->seed( $migration->get_schema(), $migration->get_seeds() );
163
			if ( count( $row ) !== 0 ) {
164
				$this->migration_log->mark_table_seeded( $migration->get_schema() );
165
			}
166
		}
167
168
		return $this;
169
	}
170
171
	/**
172
	 * Removes all the tables in collection except those passed as excluded
173
	 *
174
	 * @param string ...$excluded_table Table names to exclude.
175
	 * @return self
176
	 */
177
	public function drop_tables( string ...$excluded_table ): self {
178
		// Remove excluded tables.
179
		$to_seed = array_filter(
180
			$this->migrations,
181
			function( Database_Migration $migration ) use ( $excluded_table ): bool {
182
				return ! in_array( $migration->get_table_name(), $excluded_table, true );
183
			}
184
		);
185
186
		foreach ( $to_seed as $migration ) {
187
188
			try {
189
				$result = $this->builder->drop_table( $migration->get_schema() );
190
			} catch ( Engine_Exception $th ) {
191
				throw Migration_Exception::failed_to_drop_table( $migration->get_schema(), $th->getMessage() );
192
			}
193
194
			// Throw exception if fails.
195
			if ( $result === false ) {
196
				throw Migration_Exception::failed_to_drop_table( $migration->get_schema(), '' );
197
			}
198
199
			// Remove migration from log.
200
			$this->migration_log->remove_migration( $migration->get_schema() );
201
		}
202
203
		return $this;
204
	}
205
}
206