query = $query; $this->priceResolver = $priceResolver; } public function getCheapestProduct($productFamily) { $priceResolver = $this->priceResolver; return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceResolver) { return $priceResolver->getPriceWithTaxAndReduction($a) > $priceResolver->getPriceWithTaxAndReduction($b); }, true); } public function getCheapestProductByRefUnit(ProductFamilyInterface $productFamily) { $priceResolver = $this->priceResolver; return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceResolver) { return $priceResolver->getPriceByRefUnitWithTaxAndReduction($a) > $priceResolver->getPriceByRefUnitWithTaxAndReduction($b); }, false); } public function getMostExpensiveProductByRefUnit(ProductFamilyInterface $productFamily) { $priceResolver = $this->priceResolver; return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceResolver) { return $priceResolver->getPriceByRefUnitWithTaxAndReduction($a) < $priceResolver->getPriceByRefUnitWithTaxAndReduction($b); }, false); } private function getCheapestOrMostExpensiveProduct(ProductFamilyInterface $productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts) { if ($productFamily->getActiveProducts()) { $products = $productFamily->getProductsOnline()->getValues(); if (count($products) > 0) { usort($products, $comparisonFunction); return $products[0]; } } else { return $productFamily->getOriginProduct(); } if ($returnSelfIfNotActiveProducts) { return $productFamily; } else { return false; } } }