CarrierFactory.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Model;
  7. /**
  8. * Class CarrierFactory
  9. */
  10. class CarrierFactory implements CarrierFactoryInterface
  11. {
  12. /**
  13. * Core store config
  14. *
  15. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  16. */
  17. protected $_scopeConfig;
  18. /**
  19. * @var \Magento\Framework\ObjectManagerInterface
  20. */
  21. protected $_objectManager;
  22. /**
  23. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  24. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  25. */
  26. public function __construct(
  27. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  28. \Magento\Framework\ObjectManagerInterface $objectManager
  29. ) {
  30. $this->_scopeConfig = $scopeConfig;
  31. $this->_objectManager = $objectManager;
  32. }
  33. /**
  34. * Get carrier instance
  35. *
  36. * @param string $carrierCode
  37. * @return bool|Carrier\AbstractCarrier
  38. */
  39. public function get($carrierCode)
  40. {
  41. $className = $this->_scopeConfig->getValue(
  42. 'carriers/' . $carrierCode . '/model',
  43. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  44. );
  45. if (!$className) {
  46. return false;
  47. }
  48. $carrier = $this->_objectManager->get($className);
  49. $carrier->setId($carrierCode);
  50. return $carrier;
  51. }
  52. /**
  53. * Create carrier instance
  54. *
  55. * @param string $carrierCode
  56. * @param int|null $storeId
  57. * @return bool|Carrier\AbstractCarrier
  58. */
  59. public function create($carrierCode, $storeId = null)
  60. {
  61. $className = $this->_scopeConfig->getValue(
  62. 'carriers/' . $carrierCode . '/model',
  63. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  64. $storeId
  65. );
  66. if (!$className) {
  67. return false;
  68. }
  69. $carrier = $this->_objectManager->create($className);
  70. $carrier->setId($carrierCode);
  71. if ($storeId) {
  72. $carrier->setStore($storeId);
  73. }
  74. return $carrier;
  75. }
  76. /**
  77. * Get carrier by its code if it is active
  78. *
  79. * @param string $carrierCode
  80. * @return bool|Carrier\AbstractCarrier
  81. */
  82. public function getIfActive($carrierCode)
  83. {
  84. return $this->_scopeConfig->isSetFlag(
  85. 'carriers/' . $carrierCode . '/active',
  86. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  87. ) ? $this->get(
  88. $carrierCode
  89. ) : false;
  90. }
  91. /**
  92. * Create carrier by its code if it is active
  93. *
  94. * @param string $carrierCode
  95. * @param null|int $storeId
  96. * @return bool|Carrier\AbstractCarrier
  97. */
  98. public function createIfActive($carrierCode, $storeId = null)
  99. {
  100. return $this->_scopeConfig->isSetFlag(
  101. 'carriers/' . $carrierCode . '/active',
  102. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  103. $storeId
  104. ) ? $this->create(
  105. $carrierCode,
  106. $storeId
  107. ) : false;
  108. }
  109. }