GoodsOptionPriceManager.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\common\modules\goodsOption;
  3. use app\common\helpers\Serializer;
  4. use app\common\models\GoodsOption;
  5. use app\common\modules\goodsOption\dealPrice\BaseDealPrice;
  6. class GoodsOptionPriceManager
  7. {
  8. /**
  9. * @var GoodsOption $goodsOption
  10. */
  11. private $goodsOption;
  12. private $detailPrice;
  13. public function __construct(GoodsOption $goodsOption)
  14. {
  15. $this->goodsOption = $goodsOption;
  16. }
  17. public function getDealPrice()
  18. {
  19. if (!isset($this->detailPrice)) {
  20. $this->detailPrice = $this->_getDealPrice();
  21. }
  22. return $this->detailPrice;
  23. }
  24. private function _getDealPrice()
  25. {
  26. $dealPrices = collect(\app\common\modules\shop\ShopConfig::current()->get('shop-foundation.goods-option.dealPrice'))->map(function (array $dealPriceStrategy) {
  27. return call_user_func($dealPriceStrategy['class'], $this->goodsOption);
  28. });
  29. $dealPrices = $dealPrices->sortBy(function (BaseDealPrice $dealPrice) {
  30. return $dealPrice->getWeight();
  31. });
  32. /**
  33. * @var BaseDealPrice $dealPrice
  34. */
  35. $dealPrice = $dealPrices->first(function (BaseDealPrice $dealPrice) {
  36. return $dealPrice->enable();
  37. });
  38. return $dealPrice->getDealPrice();
  39. }
  40. }