feat: add sync result value object
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* Immutable sync operation result.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Sync;
|
||||
|
||||
final class SyncResult {
|
||||
private bool $successful;
|
||||
private int $created;
|
||||
private int $updated;
|
||||
private int $skipped;
|
||||
private int $conflicts;
|
||||
|
||||
/** @var array<int, string> */
|
||||
private array $errors;
|
||||
|
||||
/**
|
||||
* @param array<int, string> $errors Error messages.
|
||||
*/
|
||||
private function __construct( bool $successful, int $created, int $updated, int $skipped, int $conflicts, array $errors ) {
|
||||
$this->successful = $successful;
|
||||
$this->created = max( 0, $created );
|
||||
$this->updated = max( 0, $updated );
|
||||
$this->skipped = max( 0, $skipped );
|
||||
$this->conflicts = max( 0, $conflicts );
|
||||
$this->errors = array_values( array_map( 'strval', $errors ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, int> $counts Result counts.
|
||||
*/
|
||||
public static function success( array $counts = array() ): self {
|
||||
return new self(
|
||||
true,
|
||||
(int) ( $counts['created'] ?? 0 ),
|
||||
(int) ( $counts['updated'] ?? 0 ),
|
||||
(int) ( $counts['skipped'] ?? 0 ),
|
||||
(int) ( $counts['conflicts'] ?? 0 ),
|
||||
array()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $errors Error messages.
|
||||
*/
|
||||
public static function failure( array $errors ): self {
|
||||
return new self( false, 0, 0, 0, 0, $errors );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, self> $results Results to merge.
|
||||
*/
|
||||
public static function merge( array $results ): self {
|
||||
$successful = true;
|
||||
$created = 0;
|
||||
$updated = 0;
|
||||
$skipped = 0;
|
||||
$conflicts = 0;
|
||||
$errors = array();
|
||||
|
||||
foreach ( $results as $result ) {
|
||||
$successful = $successful && $result->isSuccessful();
|
||||
$created += $result->created();
|
||||
$updated += $result->updated();
|
||||
$skipped += $result->skipped();
|
||||
$conflicts += $result->conflicts();
|
||||
$errors = array_merge( $errors, $result->errors() );
|
||||
}
|
||||
|
||||
return new self( $successful, $created, $updated, $skipped, $conflicts, $errors );
|
||||
}
|
||||
|
||||
public function isSuccessful(): bool {
|
||||
return $this->successful;
|
||||
}
|
||||
|
||||
public function created(): int {
|
||||
return $this->created;
|
||||
}
|
||||
|
||||
public function updated(): int {
|
||||
return $this->updated;
|
||||
}
|
||||
|
||||
public function skipped(): int {
|
||||
return $this->skipped;
|
||||
}
|
||||
|
||||
public function conflicts(): int {
|
||||
return $this->conflicts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function errors(): array {
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(): array {
|
||||
return array(
|
||||
'successful' => $this->successful,
|
||||
'created' => $this->created,
|
||||
'updated' => $this->updated,
|
||||
'skipped' => $this->skipped,
|
||||
'conflicts' => $this->conflicts,
|
||||
'errors' => $this->errors,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user