public function viewAction($id, Request $request) { $query = new GetProductByIdQuery($id); $this->queryBus->handle($query); /** @var ProductView $product */ $product = $query->getResult(); $command = new UpdateProductCommand($product->getId(), $product->getName(), $product->getPrice(), $product->getCategory()->getId(), $product->getDescription(), $product->isAvailable(), $product->getImageUrl()); $command->setProductOptions(array_map(function (ProductOptionView $optionView) { $productOption = new ProductOption(); $productOption->setOption($optionView->getOption()->getId()); $productOption->setValue($optionView->getValue()); return $productOption; }, $product->getProductOptions())); $form = $this->formFactory->create('Shop\\Presentation\\Form\\ProductType', $command); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->commandBus->handle($command); return new RedirectResponse($this->router->generate('admin_product', ['id' => $command->getId()])); } return new Response($this->engine->render(':admin/product:form.html.twig', ['form' => $form->createView()])); }
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); }