public function addProduct(AddProductCommand $command) { $cart = $this->repository->getCart(); $product = $this->productRepository->findByIdentity(new UuidIdentity($command->getProductId())); $cart->addProduct($product, new Quantity($command->getQuantity())); $this->repository->setCart($cart); }
/** * @inheritdoc */ public function getCart() : Cart { $data = $this->session->get(self::NAME, []); $lineItems = new ArrayCollection(); $products = $this->repository->findByIdentities(array_keys($data)); foreach ($products as $product) { $lineItem = new LineItem($product, new Quantity($data[$product->getId()->getValue()])); $lineItems->set($product->getId()->getValue(), $lineItem); } return new Cart($lineItems); }
public function updateProduct(UpdateProductCommand $command) { $product = $this->repository->findByIdentity(new UuidIdentity($command->getId())); $category = $this->categoryRepository->findByIdentity(new UuidIdentity($command->getCategory())); $product->updateInfo($command->getName(), new Money($command->getPrice()), $category, $command->getDescription(), $command->isAvailable(), $command->getImageUrl()); $productOptions = $command->getProductOptions(); foreach ($product->getProductOptions() as $key => $productOption) { if (isset($productOptions[$key])) { $productOption->changeValue($productOptions[$key]->getValue()); unset($productOptions[$key]); } else { $product->getProductOptions()->remove($key); } } foreach ($productOptions as $productOption) { $option = $this->optionRepository->getReference(new UuidIdentity($productOption->getOption())); $product->addOption($option, $productOption->getValue()); } $this->repository->save($product); $event = new ProductSavedEvent($product); $this->eventBus->handle($event); }