| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Events; |
| 4 |
|
| 5 |
/** |
| 6 |
* Event dispatched when a bulk action completes. |
| 7 |
* |
| 8 |
* This event is fired after a bulk operation on multiple images |
| 9 |
* has completed. Listeners can use this for logging, showing |
| 10 |
* admin notices, or triggering cleanup actions. |
| 11 |
* |
| 12 |
* @package AATXT\App\Events |
| 13 |
*/ |
| 14 |
final class BulkActionCompletedEvent |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Name of the bulk action |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $actionName; |
| 22 |
|
| 23 |
/** |
| 24 |
* Total number of items processed |
| 25 |
* |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
private $totalItems; |
| 29 |
|
| 30 |
/** |
| 31 |
* Number of successfully processed items |
| 32 |
* |
| 33 |
* @var int |
| 34 |
*/ |
| 35 |
private $successCount; |
| 36 |
|
| 37 |
/** |
| 38 |
* Number of failed items |
| 39 |
* |
| 40 |
* @var int |
| 41 |
*/ |
| 42 |
private $failureCount; |
| 43 |
|
| 44 |
/** |
| 45 |
* Timestamp when the event occurred |
| 46 |
* |
| 47 |
* @var int |
| 48 |
*/ |
| 49 |
private $timestamp; |
| 50 |
|
| 51 |
/** |
| 52 |
* IDs of successfully processed items |
| 53 |
* |
| 54 |
* @var array<int> |
| 55 |
*/ |
| 56 |
private $successIds; |
| 57 |
|
| 58 |
/** |
| 59 |
* IDs of failed items |
| 60 |
* |
| 61 |
* @var array<int> |
| 62 |
*/ |
| 63 |
private $failedIds; |
| 64 |
|
| 65 |
/** |
| 66 |
* Constructor |
| 67 |
* |
| 68 |
* @param string $actionName Name of the bulk action |
| 69 |
* @param int $totalItems Total number of items processed |
| 70 |
* @param int $successCount Number of successfully processed items |
| 71 |
* @param array<int> $successIds IDs of successfully processed items |
| 72 |
* @param array<int> $failedIds IDs of failed items |
| 73 |
*/ |
| 74 |
public function __construct( |
| 75 |
string $actionName, |
| 76 |
int $totalItems, |
| 77 |
int $successCount, |
| 78 |
array $successIds = [], |
| 79 |
array $failedIds = [] |
| 80 |
) { |
| 81 |
$this->actionName = $actionName; |
| 82 |
$this->totalItems = $totalItems; |
| 83 |
$this->successCount = $successCount; |
| 84 |
$this->failureCount = $totalItems - $successCount; |
| 85 |
$this->successIds = $successIds; |
| 86 |
$this->failedIds = $failedIds; |
| 87 |
$this->timestamp = time(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get the action name. |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
public function getActionName(): string |
| 96 |
{ |
| 97 |
return $this->actionName; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get the total number of items processed. |
| 102 |
* |
| 103 |
* @return int |
| 104 |
*/ |
| 105 |
public function getTotalItems(): int |
| 106 |
{ |
| 107 |
return $this->totalItems; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get the number of successfully processed items. |
| 112 |
* |
| 113 |
* @return int |
| 114 |
*/ |
| 115 |
public function getSuccessCount(): int |
| 116 |
{ |
| 117 |
return $this->successCount; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get the number of failed items. |
| 122 |
* |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
public function getFailureCount(): int |
| 126 |
{ |
| 127 |
return $this->failureCount; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get the timestamp when the event occurred. |
| 132 |
* |
| 133 |
* @return int Unix timestamp |
| 134 |
*/ |
| 135 |
public function getTimestamp(): int |
| 136 |
{ |
| 137 |
return $this->timestamp; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get the IDs of successfully processed items. |
| 142 |
* |
| 143 |
* @return array<int> |
| 144 |
*/ |
| 145 |
public function getSuccessIds(): array |
| 146 |
{ |
| 147 |
return $this->successIds; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get the IDs of failed items. |
| 152 |
* |
| 153 |
* @return array<int> |
| 154 |
*/ |
| 155 |
public function getFailedIds(): array |
| 156 |
{ |
| 157 |
return $this->failedIds; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Check if all items were processed successfully. |
| 162 |
* |
| 163 |
* @return bool |
| 164 |
*/ |
| 165 |
public function isFullySuccessful(): bool |
| 166 |
{ |
| 167 |
return $this->failureCount === 0; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Check if all items failed. |
| 172 |
* |
| 173 |
* @return bool |
| 174 |
*/ |
| 175 |
public function isFullyFailed(): bool |
| 176 |
{ |
| 177 |
return $this->successCount === 0; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get the success rate as a percentage. |
| 182 |
* |
| 183 |
* @return float Percentage between 0 and 100 |
| 184 |
*/ |
| 185 |
public function getSuccessRate(): float |
| 186 |
{ |
| 187 |
if ($this->totalItems === 0) { |
| 188 |
return 0.0; |
| 189 |
} |
| 190 |
|
| 191 |
return ($this->successCount / $this->totalItems) * 100; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get a summary message for display. |
| 196 |
* |
| 197 |
* @return string |
| 198 |
*/ |
| 199 |
public function getSummaryMessage(): string |
| 200 |
{ |
| 201 |
if ($this->isFullySuccessful()) { |
| 202 |
return sprintf( |
| 203 |
'Successfully processed all %d items.', |
| 204 |
$this->totalItems |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
if ($this->isFullyFailed()) { |
| 209 |
return sprintf( |
| 210 |
'Failed to process all %d items.', |
| 211 |
$this->totalItems |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
return sprintf( |
| 216 |
'Processed %d of %d items successfully (%d failed).', |
| 217 |
$this->successCount, |
| 218 |
$this->totalItems, |
| 219 |
$this->failureCount |
| 220 |
); |
| 221 |
} |
| 222 |
} |
| 223 |
|