/** * @param Event $event * @return CreateOrderCommand */ public function create(Event $event, StoreEntity $store) { $eventName = $event->getName(); switch ($eventName) { case EventInterface::NAME_ORDER_CREATED: $command = new CreateOrderCommand($event, $store); break; default: throw new \RuntimeException("ShopifyCommandFactory doesn't know how to create a command for event: " . $eventName); } return $command; }
/** * @param Event $event * @return CreateOrderHandler */ public function create(Event $event) { $eventName = $event->getName(); switch ($eventName) { case EventInterface::NAME_ORDER_CREATED: $handler = $this->createOrderHandler; break; default: throw new \RuntimeException("HandlerFactory doesn't know how to create a handler for event: " . $eventName); break; } return $handler; }
/** * @param Request $request * * @Rest\View(statusCode=204) * @Rest\Post("/webhook", defaults={"_format": "json"}) * * @throws ResourceConflictException * @throws \Exception * @return null */ public function handleWebHookAction(Request $request) { $this->logger->info($request->getContent()); try { $event = null; $store = $this->storeService->getStoreByRequest($request); $eventId = $this->shopifyEventRetriever->verifyWebhookRequest($request, $store); $event = $this->shopifyEventRetriever->retrieve($eventId); if ($event) { throw new EventAlreadyProcessed(sprintf('Event Id %s has already been processed', $eventId)); } //Save the event so we don't process this again $event = Event::createFromRequest($request, $eventId); $this->eventRepository->save($event); $cmd = $this->commandFactory->create($event, $store); $handler = $this->handlerFactory->create($event); $handler->execute($cmd); $event->updateStatus(Event::STATUS_PROCESSED); $this->logger->alert(sprintf('Completed Processing event id %s', $eventId)); } catch (\Exception $e) { if ($event instanceof Event) { $event->updateStatus(Event::STATUS_FAILED); } $this->logger->alert($e->getMessage()); } finally { if ($event instanceof Event) { $this->eventRepository->update($event); } return new Response('', 201); } }