Composable Commerce PHP SDKPHP SDK
  • Namespace
  • Class
  • Tree

Namespaces

  • Commercetools
    • Commons
      • Helper
    • Core
      • Builder
        • Request
        • Update
      • Cache
      • Client
        • Adapter
        • OAuth
      • Error
      • Helper
        • Annotate
        • State
          • Renderer
        • Subscriber
          • Log
      • Model
        • ApiClient
        • Cart
        • CartDiscount
        • Category
        • Channel
        • Common
        • Customer
        • CustomerGroup
        • CustomField
        • CustomObject
        • DiscountCode
        • Extension
        • Inventory
        • Message
        • Order
        • OrderEdit
        • Payment
        • Product
          • Search
        • ProductDiscount
        • ProductSelection
        • ProductType
        • Project
        • Review
        • ShippingMethod
        • ShoppingList
        • State
        • Store
        • Subscription
        • TaxCategory
        • Type
        • Zone
      • Request
        • ApiClients
        • CartDiscounts
          • Command
        • Carts
          • Command
        • Categories
          • Command
        • Channels
          • Command
        • CustomerGroups
          • Command
        • Customers
          • Command
        • CustomField
          • Command
        • CustomObjects
        • DiscountCodes
          • Command
        • Extensions
          • Command
        • GraphQL
        • InStores
        • Inventory
          • Command
        • Me
          • Command
        • Messages
        • OrderEdits
          • Command
          • StagedOrder
            • Command
        • Orders
          • Command
        • Payments
          • Command
        • ProductDiscounts
          • Command
        • Products
          • Command
        • ProductSelections
          • Command
        • ProductTypes
          • Command
        • Project
          • Command
        • Query
        • Reviews
          • Command
        • ShippingMethods
          • Command
        • ShoppingLists
          • Command
        • States
          • Command
        • Stores
          • Command
        • Subscriptions
          • Command
        • TaxCategories
          • Command
        • Types
          • Command
        • Zones
          • Command
      • Response

Classes

  • PriceFinder
  • QueryHelper
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 
<?php
/**
 * @author @ct-jensschulze <jens.schulze@commercetools.de>
 */

namespace Commercetools\Commons\Helper;

use Commercetools\Core\Model\Channel\ChannelReference;
use Commercetools\Core\Model\Common\Price;
use Commercetools\Core\Model\Common\PriceCollection;
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;

/**
 * @deprecated Please use the price selection functionality of Composable Commerce
 * @link http://docs.commercetools.com/http-api-projects-products.html#price-selection
 */
class PriceFinder
{
    /**
     * @var string
     */
    private $currency;

    /**
     * @var string
     */
    private $country;

    /**
     * @var CustomerGroupReference
     */
    private $customerGroup;

    /**
     * @var ChannelReference
     */
    private $channel;

    /**
     * PriceFinder constructor.
     * @param string $currency
     * @param string $country
     * @param CustomerGroupReference $customerGroup
     * @param ChannelReference $channel
     */
    public function __construct(
        $currency,
        $country = null,
        CustomerGroupReference $customerGroup = null,
        ChannelReference $channel = null
    ) {
        $this->currency = $currency;
        $this->country = $country;
        $this->customerGroup = $customerGroup;
        $this->channel = $channel;
    }

    /**
     * @param PriceCollection $prices
     * @return Price
     */
    public function findPrice(PriceCollection $prices)
    {
        $currency = $this->currency;
        $country = $this->country;
        $customerGroup = $this->customerGroup;
        $channel = $this->channel;

        $prices = new \CallbackFilterIterator(
            $prices,
            function ($price) use ($currency, $country, $customerGroup, $channel) {
                if (!$this->priceHasCurrency($price, $currency)) {
                    return false;
                }
                if (!$this->priceHasNoDate($price) && !$this->priceHasValidDate($price, new \DateTime())) {
                    return false;
                }
                if (is_null($country)) {
                    if ($this->priceHas($price, 'country')) {
                        return false;
                    }
                } elseif (!$this->priceHasCountry($price, $country)) {
                    return false;
                }
                if (is_null($customerGroup)) {
                    if ($this->priceHas($price, 'customerGroup')) {
                        return false;
                    }
                } elseif (!$this->priceHasCustomerGroup($price, $customerGroup)) {
                    return false;
                }
                if (is_null($channel)) {
                    if ($this->priceHas($price, 'channel')) {
                        return false;
                    }
                } elseif (!$this->priceHasChannel($price, $channel)) {
                    return false;
                }
                return true;
            }
        );

        foreach ($prices as $price) {
            return $price;
        }
        return null;
    }

    /**
     * @param $prices
     * @param string $currency
     * @param string $country
     * @param CustomerGroupReference $customerGroup
     * @param ChannelReference $channel
     * @return Price|null
     */
    public static function findPriceFor(
        $prices,
        $currency,
        $country = null,
        CustomerGroupReference $customerGroup = null,
        ChannelReference $channel = null
    ) {
        $priceFinder = new static($currency, $country, $customerGroup, $channel);

        return $priceFinder->findPrice($prices);
    }

    /**
     * @param Price $price
     * @param string $currency
     * @return bool
     */
    private function priceHasCurrency(Price $price, $currency)
    {
        return !is_null($price->getValue()) && $price->getValue()->getCurrencyCode() == $currency;
    }

    /**
     * @param Price $price
     * @param string $country
     * @return bool
     */
    private function priceHasCountry(Price $price, $country)
    {
        return !is_null($price->getCountry()) && $price->getCountry() == $country;
    }

    /**
     * @param Price $price
     * @param CustomerGroupReference $customerGroup
     * @return bool
     */
    private function priceHasCustomerGroup(Price $price, CustomerGroupReference $customerGroup)
    {
        return !is_null($price->getCustomerGroup()) && $price->getCustomerGroup()->getId() == $customerGroup->getId();
    }

    /**
     * @param Price $price
     * @param ChannelReference $channel
     * @return bool
     */
    private function priceHasChannel(Price $price, ChannelReference $channel)
    {
        return !is_null($price->getChannel()) && $price->getChannel()->getId() == $channel->getId();
    }

    /**
     * @param Price $price
     * @param \DateTime $date
     * @return bool
     */
    private function priceHasValidDate(Price $price, \DateTime $date)
    {
        $tooEarly = !is_null($price->getValidFrom()) && $price->getValidFrom()->getDateTime() > $date;
        $tooLate = !is_null($price->getValidUntil()) && $price->getValidUntil()->getDateTime() < $date;
        return !$tooEarly && !$tooLate;
    }

    /**
     * @param Price $price
     * @return bool
     */
    private function priceHasNoDate(Price $price)
    {
        return is_null($price->getValidFrom()) && is_null($price->getValidUntil());
    }

    /**
     * @param Price $price
     * @param string $field
     * @return bool
     */
    private function priceHas(Price $price, $field)
    {
        return !is_null($price->get($field));
    }
}
PHP SDK API documentation generated by ApiGen