|
- <?php
-
- namespace Lc\CaracoleBundle\Repository\Product;
-
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
- use Lc\SovBundle\Repository\AbstractStore;
-
- class ProductStore extends AbstractStore
- {
- protected ProductRepositoryQuery $query;
- protected PriceResolver $priceResolver;
-
- public function __construct(ProductRepositoryQuery $query, PriceResolver $priceResolver)
- {
- $this->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;
- }
- }
- }
|