CleanCode is not what you think

final class WooIntegration extends WooBase3
{
public function importOrders(): void
{
$wooOrders = $this->retrieveWooOrders();
foreach ($wooOrders as $wooOrder) {
try {
$this->importOneOrder($wooOrder);
} catch (Exception $e) {
$this->process->fail($e->getMessage());
}
}
}
......
private function importOneOrder(array $wooOrder): void
{
$order = new Order();
$order->provider_id = $this->connection->provider_id;
$order->shipping_address = $wooOrder['shipping']['address'];
......
$order->save();
$this->importLines($order, $wooOrder);
}
private function importLines(Order $order, array $wooOrder): void
{
foreach ($wooOrder['items'] as $wooLine) {
$sku = $wooLine['sku'];
$product = Inventory::find()->andWhere(['sku' => $sku, 'integration_id' => $this->connection->integrationId]);
$orderLine = new OrderLine();
$orderLine->order_id = $order->id;
$orderLine->qty = $wooLine['qty'];
$orderLine->gtin = $product->gtin;
$orderLine->save();
}
}
public function importOrders(): void
{
$service = new ImportOneWooOrderService();
$wooOrders = $this->retrieveWooOrders();
foreach ($wooOrders as $wooOrder) {
try {
$service->importOneOrder($wooOrder);
} catch (Exception $e) {
$this->process->fail($e->getMessage());
}
}
}
$order->provider_id = $this->connection->provider_id;
private function importOneOrder(array $wooOrder, int $providerId): void
private function importLines(Order $order, array $wooOrder): void
{
foreach ($wooOrder['items'] as $wooLine) {
$sku = $wooLine['sku'];
$product = Inventory::find()->andWhere(['sku' => $sku, 'integration_id' => $this->connection->integrationId]);
$orderLine = new OrderLine();
$orderLine->order_id = $order->id;
$orderLine->qty = $wooLine['qty'];
$orderLine->gtin = $product->gtin;
$orderLine->save();
}
}
private function importLines(Order $order, array $wooOrder, InventoryServiceInterface $inventoryService): void
{
foreach ($wooOrder['items'] as $wooLine) {
$sku = $wooLine['sku'];
$product = $inventoryService->retriveBySkuAndIntegrationId($sku, $this->connection->integrationId);
$inventoryService = Mockery::mock(InventoryServiceInterface::class);
$inventoryService->shouldReceive('retriveBySkuAndIntegrationId')->andReturn(InventoryMother::randomWithSku($sku));
$importOneOrderService->importOneOrder($order,$providerId,$inventoryService);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store