repository = $repository; $this->invoiceSentFactory = $invoiceSentFactory; $this->logger = $logger; } /** * Determine if an invoice has already been sent to Vertex * * @param InvoiceInterface $invoice * @return bool */ public function hasInvoiceBeenSentToVertex(InvoiceInterface $invoice) { if ($this->isInvoiceCachedAsSent($invoice)) { return true; } try { $invoiceSent = $this->repository->getByInvoiceId($invoice->getEntityId()); } catch (NoSuchEntityException $exception) { return false; } $result = $invoiceSent->isSent(); if ($result) { $this->cacheInvoiceSent($invoice); } return $result; } /** * Declare that an invoice has been sent to Vertex * * @param InvoiceInterface $invoice */ public function setInvoiceHasBeenSentToVertex(InvoiceInterface $invoice) { if ($this->isInvoiceCachedAsSent($invoice) || !$invoice->getEntityId()) { return; } /** @var InvoiceSent $invoiceSent */ $invoiceSent = $this->invoiceSentFactory->create(); $invoiceSent->setInvoiceId($invoice->getEntityId()); $invoiceSent->setIsSent(true); try { $this->repository->save($invoiceSent); } catch (AlreadyExistsException $exception) { // Too many cooks - or requests, as the case may be. Perfectly acceptable } catch (\Exception $exception) { $this->logger->critical($exception); } } /** * Check the cache for whether or not an Invoice has been sent to Vertex * * @param InvoiceInterface $invoice * @return bool */ private function isInvoiceCachedAsSent(InvoiceInterface $invoice) { return $invoice->getEntityId() && isset($this->isSentCache[$invoice->getEntityId()]) && $this->isSentCache[$invoice->getEntityId()]; } /** * Add to the cache that an Invoice has been sent to Vertex * * @param InvoiceInterface $invoice */ private function cacheInvoiceSent(InvoiceInterface $invoice) { if ($invoice->getEntityId()) { $this->isSentCache[$invoice->getEntityId()] = true; } } }