Browse Source

Refactoring

develop
Fabien Normand 2 years ago
parent
commit
fccbf1adb9
2 changed files with 61 additions and 61 deletions
  1. +22
    -44
      Builder/Order/OrderShopBuilder.php
  2. +39
    -17
      EventSubscriber/Product/UpdateProductfamilyAfterFlushEventSubscriber.php

+ 22
- 44
Builder/Order/OrderShopBuilder.php View File

use Lc\CaracoleBundle\Repository\Order\OrderStatusStore; use Lc\CaracoleBundle\Repository\Order\OrderStatusStore;
use Lc\CaracoleBundle\Repository\Product\ProductFamilyStore; use Lc\CaracoleBundle\Repository\Product\ProductFamilyStore;
use Lc\CaracoleBundle\Resolver\MerchantResolver; use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\CaracoleBundle\Resolver\OpeningResolver;
use Lc\CaracoleBundle\Resolver\OrderShopResolver; use Lc\CaracoleBundle\Resolver\OrderShopResolver;
use Lc\CaracoleBundle\Solver\Order\OrderProductReductionCatalogSolver; use Lc\CaracoleBundle\Solver\Order\OrderProductReductionCatalogSolver;
use Lc\CaracoleBundle\Solver\Order\OrderProductSolver; use Lc\CaracoleBundle\Solver\Order\OrderProductSolver;
use Lc\CaracoleBundle\Solver\Product\ProductSolver; use Lc\CaracoleBundle\Solver\Product\ProductSolver;
use Lc\CaracoleBundle\Statistic\Product\ProductsSalesStatistic; use Lc\CaracoleBundle\Statistic\Product\ProductsSalesStatistic;
use Lc\SovBundle\Model\User\UserInterface; use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Translation\FlashBagTranslator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;


protected DocumentBuilder $documentBuilder; protected DocumentBuilder $documentBuilder;
protected EventDispatcherInterface $eventDispatcher; protected EventDispatcherInterface $eventDispatcher;
protected FlashBagInterface $flashBag; protected FlashBagInterface $flashBag;
protected OpeningResolver $openingResolver;
protected ProductSolver $productSolver; protected ProductSolver $productSolver;
protected OrderShopResolver $orderShopResolver; protected OrderShopResolver $orderShopResolver;
protected OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver; protected OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver;
protected DistributionBuilder $distributionBuilder; protected DistributionBuilder $distributionBuilder;
protected MerchantResolver $merchantResolver; protected MerchantResolver $merchantResolver;
protected CreditHistoryBuilder $creditHistoryBuilder; protected CreditHistoryBuilder $creditHistoryBuilder;
protected FlashBagTranslator $flashBagTranslator;
protected OrderShopFactory $orderShopFactory; protected OrderShopFactory $orderShopFactory;
protected OrderProductSolver $orderProductSolver; protected OrderProductSolver $orderProductSolver;


PriceSolver $priceSolver, PriceSolver $priceSolver,
EventDispatcherInterface $eventDispatcher, EventDispatcherInterface $eventDispatcher,
FlashBagInterface $flashBag, FlashBagInterface $flashBag,
OpeningResolver $openingResolver,
ProductSolver $productSolver, ProductSolver $productSolver,
OrderShopResolver $orderShopResolver, OrderShopResolver $orderShopResolver,
OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver, OrderProductReductionCatalogSolver $orderProductReductionCatalogSolver,
DistributionBuilder $distributionBuilder, DistributionBuilder $distributionBuilder,
MerchantResolver $merchantResolver, MerchantResolver $merchantResolver,
CreditHistoryBuilder $creditHistoryBuilder, CreditHistoryBuilder $creditHistoryBuilder,
FlashBagTranslator $flashBagTranslator,
OrderShopFactory $orderShopFactory, OrderShopFactory $orderShopFactory,
OrderProductSolver $orderProductSolver OrderProductSolver $orderProductSolver
) { ) {
$this->priceSolver = $priceSolver; $this->priceSolver = $priceSolver;
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
$this->flashBag = $flashBag; $this->flashBag = $flashBag;
$this->openingResolver = $openingResolver;
$this->productSolver = $productSolver; $this->productSolver = $productSolver;
$this->orderShopResolver = $orderShopResolver; $this->orderShopResolver = $orderShopResolver;
$this->orderProductReductionCatalogSolver = $orderProductReductionCatalogSolver; $this->orderProductReductionCatalogSolver = $orderProductReductionCatalogSolver;
$this->distributionBuilder = $distributionBuilder; $this->distributionBuilder = $distributionBuilder;
$this->merchantResolver = $merchantResolver; $this->merchantResolver = $merchantResolver;
$this->creditHistoryBuilder = $creditHistoryBuilder; $this->creditHistoryBuilder = $creditHistoryBuilder;
$this->flashBagTranslator = $flashBagTranslator;
$this->orderShopFactory = $orderShopFactory; $this->orderShopFactory = $orderShopFactory;
$this->orderProductSolver = $orderProductSolver; $this->orderProductSolver = $orderProductSolver;
} }
$this->entityManager->flush(); $this->entityManager->flush();
} }


