InvoiceSentRepository.php 3.13 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Model\Repository;

use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Vertex\Tax\Model\Data\InvoiceSent;
use Vertex\Tax\Model\Data\InvoiceSentFactory;
use Vertex\Tax\Model\ResourceModel\InvoiceSent as ResourceModel;

/**
 * Repository of Invoice sent data
 */
class InvoiceSentRepository
{
    /** @var ResourceModel */
    private $resourceModel;

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

    /**
     * @param ResourceModel $resourceModel
     * @param InvoiceSentFactory $factory
     */
    public function __construct(ResourceModel $resourceModel, InvoiceSentFactory $factory)
    {
        $this->resourceModel = $resourceModel;
        $this->factory = $factory;
    }

    /**
     * Save an InvoiceSent object
     *
     * @param InvoiceSent $invoiceSent
     * @return $this
     * @throws \Magento\Framework\Exception\CouldNotSaveException
     * @throws \Magento\Framework\Exception\AlreadyExistsException
     */
    public function save(InvoiceSent $invoiceSent)
    {
        try {
            $this->resourceModel->save($invoiceSent);
        } catch (AlreadyExistsException $e) {
            throw $e;
        } catch (\Exception $e) {
            throw new CouldNotSaveException(__('Unable to save Invoice Sent status'), $e);
        }
        return $this;
    }

    /**
     * Delete an InvoiceSent object
     *
     * @param InvoiceSent $invoiceSent
     * @return $this
     * @throws CouldNotDeleteException
     */
    public function delete(InvoiceSent $invoiceSent)
    {
        try {
            $this->resourceModel->delete($invoiceSent);
        } catch (\Exception $e) {
            throw new CouldNotDeleteException(__('Unable to delete Invoice Sent status'), $e);
        }
        return $this;
    }

    /**
     * Delete an InvoiceSent object given an Invoice ID
     *
     * @param int $invoiceId
     * @return $this
     * @throws CouldNotDeleteException
     */
    public function deleteByInvoiceId($invoiceId)
    {
        /** @var InvoiceSent $invoiceSent */
        $invoiceSent = $this->factory->create();
        $invoiceSent->setId($invoiceId);
        try {
            $this->resourceModel->delete($invoiceSent);
        } catch (\Exception $e) {
            throw new CouldNotDeleteException(__('Unable to delete Invoice Sent status'), $e);
        }
        return $this;
    }

    /**
     * Retrieve an InvoiceSent object for an Invoice
     *
     * @param int $invoiceId
     * @return InvoiceSent
     * @throws NoSuchEntityException
     */
    public function getByInvoiceId($invoiceId)
    {
        $invoiceSent = $this->factory->create();
        $this->resourceModel->load($invoiceSent, $invoiceId);
        if (!$invoiceSent->getId()) {
            throw NoSuchEntityException::singleField('invoiceId', $invoiceId);
        }
        return $invoiceSent;
    }
}