HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/magento.bikenow.co/vendor/vertex/module-tax/Model/InvoiceSentRegistry.php
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Model;

use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Api\Data\InvoiceInterface;
use Vertex\Tax\Model\Data\InvoiceSent;
use Vertex\Tax\Model\Data\InvoiceSentFactory;
use Vertex\Tax\Model\Repository\InvoiceSentRepository;

/**
 * Registry of Invoice Sent status
 *
 * Chiefly used to reduce database calls
 */
class InvoiceSentRegistry
{
    /** @var InvoiceSentRepository */
    private $repository;

    /** @var InvoiceSentFactory */
    private $invoiceSentFactory;

    /** @var bool[] Indexed by Invoice ID */
    private $isSentCache = [];

    /** @var ExceptionLogger */
    private $logger;

    /**
     * @param InvoiceSentRepository $repository
     * @param InvoiceSentFactory $invoiceSentFactory
     * @param ExceptionLogger $logger
     */
    public function __construct(
        InvoiceSentRepository $repository,
        InvoiceSentFactory $invoiceSentFactory,
        ExceptionLogger $logger
    ) {
        $this->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;
        }
    }
}