/** * @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); } }
/** * @param $eventId * @return object */ public function retrieve($eventId) { $event = $this->eventRepository->findOneBy(['eventId' => $eventId]); return $event; }