productPriceUtils = $productPriceUtils ; } public function getCheapestProduct($productFamily) { $productPriceUtils = $this->productPriceUtils ; return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) { return $productPriceUtils->getPriceWithTaxAndReduction($a) > $productPriceUtils->getPriceWithTaxAndReduction($b) ; }, true); } public function getCheapestProductByRefUnit($productFamily) { $productPriceUtils = $this->productPriceUtils ; return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) { return $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($b) ; }, false); } public function getMostExpensiveProductByRefUnit($productFamily) { $productPriceUtils = $this->productPriceUtils ; return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) { return $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $productPriceUtils->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; } } }