|
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- use Lc\ShopBundle\Context\PriceUtilsInterface;
-
- class ProductFamilyUtils
- {
- protected $priceUtils ;
-
- public function __construct(PriceUtilsInterface $priceUtils)
- {
- $this->priceUtils = $priceUtils ;
- }
-
- public function getCheapestProduct($productFamily)
- {
- $priceUtils = $this->priceUtils ;
- return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
- return $priceUtils->getPriceWithTaxAndReduction($a) > $priceUtils->getPriceWithTaxAndReduction($b) ;
- }, true);
- }
-
- public function getCheapestProductByRefUnit($productFamily)
- {
- $priceUtils = $this->priceUtils ;
- return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
- return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
- }, false);
- }
-
- public function getMostExpensiveProductByRefUnit($productFamily)
- {
- $priceUtils = $this->priceUtils ;
- return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
- return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
- }, false);
- }
-
- private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts)
- {
- $products = $productFamily->getProducts()->getValues() ;
- if (count($products) > 0) {
- usort($products, $comparisonFunction);
- return $products[0];
- }
- if ($returnSelfIfNotActiveProducts) {
- return $productFamily;
- }
- else {
- return false;
- }
- }
-
- }
|