|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author @ct-jensschulze <[email protected]> |
| 4 | + */ |
| 5 | + |
| 6 | +namespace Commercetools\Commons\Helper; |
| 7 | + |
| 8 | + |
| 9 | +use Commercetools\Core\Model\Channel\ChannelReference; |
| 10 | +use Commercetools\Core\Model\Common\Price; |
| 11 | +use Commercetools\Core\Model\Common\PriceCollection; |
| 12 | +use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference; |
| 13 | + |
| 14 | +class PriceFinder |
| 15 | +{ |
| 16 | + private $currency; |
| 17 | + private $country; |
| 18 | + private $customerGroup; |
| 19 | + private $channel; |
| 20 | + |
| 21 | + public function __construct($currency, $country = null, CustomerGroupReference $customerGroup = null, ChannelReference $channel = null) |
| 22 | + { |
| 23 | + $this->currency = $currency; |
| 24 | + $this->country = $country; |
| 25 | + $this->customerGroup = $customerGroup; |
| 26 | + $this->channel = $channel; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param PriceCollection $prices |
| 31 | + * @return Price |
| 32 | + */ |
| 33 | + public function findPrice(PriceCollection $prices) |
| 34 | + { |
| 35 | + $currency = $this->currency; |
| 36 | + $country = $this->country; |
| 37 | + $customerGroup = $this->customerGroup; |
| 38 | + $channel = $this->channel; |
| 39 | + |
| 40 | + $prices = new \CallbackFilterIterator( |
| 41 | + $prices, |
| 42 | + function ($price) use ($currency, $country, $customerGroup, $channel) { |
| 43 | + if (!$this->priceHasCurrency($price, $currency)) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + if (!$this->priceHasNoDate($price) && !$this->priceHasValidDate($price, new \DateTime())) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + if (is_null($country)) { |
| 50 | + if ($this->priceHas($price, 'country')) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + } elseif (!$this->priceHasCountry($price, $country)) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + if (is_null($customerGroup)) { |
| 57 | + if ($this->priceHas($price, 'customerGroup')) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + } elseif (!$this->priceHasCustomerGroup($price, $customerGroup)) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + if (is_null($channel)) { |
| 64 | + if ($this->priceHas($price, 'channel')) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } elseif (!$this->priceHasChannel($price, $channel)) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + return true; |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + foreach ($prices as $price) { |
| 75 | + return $price; |
| 76 | + } |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @param $prices |
| 82 | + * @param $currency |
| 83 | + * @param string $country |
| 84 | + * @param CustomerGroupReference $customerGroup |
| 85 | + * @param ChannelReference $channel |
| 86 | + * @return Price|null |
| 87 | + */ |
| 88 | + public static function findPriceFor( |
| 89 | + $prices, |
| 90 | + $currency, |
| 91 | + $country = null, |
| 92 | + CustomerGroupReference $customerGroup = null, |
| 93 | + ChannelReference $channel = null |
| 94 | + ) { |
| 95 | + $priceFinder = new static($currency, $country, $customerGroup, $channel); |
| 96 | + |
| 97 | + return $priceFinder->findPrice($prices); |
| 98 | + } |
| 99 | + |
| 100 | + private function priceHasCurrency(Price $price, $currency) |
| 101 | + { |
| 102 | + return !is_null($price->getValue()) && $price->getValue()->getCurrencyCode() == $currency; |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + private function priceHasCountry(Price $price, $country) |
| 107 | + { |
| 108 | + return !is_null($price->getCountry()) && $price->getCountry() == $country; |
| 109 | + } |
| 110 | + |
| 111 | + private function priceHasCustomerGroup(Price $price, CustomerGroupReference $customerGroup) |
| 112 | + { |
| 113 | + return !is_null($price->getCustomerGroup()) && $price->getCustomerGroup()->getId() == $customerGroup->getId(); |
| 114 | + } |
| 115 | + |
| 116 | + private function priceHasChannel(Price $price, ChannelReference $channel) |
| 117 | + { |
| 118 | + return !is_null($price->getChannel()) && $price->getChannel()->getId() == $channel->getId(); |
| 119 | + } |
| 120 | + |
| 121 | + private function priceHasValidDate(Price $price, \DateTime $date) |
| 122 | + { |
| 123 | + $tooEarly = !is_null($price->getValidFrom()) && $price->getValidFrom()->getDateTime() > $date; |
| 124 | + $tooLate = !is_null($price->getValidUntil()) && $price->getValidUntil()->getDateTime() < $date; |
| 125 | + return !$tooEarly && !$tooLate; |
| 126 | + } |
| 127 | + |
| 128 | + private function priceHasNoDate(Price $price) |
| 129 | + { |
| 130 | + return is_null($price->getValidFrom()) && is_null($price->getValidUntil()); |
| 131 | + } |
| 132 | + |
| 133 | + private function priceHas(Price $price, $field) { |
| 134 | + return !is_null($price->get($field)); |
| 135 | + } |
| 136 | +} |
0 commit comments