RefundOrderFactory.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2022/11/3
  6. * Time: 15:43
  7. */
  8. namespace app\common\modules\refund;
  9. use app\common\models\Order;
  10. use app\common\modules\refund\product\RefundOrderTypeBase;
  11. use app\common\modules\refund\product\ShopRefundOrder;
  12. class RefundOrderFactory
  13. {
  14. /**
  15. * @var Order
  16. */
  17. protected $order;
  18. protected $refundOrder;
  19. protected $refundConfigs;
  20. /**
  21. * @var null|static
  22. */
  23. protected static $instance = null;
  24. /**
  25. * 单例缓存
  26. * @return null|static
  27. */
  28. public static function getInstance()
  29. {
  30. if (!isset(self::$instance)) {
  31. self::$instance = new self();
  32. }
  33. return self::$instance;
  34. }
  35. public function forgetInstance()
  36. {
  37. self::$instance = null;
  38. }
  39. public function __construct()
  40. {
  41. $this->refundConfigs = \app\common\modules\shop\ShopConfig::current()->get('shop-foundation.refund.order-type');
  42. }
  43. public function getConfigs()
  44. {
  45. return $this->refundConfigs;
  46. }
  47. /**
  48. * @param Order $order
  49. * @param string $port
  50. * @return RefundOrderTypeBase|ShopRefundOrder
  51. */
  52. public function getRefundOrder(Order $order, $port = 'frontend')
  53. {
  54. $refundConfigs = $this->getConfigs();
  55. // 从配置文件中载入,按优先级排序
  56. //$refundConfigs = collect($configs)->sortBy('priority');
  57. //遍历取到第一个通过验证的订单类型返回
  58. foreach ($refundConfigs as $configItem) {
  59. /**
  60. * @var RefundOrderTypeBase $orderType
  61. */
  62. $orderType = call_user_func($configItem['class'], $order, $port);
  63. //通过验证返回
  64. if (isset($orderType) && $orderType->isBelongTo()) {
  65. return $orderType;
  66. }
  67. }
  68. //没有对应订单类型,返回默认订单类型
  69. return new ShopRefundOrder($order, $port);
  70. }
  71. }