|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- class ProductFamilyUtils
- {
- protected $priceUtils ;
-
- public function __construct(PriceUtils $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;
- }
- }
-
- }
|