BackendOrderOperationsCollector.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020/12/1
  6. * Time: 11:24
  7. */
  8. namespace app\common\modules\order;
  9. use app\common\models\Order;
  10. use app\frontend\modules\order\operations\OrderOperation;
  11. use app\frontend\modules\order\operations\OrderOperationInterface;
  12. class BackendOrderOperationsCollector
  13. {
  14. /**
  15. * @param Order $order
  16. * @return array
  17. * @throws \app\common\exceptions\AppException
  18. */
  19. public function getOperations(Order $order)
  20. {
  21. $operationsSettings = $this->getOperationsSetting($order);
  22. $operations = array_map(function ($operationName) use ($order) {
  23. /**
  24. * @var OrderOperationInterface $operation
  25. */
  26. $operation = new $operationName($order);
  27. if (!$operation->enable()) {
  28. return null;
  29. }
  30. $result['name'] = $operation->getName();
  31. $result['value'] = $operation->getValue();
  32. $result['api'] = $operation->getApi();
  33. $result['type'] = $operation->getType();
  34. return $result;
  35. }, $operationsSettings);
  36. $operations = array_filter($operations);
  37. return array_values($operations) ?: [];
  38. }
  39. protected function getOperationsSetting($order)
  40. {
  41. $settings = array_merge($this->getBasicOperations(), $this->addOperationConfig());
  42. return $settings[$this->getStatusCode($order)];
  43. }
  44. public function addOperationConfig()
  45. {
  46. return [];
  47. }
  48. protected function getBasicOperations()
  49. {
  50. return [
  51. 'waitPay' => [
  52. \app\backend\modules\order\operations\Pay::class,
  53. ],
  54. 'waitSend' => [
  55. \app\backend\modules\order\operations\Send::class,
  56. \app\backend\modules\order\operations\SeparateSend::class,
  57. ],
  58. 'waitReceive' => [
  59. \app\backend\modules\order\operations\SeparateSend::class,
  60. \app\backend\modules\order\operations\Receive::class,
  61. \app\backend\modules\order\operations\CancelSend::class,
  62. ],
  63. 'complete' => [],
  64. 'close' => [],
  65. ];
  66. }
  67. /**
  68. * @param Order $order
  69. */
  70. public function getAllOperations(Order $order)
  71. {
  72. }
  73. protected function getStatusCode(Order $order)
  74. {
  75. $defaults = [0 => 'waitPay', 1 => 'waitSend', 2 => 'waitReceive', 3 => 'complete', -1 => 'close'];
  76. return $defaults[$order->status];
  77. }
  78. }