OrderOperationsCollector.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2018/8/2
  6. * Time: 下午2:01
  7. */
  8. namespace app\common\modules\order;
  9. use app\common\models\Order;
  10. use app\common\modules\shop\ShopConfig;
  11. use app\frontend\modules\order\operations\OrderOperation;
  12. use app\frontend\modules\order\operations\OrderOperationInterface;
  13. use app\frontend\modules\order\services\OrderFrontendButtonBase;
  14. class OrderOperationsCollector
  15. {
  16. /**
  17. * @param Order $order
  18. * @return array
  19. */
  20. public function getOperations(Order $order)
  21. {
  22. $operations = $order->getOperationsSetting();
  23. if (empty($operations)) {
  24. return [];
  25. }
  26. $button = [];
  27. foreach ($operations as $item) {
  28. if (empty($item['class']) || !class_exists($item['class'])) {
  29. continue;
  30. }
  31. $class = new $item['class']();
  32. if (!($class instanceof OrderFrontendButtonBase)) {
  33. continue;
  34. }
  35. $class->init($order);
  36. if ($class->enable()) {
  37. //取第一个通过验证的
  38. $button = $class->getButton();
  39. break;
  40. }
  41. }
  42. $button = array_filter($button);
  43. return array_values($button) ? : [];
  44. // $operationsSettings = $order->getOperationsSetting();
  45. // $operations = array_map(function ($operationName) use ($order) {
  46. // /**
  47. // * @var OrderOperationInterface $operation
  48. // */
  49. // $operation = new $operationName($order);
  50. // if (!$operation->enable()) {
  51. // return null;
  52. // }
  53. // $result['name'] = $operation->getName();
  54. // $result['value'] = $operation->getValue();
  55. // $result['api'] = $operation->getApi();
  56. // $result['type'] = $operation->getType();
  57. //
  58. // return $result;
  59. // }, $operationsSettings);
  60. //
  61. // $operations = array_filter($operations);
  62. // return array_values($operations) ?: [];
  63. }
  64. /**
  65. * @param Order $order
  66. * @return array
  67. */
  68. public function getAllOperations(Order $order)
  69. {
  70. $operations = array_map(function ($operationName) use ($order) {
  71. /**
  72. * @var OrderOperation $operation
  73. */
  74. $operation = new $operationName($order);
  75. $result['name'] = $operation->getName();
  76. $result['value'] = $operation->getValue();
  77. return $result;
  78. }, $this->getContract($order->statusCode));
  79. return $operations;
  80. }
  81. }