Source.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Inventory\Model\ResourceModel;
  8. use Exception;
  9. use Magento\Framework\Model\AbstractModel;
  10. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  11. use Magento\Framework\Model\ResourceModel\Db\Context;
  12. use Magento\InventoryApi\Model\SourceCarrierLinkManagementInterface;
  13. use Magento\InventoryApi\Api\Data\SourceInterface;
  14. use Magento\Framework\Model\ResourceModel\PredefinedId;
  15. /**
  16. * Implementation of basic operations for Source entity for specific db layer
  17. */
  18. class Source extends AbstractDb
  19. {
  20. /**
  21. * Provides possibility of saving entity with predefined/pre-generated id
  22. */
  23. use PredefinedId;
  24. /**#@+
  25. * Constants related to specific db layer
  26. */
  27. const TABLE_NAME_SOURCE = 'inventory_source';
  28. /**#@-*/
  29. /**
  30. * Primary key auto increment flag
  31. *
  32. * @var bool
  33. */
  34. protected $_isPkAutoIncrement = false;
  35. /**
  36. * @var SourceCarrierLinkManagementInterface
  37. */
  38. private $sourceCarrierLinkManagement;
  39. /**
  40. * @param Context $context
  41. * @param SourceCarrierLinkManagementInterface $sourceCarrierLinkManagement
  42. * @param null $connectionName
  43. */
  44. public function __construct(
  45. Context $context,
  46. SourceCarrierLinkManagementInterface $sourceCarrierLinkManagement,
  47. $connectionName = null
  48. ) {
  49. parent::__construct($context, $connectionName);
  50. $this->sourceCarrierLinkManagement = $sourceCarrierLinkManagement;
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. protected function _construct()
  56. {
  57. $this->_init(self::TABLE_NAME_SOURCE, SourceInterface::SOURCE_CODE);
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function load(AbstractModel $object, $value, $field = null)
  63. {
  64. parent::load($object, $value, $field);
  65. /** @var SourceInterface $object */
  66. $this->sourceCarrierLinkManagement->loadCarrierLinksBySource($object);
  67. return $this;
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. public function save(AbstractModel $object)
  73. {
  74. $connection = $this->getConnection();
  75. $connection->beginTransaction();
  76. try {
  77. parent::save($object);
  78. /** @var SourceInterface $object */
  79. $this->sourceCarrierLinkManagement->saveCarrierLinksBySource($object);
  80. $connection->commit();
  81. } catch (Exception $e) {
  82. $connection->rollBack();
  83. throw $e;
  84. }
  85. return $this;
  86. }
  87. }