OrderInvoiceStatusRepository.php 3.32 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\ResourceModel\OrderInvoiceStatus as ResourceModel;
use Vertex\Tax\Model\Data\OrderInvoiceStatusFactory as Factory;
use Vertex\Tax\Model\Data\OrderInvoiceStatus;

/**
 * Repository of Order Invoice data
 */
class OrderInvoiceStatusRepository
{
    /** @var ResourceModel */
    private $resourceModel;

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

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

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

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

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

    /**
     * Retrieve an OrderInvoiceStatus object for an Order
     *
     * @param int $orderId
     * @return OrderInvoiceStatus
     * @throws NoSuchEntityException
     */
    public function getByOrderId($orderId)
    {
        /** @var OrderInvoiceStatus $orderInvoiceStatus */
        $orderInvoiceStatus = $this->factory->create();
        $this->resourceModel->load($orderInvoiceStatus, $orderId);
        if (!$orderInvoiceStatus->getId()) {
            throw NoSuchEntityException::singleField('orderId', $orderId);
        }
        return $orderInvoiceStatus;
    }
}