Address.php 2.82 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
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */
namespace Temando\Shipping\Model\ResourceModel\Checkout;

use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
use Magento\Framework\Serialize\SerializerInterface;
use Temando\Shipping\Api\Data\Checkout\AddressInterface;
use Temando\Shipping\Setup\SetupSchema;

/**
 * Checkout shipping address extension resource model
 *
 * @package  Temando\Shipping\Model
 * @author   Christoph Aßmann <christoph.assmann@netresearch.de>
 * @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link     http://www.temando.com/
 */
class Address extends AbstractDb
{
    /**
     * Serializable fields declaration
     * - serialized: JSON object
     * - unserialized: associative array
     *
     * @var mixed[]
     */
    protected $_serializableFields = [
        AddressInterface::SERVICE_SELECTION => [
            [],
            [],
        ],
    ];

    /**
     * Address constructor.
     * @param Context $context
     * @param Snapshot $entitySnapshot
     * @param RelationComposite $entityRelationComposite
     * @param SerializerInterface $serializer
     * @param string $connectionName
     */
    public function __construct(
        Context $context,
        Snapshot $entitySnapshot,
        RelationComposite $entityRelationComposite,
        SerializerInterface $serializer,
        $connectionName = null
    ) {
        $this->serializer = $serializer;

        parent::__construct($context, $entitySnapshot, $entityRelationComposite, $connectionName);
    }

    /**
     * Init main table and primary key.
     *
     * @return void
     */
    protected function _construct()
    {
         $this->_init(SetupSchema::TABLE_CHECKOUT_ADDRESS, AddressInterface::ENTITY_ID);
    }

    /**
     * Query primary key by given shipping address id.
     *
     * @param int $quoteAddressId
     * @return int|null
     */
    public function getIdByQuoteAddressId($quoteAddressId)
    {
        try {
            $connection = $this->getConnection();
            $tableName  = $this->getMainTable();
            $table      = $this->getTable($tableName);

            $select = $connection->select()
                ->from($table, AddressInterface::ENTITY_ID)
                ->where('shipping_address_id = :shipping_address_id');

            $bind  = [':shipping_address_id' => (string)$quoteAddressId];
            $entityId = $connection->fetchOne($select, $bind);

            return $entityId ? (int) $entityId : null;
        } catch (\Exception $exception) {
            return null;
        }
    }
}