| 1 |
<?php |
| 2 |
/** |
| 3 |
* WCPOS sync read surface. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Sync |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Sync; |
| 9 |
|
| 10 |
// phpcs:disable Squiz.Commenting, Generic.Commenting -- Ported lab documentation is preserved verbatim. |
| 11 |
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- The uuid-stop fail-closed message carries only a numeric order id, not rendered output. |
| 12 |
|
| 13 |
/** |
| 14 |
* THE complete-order-document builder (#424): the envelope the order read |
| 15 |
* surface ships — the batch custom pull — assembled in exactly one place, keyed |
| 16 |
* by the order's stable uuid (P0-1), with the uuid guard's single fail-closed |
| 17 |
* reaction (Order_Uuid_Exception). |
| 18 |
* |
| 19 |
* The re-key (ADR 0021 trap): `id` is the record's _woocommerce_pos_uuid — the |
| 20 |
* SAME identity the client stores under — never woo-order:<id>. A numeric-keyed |
| 21 |
* document would duplicate the uuid-keyed row client-side. |
| 22 |
*/ |
| 23 |
final class Order_Document { |
| 24 |
/** |
| 25 |
* Read the order's stable uuid from a payload's meta, failing closed. |
| 26 |
* The IDENTITY payload must be the full serialized payload (the serializer |
| 27 |
* stamped it via the woocommerce_pos_sync_serialized_order filter). |
| 28 |
* |
| 29 |
* @param array $identity_payload The FULL serialized payload (uuid source of truth). |
| 30 |
* @param int $order_id The numeric Woo order id (for the error message). |
| 31 |
* |
| 32 |
* @throws Order_Uuid_Exception when the payload carries no valid uuid. |
| 33 |
*/ |
| 34 |
public static function require_uuid( array $identity_payload, int $order_id ): string { |
| 35 |
$uuid = Pos_Uuid::read_valid_uuid_from_meta( $identity_payload['meta_data'] ?? array() ); |
| 36 |
if ( '' === $uuid ) { |
| 37 |
throw Order_Uuid_Exception::for_order( $order_id ); |
| 38 |
} |
| 39 |
return $uuid; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Build the complete order document envelope. |
| 44 |
* |
| 45 |
* @param array $identity_payload The FULL serialized payload (uuid source of truth). |
| 46 |
* @param array $client_payload The payload actually shipped. |
| 47 |
* @param int $order_id The numeric Woo order id (for the uuid guard's error message). |
| 48 |
* @param string $revision The document revision (index row's, or the canonical fallback). |
| 49 |
* @param array $checkpoint The per-document checkpoint (updatedAtGmt/orderId/revision/sequence). |
| 50 |
* @param bool $partial Whether $client_payload is a partial fieldset. |
| 51 |
* @param string $source 'custom-pull' | 'skeleton' | 'snapshot'. |
| 52 |
* |
| 53 |
* @throws Order_Uuid_Exception when the identity payload carries no valid uuid. |
| 54 |
*/ |
| 55 |
public static function build( array $identity_payload, array $client_payload, int $order_id, string $revision, array $checkpoint, bool $partial, string $source ): array { |
| 56 |
return array( |
| 57 |
// ADR 0029 decision 6: no per-document wooOrderId key — the client derives |
| 58 |
// remote identity from payload.id at its ingest edge. The deletes channel |
| 59 |
// (a SEPARATE response key) still speaks numeric order ids. |
| 60 |
'id' => self::require_uuid( $identity_payload, $order_id ), |
| 61 |
'payload' => $client_payload, |
| 62 |
'sync' => array( |
| 63 |
'revision' => $revision, |
| 64 |
'checkpoint' => $checkpoint, |
| 65 |
'partial' => $partial, |
| 66 |
'source' => $source, |
| 67 |
), |
| 68 |
'local' => array( |
| 69 |
'dirty' => false, |
| 70 |
'pendingMutationIds' => array(), |
| 71 |
), |
| 72 |
); |
| 73 |
} |
| 74 |
} |
| 75 |
|