commercetools /
commercetools-php-sdk
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @author @ct-jensschulze <[email protected]> |
||
| 4 | */ |
||
| 5 | |||
| 6 | namespace Commercetools\Commons\Helper; |
||
| 7 | |||
| 8 | use Commercetools\Core\Model\Channel\ChannelReference; |
||
| 9 | use Commercetools\Core\Model\Common\Price; |
||
| 10 | use Commercetools\Core\Model\Common\PriceCollection; |
||
| 11 | use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @deprecated Please use the price selection functionality of the platform |
||
| 15 | * @link http://docs.commercetools.com/http-api-projects-products.html#price-selection |
||
| 16 | */ |
||
| 17 | class PriceFinder |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $currency; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $country; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var CustomerGroupReference |
||
| 31 | */ |
||
| 32 | private $customerGroup; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ChannelReference |
||
| 36 | */ |
||
| 37 | private $channel; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * PriceFinder constructor. |
||
| 41 | * @param string $currency |
||
| 42 | * @param string $country |
||
| 43 | * @param CustomerGroupReference $customerGroup |
||
| 44 | * @param ChannelReference $channel |
||
| 45 | */ |
||
| 46 | 26 | public function __construct( |
|
| 47 | $currency, |
||
| 48 | $country = null, |
||
| 49 | CustomerGroupReference $customerGroup = null, |
||
| 50 | ChannelReference $channel = null |
||
| 51 | ) { |
||
| 52 | 26 | $this->currency = $currency; |
|
| 53 | 26 | $this->country = $country; |
|
| 54 | 26 | $this->customerGroup = $customerGroup; |
|
| 55 | 26 | $this->channel = $channel; |
|
| 56 | 26 | } |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param PriceCollection $prices |
||
| 60 | * @return Price |
||
| 61 | */ |
||
| 62 | 26 | public function findPrice(PriceCollection $prices) |
|
| 63 | { |
||
| 64 | 26 | $currency = $this->currency; |
|
| 65 | 26 | $country = $this->country; |
|
| 66 | 26 | $customerGroup = $this->customerGroup; |
|
| 67 | 26 | $channel = $this->channel; |
|
| 68 | |||
| 69 | 26 | $prices = new \CallbackFilterIterator( |
|
| 70 | 26 | $prices, |
|
| 71 | 26 | function ($price) use ($currency, $country, $customerGroup, $channel) { |
|
| 72 | 26 | if (!$this->priceHasCurrency($price, $currency)) { |
|
| 73 | 17 | return false; |
|
| 74 | } |
||
| 75 | 26 | if (!$this->priceHasNoDate($price) && !$this->priceHasValidDate($price, new \DateTime())) { |
|
| 76 | return false; |
||
| 77 | } |
||
| 78 | 26 | if (is_null($country)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 79 | 6 | if ($this->priceHas($price, 'country')) { |
|
| 80 | 6 | return false; |
|
| 81 | } |
||
| 82 | 20 | } elseif (!$this->priceHasCountry($price, $country)) { |
|
| 83 | 17 | return false; |
|
| 84 | } |
||
| 85 | 26 | if (is_null($customerGroup)) { |
|
| 86 | 12 | if ($this->priceHas($price, 'customerGroup')) { |
|
| 87 | 12 | return false; |
|
| 88 | } |
||
| 89 | 14 | } elseif (!$this->priceHasCustomerGroup($price, $customerGroup)) { |
|
| 90 | 11 | return false; |
|
| 91 | } |
||
| 92 | 26 | if (is_null($channel)) { |
|
| 93 | 12 | if ($this->priceHas($price, 'channel')) { |
|
| 94 | 12 | return false; |
|
| 95 | } |
||
| 96 | 14 | } elseif (!$this->priceHasChannel($price, $channel)) { |
|
| 97 | 9 | return false; |
|
| 98 | } |
||
| 99 | 26 | return true; |
|
| 100 | 26 | } |
|
| 101 | ); |
||
| 102 | |||
| 103 | 26 | foreach ($prices as $price) { |
|
| 104 | 26 | return $price; |
|
| 105 | } |
||
| 106 | return null; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param $prices |
||
| 111 | * @param string $currency |
||
| 112 | * @param string $country |
||
| 113 | * @param CustomerGroupReference $customerGroup |
||
| 114 | * @param ChannelReference $channel |
||
| 115 | * @return Price|null |
||
| 116 | */ |
||
| 117 | 26 | public static function findPriceFor( |
|
| 118 | $prices, |
||
| 119 | $currency, |
||
| 120 | $country = null, |
||
| 121 | CustomerGroupReference $customerGroup = null, |
||
| 122 | ChannelReference $channel = null |
||
| 123 | ) { |
||
| 124 | 26 | $priceFinder = new static($currency, $country, $customerGroup, $channel); |
|
| 125 | |||
| 126 | 26 | return $priceFinder->findPrice($prices); |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param Price $price |
||
| 131 | * @param string $currency |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | 26 | private function priceHasCurrency(Price $price, $currency) |
|
| 135 | { |
||
| 136 | 26 | return !is_null($price->getValue()) && $price->getValue()->getCurrencyCode() == $currency; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param Price $price |
||
| 141 | * @param string $country |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | 20 | private function priceHasCountry(Price $price, $country) |
|
| 145 | { |
||
| 146 | 20 | return !is_null($price->getCountry()) && $price->getCountry() == $country; |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param Price $price |
||
| 151 | * @param CustomerGroupReference $customerGroup |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | 14 | private function priceHasCustomerGroup(Price $price, CustomerGroupReference $customerGroup) |
|
| 155 | { |
||
| 156 | 14 | return !is_null($price->getCustomerGroup()) && $price->getCustomerGroup()->getId() == $customerGroup->getId(); |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param Price $price |
||
| 161 | * @param ChannelReference $channel |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | 14 | private function priceHasChannel(Price $price, ChannelReference $channel) |
|
| 165 | { |
||
| 166 | 14 | return !is_null($price->getChannel()) && $price->getChannel()->getId() == $channel->getId(); |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param Price $price |
||
| 171 | * @param \DateTime $date |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | private function priceHasValidDate(Price $price, \DateTime $date) |
||
| 175 | { |
||
| 176 | $tooEarly = !is_null($price->getValidFrom()) && $price->getValidFrom()->getDateTime() > $date; |
||
| 177 | $tooLate = !is_null($price->getValidUntil()) && $price->getValidUntil()->getDateTime() < $date; |
||
| 178 | return !$tooEarly && !$tooLate; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param Price $price |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | 26 | private function priceHasNoDate(Price $price) |
|
| 186 | { |
||
| 187 | 26 | return is_null($price->getValidFrom()) && is_null($price->getValidUntil()); |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param Price $price |
||
| 192 | * @param string $field |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | 18 | private function priceHas(Price $price, $field) |
|
| 196 | { |
||
| 197 | 18 | return !is_null($price->get($field)); |
|
| 198 | } |
||
| 199 | } |
||
| 200 |