Code Duplication    Length = 20-20 lines in 2 locations

includes/cli/class-wc-cli-order.php 1 location

@@ 877-896 (lines=20) @@
874
	 * @param  string $action either 'create' or 'update'
875
	 * @throws WC_CLI_Exception if item ID is not associated with order
876
	 */
877
	protected function set_item( $order, $item_type, $item, $action ) {
878
		global $wpdb;
879
880
		$set_method = "set_{$item_type}";
881
882
		// verify provided line item ID is associated with order
883
		if ( 'update' === $action ) {
884
			$result = $wpdb->get_row(
885
				$wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
886
				absint( $item['id'] ),
887
				absint( $order->id )
888
			) );
889
890
			if ( is_null( $result ) ) {
891
				throw new WC_CLI_Exception( 'woocommerce_invalid_item_id', __( 'Order item ID provided is not associated with order', 'woocommerce' ) );
892
			}
893
		}
894
895
		$this->$set_method( $order, $item, $action );
896
	}
897
898
	/**
899
	 * Create or update a line item

includes/api/class-wc-rest-orders-controller.php 1 location

@@ 913-932 (lines=20) @@
910
	 * @param string $action either 'create' or 'update'
911
	 * @throws WC_REST_Exception If item ID is not associated with order
912
	 */
913
	protected function set_item( $order, $item_type, $item, $action ) {
914
		global $wpdb;
915
916
		$set_method = 'set_' . $item_type;
917
918
		// Verify provided line item ID is associated with order.
919
		if ( 'update' === $action ) {
920
			$result = $wpdb->get_row(
921
				$wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
922
				absint( $item['id'] ),
923
				absint( $order->id )
924
			) );
925
926
			if ( is_null( $result ) ) {
927
				throw new WC_REST_Exception( 'woocommerce_rest_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 );
928
			}
929
		}
930
931
		$this->$set_method( $order, $item, $action );
932
	}
933
934
	/**
935
	 * Helper method to check if the resource ID associated with the provided item is null.