Completed
Push — master ( 49dddc...95afef )
by Glynn
04:34 queued 04:34
created

Migration_Exception::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 5
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 Exception;
28
use Throwable;
29
use PinkCrab\Table_Builder\Schema;
30
31
class Migration_Exception extends Exception {
32
33
	/**
34
	 * Schema definition
35
	 *
36
	 * @var Schema
37
	 */
38
	protected $schema;
39
40
	/**
41
	 * WPDB Error
42
	 *
43
	 * @var string
44
	 */
45
	protected $wpdb_error;
46
47
	/**
48
	 * Create instance of Migration_Exception
49
	 *
50
	 * @param \PinkCrab\Table_Builder\Schema $schema
51
	 * @param string $wpdb_error
52
	 * @param string $message
53
	 * @param int $code
54
	 * @param Throwable$previous
55
	 */
56
	public function __construct( Schema $schema, string $wpdb_error = '', $message = '', $code = 0, Throwable $previous = null ) {
57
		parent::__construct( $message, $code, $previous );
58
		$this->schema     = $schema;
59
		$this->wpdb_error = $wpdb_error;
60
	}
61
62
63
	/**
64
	 * Exception for column in seed data not existing in schema.
65
	 *
66
	 * @param Schema $schema
67
	 * @param string $column
68
	 * @return Migration_Exception
69
	 * @code 1
70
	 */
71
	public static function seed_column_doesnt_exist( Schema $schema, string $column ): Migration_Exception {
72
		return new Migration_Exception(
73
			$schema,
74
			'',
75
			\sprintf(
76
				'Could not find column %s in %s schema definition',
77
				$column,
78
				$schema->get_table_name()
79
			),
80
			1
81
		);
82
	}
83
84
	/**
85
	 * Exception for failure to insert seed data.
86
	 *
87
	 * @param Schema $schema
88
	 * @param string $wpdb_error
89
	 * @return Migration_Exception
90
	 * @code 2
91
	 */
92
	public static function failed_to_insert_seed( Schema $schema, string $wpdb_error ): Migration_Exception {
93
		return new Migration_Exception(
94
			$schema,
95
			$wpdb_error,
96
			\sprintf( 'Could not insert seed into %s, failed with error: %s', $schema->get_table_name(), $wpdb_error ),
97
			2
98
		);
99
	}
100
101
	/**
102
	 * Exception for failure to drop a table
103
	 *
104
	 * @param Schema $schema
105
	 * @param string $wpdb_error
106
	 * @return Migration_Exception
107
	 * @code 3
108
	 */
109
	public static function failed_to_drop_table( Schema $schema, string $wpdb_error ): Migration_Exception {
110
		return new Migration_Exception(
111
			$schema,
112
			$wpdb_error,
113
			\sprintf( 'Failed to drop %s', $schema->get_table_name() ),
114
			3
115
		);
116
	}
117
118
	/**
119
	 * Get schema definition
120
	 *
121
	 * @return Schema
122
	 */
123
	public function get_schema(): Schema {
124
		return $this->schema;
125
	}
126
127
	/**
128
	 * Get WPDB Error
129
	 *
130
	 * @return string
131
	 */
132
	public function get_wpdb_error(): string {
133
		return $this->wpdb_error;
134
	}
135
}
136