Address.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Checkout;
  6. use Magento\Framework\Model\ResourceModel\Db\Context;
  7. use Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;
  8. use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
  9. use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
  10. use Magento\Framework\Serialize\SerializerInterface;
  11. use Temando\Shipping\Api\Data\Checkout\AddressInterface;
  12. use Temando\Shipping\Setup\SetupSchema;
  13. /**
  14. * Checkout shipping address extension resource model
  15. *
  16. * @package Temando\Shipping\Model
  17. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link http://www.temando.com/
  20. */
  21. class Address extends AbstractDb
  22. {
  23. /**
  24. * Serializable fields declaration
  25. * - serialized: JSON object
  26. * - unserialized: associative array
  27. *
  28. * @var mixed[]
  29. */
  30. protected $_serializableFields = [
  31. AddressInterface::SERVICE_SELECTION => [
  32. [],
  33. [],
  34. ],
  35. ];
  36. /**
  37. * Address constructor.
  38. * @param Context $context
  39. * @param Snapshot $entitySnapshot
  40. * @param RelationComposite $entityRelationComposite
  41. * @param SerializerInterface $serializer
  42. * @param string $connectionName
  43. */
  44. public function __construct(
  45. Context $context,
  46. Snapshot $entitySnapshot,
  47. RelationComposite $entityRelationComposite,
  48. SerializerInterface $serializer,
  49. $connectionName = null
  50. ) {
  51. $this->serializer = $serializer;
  52. parent::__construct($context, $entitySnapshot, $entityRelationComposite, $connectionName);
  53. }
  54. /**
  55. * Init main table and primary key.
  56. *
  57. * @return void
  58. */
  59. protected function _construct()
  60. {
  61. $this->_init(SetupSchema::TABLE_CHECKOUT_ADDRESS, AddressInterface::ENTITY_ID);
  62. }
  63. /**
  64. * Query primary key by given shipping address id.
  65. *
  66. * @param int $quoteAddressId
  67. * @return int|null
  68. */
  69. public function getIdByQuoteAddressId($quoteAddressId)
  70. {
  71. try {
  72. $connection = $this->getConnection();
  73. $tableName = $this->getMainTable();
  74. $table = $this->getTable($tableName);
  75. $select = $connection->select()
  76. ->from($table, AddressInterface::ENTITY_ID)
  77. ->where('shipping_address_id = :shipping_address_id');
  78. $bind = [':shipping_address_id' => (string)$quoteAddressId];
  79. $entityId = $connection->fetchOne($select, $bind);
  80. return $entityId ? (int) $entityId : null;
  81. } catch (\Exception $exception) {
  82. return null;
  83. }
  84. }
  85. }