GiftActionPool.php 930 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Longyi\FreeGift\Services\Actions;
  3. use Longyi\FreeGift\Config\Gift;
  4. /**
  5. * action_type → 算法 的分发器。
  6. */
  7. class GiftActionPool
  8. {
  9. /**
  10. * @var array<string, AbstractGiftAction>
  11. */
  12. protected array $actions;
  13. public function __construct(
  14. SameProductAction $sameProduct,
  15. ProductAction $product,
  16. EachNAction $eachN,
  17. CartAction $cart,
  18. SpentAction $spent
  19. ) {
  20. $this->actions = [
  21. Gift::ACTION_SAME_PRODUCT => $sameProduct,
  22. Gift::ACTION_WITH_PRODUCTS => $product,
  23. Gift::ACTION_EACH_N => $eachN,
  24. Gift::ACTION_WHOLE_CART => $cart,
  25. Gift::ACTION_SPENT => $spent,
  26. ];
  27. }
  28. /**
  29. * 取 action_type 对应的算法。
  30. */
  31. public function get($actionType): ?AbstractGiftAction
  32. {
  33. return $this->actions[$actionType] ?? null;
  34. }
  35. }