-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigration_UI.php
More file actions
204 lines (172 loc) · 4.92 KB
/
Migration_UI.php
File metadata and controls
204 lines (172 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* Migration UI Utility.
*
* Centralizes UI logic for migration views (labels, icons, visibility).
*
* @since 0.0.1
*
* @package StellarWP\Migrations\Utilities
*/
namespace StellarWP\Migrations\Utilities;
use StellarWP\Migrations\Contracts\Migration;
use StellarWP\Migrations\Enums\Operation;
use StellarWP\Migrations\Enums\Status;
use StellarWP\Migrations\Models\Execution;
use StellarWP\Migrations\Tables\Migration_Executions;
/**
* Utility for migration admin UI (labels, icons, button visibility).
*
* @since 0.0.1
*
* @package StellarWP\Migrations\Utilities
*/
class Migration_UI {
/**
* The migration instance.
*
* @since 0.0.1
*
* @var Migration
*/
private Migration $migration;
/**
* Constructor.
*
* @since 0.0.1
*
* @param Migration $migration Migration instance.
*/
public function __construct( Migration $migration ) {
$this->migration = $migration;
}
/**
* Returns the display status, used for display purposes only.
*
* @since 0.0.1
*
* @return Status The display status for the migration.
*/
public function get_display_status(): Status {
$status = $this->migration->get_status();
// If the migration is pending and has no items, return NOT_APPLICABLE.
if (
$status->equals( Status::PENDING() )
&& $this->migration->get_total_items() === 0
) {
return Status::NOT_APPLICABLE();
}
// If the migration is reverted but the parent execution is failed, return FAILED.
$parent_execution = $this->get_parent_execution();
if (
$status->equals( Status::REVERTED() )
&& null !== $parent_execution
&& $parent_execution->get_status()->equals( Status::FAILED() )
) {
return Status::FAILED();
}
return $status;
}
/**
* Returns the human-readable label for the display status.
*
* @since 0.0.1
*
* @return string Translated display status label.
*/
public function get_display_status_label(): string {
$parent_execution = $this->get_parent_execution();
$default_label = $this->get_display_status()->get_label();
if ( null === $parent_execution ) {
// No parent execution, so we can use the regular label.
return $default_label;
}
$migration_status = $this->migration->get_status();
$parent_execution_status = $parent_execution->get_status();
// Case of successful auto-revert.
if ( $migration_status->equals( Status::REVERTED() ) && $parent_execution_status->equals( Status::FAILED() ) ) {
return $default_label . __( ' (auto-reverted)', 'stellarwp-migrations' );
}
// Case of failed auto-revert.
if ( $migration_status->equals( Status::FAILED() ) && $parent_execution_status->equals( Status::FAILED() ) ) {
return $default_label . __( ' (auto-revert failed)', 'stellarwp-migrations' );
}
return $default_label;
}
/**
* Returns the run action label for the migration.
*
* @since 0.0.1
*
* @return string Translated label.
*/
public function get_run_action_label(): string {
$status = $this->get_display_status();
if (
$status->equals( Status::COMPLETED() )
|| $status->equals( Status::REVERTED() )
) {
return __( 'Re-run', 'stellarwp-migrations' );
}
if ( $status->equals( Status::FAILED() ) ) {
return __( 'Retry', 'stellarwp-migrations' );
}
return __( 'Start', 'stellarwp-migrations' );
}
/**
* Whether the run button should be shown for the migration.
*
* @since 0.0.1
*
* @return bool True if run button should be shown.
*/
public function show_run(): bool {
$status_value = $this->migration->get_status()->getValue();
$runnable_statuses = [
Status::COMPLETED()->getValue(),
Status::PENDING()->getValue(),
Status::CANCELED()->getValue(),
Status::FAILED()->getValue(),
Status::REVERTED()->getValue(),
];
return $this->migration->is_applicable()
&& $this->migration->can_run()
&& in_array( $status_value, $runnable_statuses, true )
&& $this->migration->get_total_items() > 0;
}
/**
* Whether the rollback button should be shown for the migration.
*
* @since 0.0.1
*
* @return bool True if rollback button should be shown.
*/
public function show_rollback(): bool {
$status_value = $this->migration->get_status()->getValue();
$rollbackable_statuses = [
Status::COMPLETED()->getValue(),
Status::CANCELED()->getValue(),
Status::FAILED()->getValue(),
];
return $this->migration->is_applicable()
&& in_array( $status_value, $rollbackable_statuses, true )
&& $this->migration->get_total_items( Operation::DOWN() ) > 0;
}
/**
* Get the parent execution for the migration.
*
* @since 0.0.1
*
* @return Execution|null The parent execution or null if not found.
*/
private function get_parent_execution(): ?Execution {
$latest_execution = $this->migration->get_latest_execution();
if (
null === $latest_execution
|| null === $latest_execution->get_parent_execution_id()
) {
return null;
}
return Migration_Executions::get_by_id( $latest_execution->get_parent_execution_id() );
}
}