public function createAction(Request $request) { $command = new CreateProductCommand(); $command->setProductOptions([new ProductOption()]); $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 createProduct(CreateProductCommand $command) { $uuid = Uuid::uuid4(); $command->setId($uuid); $category = $this->categoryRepository->findByIdentity(new UuidIdentity($command->getCategory())); $product = new Product(new UuidIdentity($command->getId()), $command->getName(), new Money($command->getPrice()), $category, $command->getDescription(), $command->isAvailable(), $command->getImageUrl()); foreach ($command->getProductOptions() 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); }