| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Bulk_Editor\Domain\Updates; |
| 5 |
|
| 6 |
/** |
| 7 |
* This class describes the outcome of a single post update. |
| 8 |
*/ |
| 9 |
class Update_Result { |
| 10 |
|
| 11 |
/** |
| 12 |
* The ID of the post the result is for. |
| 13 |
* |
| 14 |
* @var int |
| 15 |
*/ |
| 16 |
private $post_id; |
| 17 |
|
| 18 |
/** |
| 19 |
* Whether the update succeeded. |
| 20 |
* |
| 21 |
* @var bool |
| 22 |
*/ |
| 23 |
private $success; |
| 24 |
|
| 25 |
/** |
| 26 |
* The error code, or null when the update succeeded. |
| 27 |
* |
| 28 |
* @var string|null |
| 29 |
*/ |
| 30 |
private $error_code; |
| 31 |
|
| 32 |
/** |
| 33 |
* The constructor. Use the named constructors to create instances. |
| 34 |
* |
| 35 |
* @param int $post_id The ID of the post the result is for. |
| 36 |
* @param bool $success Whether the update succeeded. |
| 37 |
* @param string|null $error_code The error code, or null when the update succeeded. |
| 38 |
*/ |
| 39 |
private function __construct( int $post_id, bool $success, ?string $error_code ) { |
| 40 |
$this->post_id = $post_id; |
| 41 |
$this->success = $success; |
| 42 |
$this->error_code = $error_code; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Creates a successful result. |
| 47 |
* |
| 48 |
* @param int $post_id The ID of the post the result is for. |
| 49 |
* |
| 50 |
* @return self The successful result. |
| 51 |
*/ |
| 52 |
public static function for_success( int $post_id ): self { |
| 53 |
return new self( $post_id, true, null ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Creates a failed result. |
| 58 |
* |
| 59 |
* @param int $post_id The ID of the post the result is for. |
| 60 |
* @param string $error_code The error code describing the failure. |
| 61 |
* |
| 62 |
* @return self The failed result. |
| 63 |
*/ |
| 64 |
public static function for_failure( int $post_id, string $error_code ): self { |
| 65 |
return new self( $post_id, false, $error_code ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Gets the ID of the post the result is for. |
| 70 |
* |
| 71 |
* @return int The ID of the post the result is for. |
| 72 |
*/ |
| 73 |
public function get_post_id(): int { |
| 74 |
return $this->post_id; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Whether the update succeeded. |
| 79 |
* |
| 80 |
* @return bool Whether the update succeeded. |
| 81 |
*/ |
| 82 |
public function is_success(): bool { |
| 83 |
return $this->success; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Gets the error code. |
| 88 |
* |
| 89 |
* @return string|null The error code, or null when the update succeeded. |
| 90 |
*/ |
| 91 |
public function get_error_code(): ?string { |
| 92 |
return $this->error_code; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Parses the result to the expected key value representation. |
| 97 |
* |
| 98 |
* @return array<string, int|bool|string> The result presented as the expected key value representation. |
| 99 |
*/ |
| 100 |
public function to_array(): array { |
| 101 |
$array = [ |
| 102 |
'id' => $this->post_id, |
| 103 |
'success' => $this->success, |
| 104 |
]; |
| 105 |
|
| 106 |
if ( ! $this->success ) { |
| 107 |
$array['error'] = $this->error_code; |
| 108 |
} |
| 109 |
|
| 110 |
return $array; |
| 111 |
} |
| 112 |
} |
| 113 |
|