Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
44 / 44 |
|
100.00% |
17 / 17 |
CRAP | |
100.00% |
1 / 1 |
| MigrationResult | |
100.00% |
44 / 44 |
|
100.00% |
17 / 17 |
18 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDescription | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSuccess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setSuccess | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getErrorMessage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setErrorMessage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getExecutionTimeMs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setExecutionTimeMs | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isDryRun | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setDryRun | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addOperationResult | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| getOperationResults | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSuccessCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFailureCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOperationCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Result of a single migration execution. |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2023-2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2026-01-25 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Setup\Migration; |
| 21 | |
| 22 | use phpMyFAQ\Setup\Migration\Operations\OperationInterface; |
| 23 | |
| 24 | class MigrationResult |
| 25 | { |
| 26 | /** @var array<int, array{operation: OperationInterface, success: bool, error: string|null}> */ |
| 27 | private array $operationResults = []; |
| 28 | |
| 29 | private bool $success = true; |
| 30 | private ?string $errorMessage = null; |
| 31 | private float $executionTimeMs = 0; |
| 32 | private bool $dryRun = false; |
| 33 | |
| 34 | public function __construct( |
| 35 | private readonly string $version, |
| 36 | private readonly string $description, |
| 37 | ) { |
| 38 | } |
| 39 | |
| 40 | public function getVersion(): string |
| 41 | { |
| 42 | return $this->version; |
| 43 | } |
| 44 | |
| 45 | public function getDescription(): string |
| 46 | { |
| 47 | return $this->description; |
| 48 | } |
| 49 | |
| 50 | public function isSuccess(): bool |
| 51 | { |
| 52 | return $this->success; |
| 53 | } |
| 54 | |
| 55 | public function setSuccess(bool $success): self |
| 56 | { |
| 57 | $this->success = $success; |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | public function getErrorMessage(): ?string |
| 62 | { |
| 63 | return $this->errorMessage; |
| 64 | } |
| 65 | |
| 66 | public function setErrorMessage(?string $errorMessage): self |
| 67 | { |
| 68 | $this->errorMessage = $errorMessage; |
| 69 | return $this; |
| 70 | } |
| 71 | |
| 72 | public function getExecutionTimeMs(): float |
| 73 | { |
| 74 | return $this->executionTimeMs; |
| 75 | } |
| 76 | |
| 77 | public function setExecutionTimeMs(float $executionTimeMs): self |
| 78 | { |
| 79 | $this->executionTimeMs = $executionTimeMs; |
| 80 | return $this; |
| 81 | } |
| 82 | |
| 83 | public function isDryRun(): bool |
| 84 | { |
| 85 | return $this->dryRun; |
| 86 | } |
| 87 | |
| 88 | public function setDryRun(bool $dryRun): self |
| 89 | { |
| 90 | $this->dryRun = $dryRun; |
| 91 | return $this; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Adds an operation result. |
| 96 | */ |
| 97 | public function addOperationResult(OperationInterface $operation, bool $success, ?string $error = null): self |
| 98 | { |
| 99 | $this->operationResults[] = [ |
| 100 | 'operation' => $operation, |
| 101 | 'success' => $success, |
| 102 | 'error' => $error, |
| 103 | ]; |
| 104 | if (!$success) { |
| 105 | $this->success = false; |
| 106 | } |
| 107 | return $this; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns all operation results. |
| 112 | * |
| 113 | * @return array<int, array{operation: OperationInterface, success: bool, error: string|null}> |
| 114 | */ |
| 115 | public function getOperationResults(): array |
| 116 | { |
| 117 | return $this->operationResults; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns the count of successful operations. |
| 122 | */ |
| 123 | public function getSuccessCount(): int |
| 124 | { |
| 125 | return count(array_filter($this->operationResults, static fn($r) => $r['success'])); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns the count of failed operations. |
| 130 | */ |
| 131 | public function getFailureCount(): int |
| 132 | { |
| 133 | return count(array_filter($this->operationResults, static fn($r) => !$r['success'])); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the total operation count. |
| 138 | */ |
| 139 | public function getOperationCount(): int |
| 140 | { |
| 141 | return count($this->operationResults); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Converts the result to an array for serialization. |
| 146 | * |
| 147 | * @return array<string, mixed> |
| 148 | */ |
| 149 | public function toArray(): array |
| 150 | { |
| 151 | return [ |
| 152 | 'version' => $this->version, |
| 153 | 'description' => $this->description, |
| 154 | 'success' => $this->success, |
| 155 | 'dryRun' => $this->dryRun, |
| 156 | 'errorMessage' => $this->errorMessage, |
| 157 | 'executionTimeMs' => $this->executionTimeMs, |
| 158 | 'operationCount' => $this->getOperationCount(), |
| 159 | 'successCount' => $this->getSuccessCount(), |
| 160 | 'failureCount' => $this->getFailureCount(), |
| 161 | 'operations' => array_map(static fn($r) => [ |
| 162 | 'type' => $r['operation']->getType(), |
| 163 | 'description' => $r['operation']->getDescription(), |
| 164 | 'success' => $r['success'], |
| 165 | 'error' => $r['error'], |
| 166 | ], $this->operationResults), |
| 167 | ]; |
| 168 | } |
| 169 | } |