public function createOrder($customerId, $products) { $this->eventDispatcher->dispatch(OrderEvent::BEFORE_CREATE, new OrderBeforeCreate($customerId, $products)); $order = new Order(); /* @var $customer Customer */ $customer = $this->doctrine->getRepository(Customer::REPOSITORY)->find($customerId); $order->setCustomer($customer); $addresses = $customer->getAddress(); $order->setBillingAddress($addresses[0]); $order->setShippingAddress($addresses[1]); foreach ($products as $product) { $this->createProductLine($order, $product['id'], $product['quantity']); } $this->entityManager->persist($order); $this->entityManager->flush(); $this->eventDispatcher->dispatch(OrderEvent::AFTER_CREATE, new OrderEvent($order)); $this->entityManager->flush(); return $order->getId(); }