public function updatePriceByProductFamily(ProductFamilyInterface $productFamily)
public function updatePriceByProductFamily(ProductFamilyInterface $productFamily, SectionInterface $section)
{ {
$debug = '';

foreach ($this->merchantResolver->getCurrent()->getSections() as $section) {
// @TODO : faire la vérification isOpenSale depuis la méthode appelante
if (!$this->openingResolver->isOpenSale($section)) {
$countOrderProductUpdated = 0;
$countOrderProductUpdated = 0;


foreach ($productFamily->getProducts() as $product) {
$orderProducts = $this->orderProductStore->resetContext()->setSection(
$section
)->getInCartsByProduct($product);
foreach ($productFamily->getProducts() as $product) {
$orderProducts = $this->orderProductStore->resetContext()->setSection(
$section
)->getInCartsByProduct($product);


foreach ($orderProducts as $orderProduct) {
$quantityOrder = $orderProduct->getQuantityOrder();
$orderShop = $orderProduct->getOrderShop();
foreach ($orderProducts as $orderProduct) {
$quantityOrder = $orderProduct->getQuantityOrder();
$orderShop = $orderProduct->getOrderShop();


$orderShop->removeOrderProduct($orderProduct);
$this->entityManager->delete($orderProduct);
$this->entityManager->flush();
$this->entityManager->refresh($orderShop);
$orderShop->removeOrderProduct($orderProduct);
$this->entityManager->delete($orderProduct);
$this->entityManager->flush();
$this->entityManager->refresh($orderShop);


$orderProductFactory = new OrderProductFactory();
$addOrderProduct = $orderProductFactory->create($product, $quantityOrder);
$this->addOrderProduct($orderShop, $addOrderProduct);
$orderProductFactory = new OrderProductFactory();
$addOrderProduct = $orderProductFactory->create($product, $quantityOrder);
$this->addOrderProduct($orderShop, $addOrderProduct);


$countOrderProductUpdated++;
}
}
if ($countOrderProductUpdated) {
$this->flashBagTranslator->add(
'success',
'orderProductUpdated',
'OrderShop',
//array('%count%' => $countOrderProductUpdated)
['%count' => $debug]
);

$this->entityManager->flush();
}
return $countOrderProductUpdated;
$countOrderProductUpdated++;
} }
} }
if ($countOrderProductUpdated) {
$this->entityManager->flush();
}
return $countOrderProductUpdated;
} }





+ 39
- 17
EventSubscriber/Product/UpdateProductfamilyAfterFlushEventSubscriber.php View File



namespace Lc\CaracoleBundle\EventSubscriber\Product; namespace Lc\CaracoleBundle\EventSubscriber\Product;


use Doctrine\ORM\EntityManagerInterface;

use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent; use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Lc\CaracoleBundle\Builder\Order\OrderShopBuilder; use Lc\CaracoleBundle\Builder\Order\OrderShopBuilder;
use Lc\CaracoleBundle\Container\Product\ProductContainer;
use Lc\CaracoleBundle\Container\Product\ProductFamilyContainer;
use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface; use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
use Lc\CaracoleBundle\Model\Product\ProductInterface;
use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\CaracoleBundle\Resolver\OpeningResolver;
use Lc\SovBundle\Translation\FlashBagTranslator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;


class UpdateProductfamilyAfterFlushEventSubscriber implements EventSubscriberInterface class UpdateProductfamilyAfterFlushEventSubscriber implements EventSubscriberInterface
{ {
protected EntityManagerInterface $em;
protected ProductFamilyContainer $productFamilyContainer;
protected ProductContainer $productContainer;
protected OrderShopBuilder $orderShopBuilder; protected OrderShopBuilder $orderShopBuilder;
protected OpeningResolver $openingResolver;
protected FlashBagTranslator $flashBagTranslator;


public function __construct(EntityManagerInterface $entityManager, ProductFamilyContainer $productFamilyContainer, ProductContainer $productContainer, OrderShopBuilder $orderShopBuilder)
public function __construct(
OrderShopBuilder $orderShopBuilder,
OpeningResolver $openingResolver,
FlashBagTranslator $flashBagTranslator
)
{ {
$this->em = $entityManager;
$this->productFamilyContainer = $productFamilyContainer;
$this->productContainer = $productContainer;
$this->orderShopBuilder = $orderShopBuilder; $this->orderShopBuilder = $orderShopBuilder;
$this->openingResolver = $openingResolver;
$this->flashBagTranslator = $flashBagTranslator;
} }


public static function getSubscribedEvents() public static function getSubscribedEvents()


public function processAfterFlushProductFamily(AfterEntityUpdatedEvent $event) public function processAfterFlushProductFamily(AfterEntityUpdatedEvent $event)
{ {
$entity = $event->getEntityInstance();
if ($entity instanceof ProductFamilyInterface) {
$this->orderShopBuilder->updatePriceByProductFamily($entity);
$productFamily = $event->getEntityInstance();
if ($productFamily instanceof ProductFamilyInterface) {


foreach ($productFamily->getProductFamilySectionProperties() as $productFamilySectionProperty) {

$section = $productFamilySectionProperty->getSection();

if (!$this->openingResolver->isOpenSale($section)) {

$countOrderProductUpdated = $this->orderShopBuilder->updatePriceByProductFamily($productFamily, $section);

if ($countOrderProductUpdated) {
$this->flashBagTranslator->add(
'success',
'orderProductUpdated',
'OrderShop',
array(
'%count%' => $countOrderProductUpdated,
'%section%' => $section->getTitle()
)
);
}

}
}
} }
} }
} }

Loading…
Cancel
Save