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

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