Skip to content

Commit 9b49aa1

Browse files
committed
Adds documentation
1 parent 6171a1c commit 9b49aa1

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Creation/Synchronizer/LocalOrderSynchronizer.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ final class LocalOrderSynchronizer: OrderSynchronizer {
5151
// No op
5252
}
5353

54+
/// Creates the order remotely.
55+
///
5456
func commitAllChanges(onCompletion: @escaping (Result<Order, Error>) -> Void) {
5557
let action = OrderAction.createOrder(siteID: siteID, order: order, onCompletion: onCompletion)
5658
stores.dispatch(action)
5759
}
5860
}
5961

6062
private extension LocalOrderSynchronizer {
63+
/// Updates order as inputs are received.
64+
///
6165
func bindInputs() {
6266
setStatus.withLatestFrom(orderPublisher)
6367
.map { newStatus, order in
@@ -76,11 +80,16 @@ private extension LocalOrderSynchronizer {
7680
order.copy(billingAddress: addressesInput?.billing, shippingAddress: addressesInput?.shipping)
7781
}
7882
.assign(to: &$order)
83+
84+
// TODO: Bind shipping & fees input
7985
}
8086
}
8187

88+
/// Helper to updates an `order` given an `OrderSyncInput` type.
89+
///
8290
private struct ProductInputTransformer {
83-
91+
/// Type to help bundling order Items parameters.
92+
///
8493
struct OrderItemParameters {
8594
let quantity: Decimal
8695
let price: Decimal
@@ -91,11 +100,15 @@ private struct ProductInputTransformer {
91100
}
92101
}
93102

103+
/// Adds, deletes, or updates order items based on the given product input.
104+
///
94105
static func update(input: OrderSyncProductInput, on order: Order) -> Order {
106+
// If the input's quantity is 0 or less, delete the item if possible.
95107
guard input.quantity > 0 else {
96108
return remove(input: input, from: order)
97109
}
98110

111+
// Add or update the order items with the new input.
99112
let newItem = createOrderItem(using: input)
100113
var items = order.items
101114
if let itemIndex = order.items.firstIndex(where: { $0.itemID == newItem.itemID }) {
@@ -107,13 +120,17 @@ private struct ProductInputTransformer {
107120
return order.copy(items: items)
108121
}
109122

110-
static func remove(input: OrderSyncProductInput, from order: Order) -> Order {
123+
/// Removes an order item from an order when the `item.itemID` matches the `input.id`.
124+
///
125+
private static func remove(input: OrderSyncProductInput, from order: Order) -> Order {
111126
var items = order.items
112127
items.removeAll { $0.itemID == input.id.hashValue }
113128
return order.copy(items: items)
114129
}
115130

116-
static func createOrderItem(using input: OrderSyncProductInput) -> OrderItem {
131+
/// Creates and order item by using the `input.id` as the `item.itemID`.
132+
///
133+
private static func createOrderItem(using input: OrderSyncProductInput) -> OrderItem {
117134
let quantity = Decimal(input.quantity)
118135
let parameters: OrderItemParameters = {
119136
switch input.product {

0 commit comments

Comments
 (0)