You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductFamilyUtils.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Lc\ShopBundle\Context\PriceUtilsInterface;
  4. class ProductFamilyUtils
  5. {
  6. protected $priceUtils ;
  7. public function __construct(PriceUtilsInterface $priceUtils)
  8. {
  9. $this->priceUtils = $priceUtils ;
  10. }
  11. public function getCheapestProduct($productFamily)
  12. {
  13. $priceUtils = $this->priceUtils ;
  14. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
  15. return $priceUtils->getPriceWithTaxAndReduction($a) > $priceUtils->getPriceWithTaxAndReduction($b) ;
  16. }, true);
  17. }
  18. public function getCheapestProductByRefUnit($productFamily)
  19. {
  20. $priceUtils = $this->priceUtils ;
  21. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
  22. return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
  23. }, false);
  24. }
  25. public function getMostExpensiveProductByRefUnit($productFamily)
  26. {
  27. $priceUtils = $this->priceUtils ;
  28. return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
  29. return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
  30. }, false);
  31. }
  32. private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts)
  33. {
  34. $products = $productFamily->getProducts()->getValues() ;
  35. if (count($products) > 0) {
  36. usort($products, $comparisonFunction);
  37. return $products[0];
  38. }
  39. if ($returnSelfIfNotActiveProducts) {
  40. return $productFamily;
  41. }
  42. else {
  43. return false;
  44. }
  45. }
  46. }