tables_to_exclude_from_drop_on_deactivation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 13
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Deactivation event to be launched on deactivation.
7
 *
8
 * @package PinkCrab\Perique\Migration\Plugin_Lifecycle
9
 * @author Glynn Quelch [email protected]
10
 * @since 0.0.1
11
 */
12
13
namespace PinkCrab\Perique\Migration\Event;
14
15
use PinkCrab\Perique\Migration\Migration;
16
use PinkCrab\DB_Migration\Migration_Manager;
17
use PinkCrab\Plugin_Lifecycle\State_Event\Deactivation as State_Events_Deactivation;
18
19
class Deactivation implements State_Events_Deactivation {
20
21
	/**
22
	 * Holds the current Migration Manager and list of all migrations.
23
	 *
24
	 * @var Migration_Manager
25
	 */
26
	protected Migration_Manager $migration_manager;
27
28
	public function __construct( Migration_Manager $migration_manager ) {
29
		$this->migration_manager = $migration_manager;
30
	}
31
32
	/**
33
	 * Create all table and seed those that allow.
34
	 *
35
	 * @return void
36
	 */
37
	public function run(): void {
38
		$this->drop_tables();
39
	}
40
41
	/**
42
	 * Drop all tables in migration manager
43
	 *
44
	 * @return void
45
	 */
46
	private function drop_tables(): void {
47
		$this->migration_manager->drop_tables(
48
			...$this->tables_to_exclude_from_drop_on_deactivation()
49
		);
50
	}
51
52
	/**
53
	 * Gets a list of all tables which should not be seeded.
54
	 *
55
	 * @return string[] Array of table names.
56
	 */
57
	private function tables_to_exclude_from_drop_on_deactivation(): array {
58
		/** @var Migration[] */
59
		$migrations = $this->migration_manager->get_migrations();
60
61
		return array_values(
62
			array_map(
63
				function( Migration $migration ): string {
64
					return $migration->get_table_name();
65
				},
66
				array_filter(
67
					$migrations,
68
					function( Migration $migration ):bool {
69
						return false === $migration->drop_on_deactivation();
70
					}
71
				)
72
			)
73
		);
74
	}
75
}
76