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.0KB

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