Browse Source

Refactoring services

packProduct
Guillaume 3 years ago
parent
commit
368951afe9
17 changed files with 383 additions and 67 deletions
  1. +37
    -0
      Builder/Merchant/MerchantBuilder.php
  2. +2
    -7
      Builder/User/UserBuilder.php
  3. +77
    -0
      Builder/User/VisitorBuilder.php
  4. +6
    -1
      Factory/User/VisitorFactory.php
  5. +0
    -1
      Repository/Config/TaxRateStore.php
  6. +7
    -0
      Repository/Merchant/MerchantRepositoryQuery.php
  7. +11
    -0
      Repository/Merchant/MerchantStore.php
  8. +88
    -1
      Repository/Product/ProductFamilyStore.php
  9. +0
    -44
      Repository/Product/ProductStore.php
  10. +7
    -0
      Repository/Section/SectionRepositoryQuery.php
  11. +23
    -0
      Repository/Section/SectionStore.php
  12. +7
    -0
      Repository/User/VisitorRepositoryQuery.php
  13. +9
    -0
      Repository/User/VisitorStore.php
  14. +54
    -12
      Resolver/MerchantResolver.php
  15. +11
    -0
      Resolver/OpeningResolver.php
  16. +1
    -1
      Resolver/Price/OrderShopPriceResolver.php
  17. +43
    -0
      Resolver/VisitorResolver.php

+ 37
- 0
Builder/Merchant/MerchantBuilder.php View File

<?php

namespace Lc\CaracoleBundle\Builder\Merchant;

use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\CaracoleBundle\Resolver\MerchantResolver;
use Lc\SovBundle\Component\CookieComponent;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Cookie;

class MerchantBuilder
{
protected ParameterBagInterface $parameterBag;
protected CookieComponent $cookieComponent;

public function __construct(
ParameterBagInterface $parameterBag,
CookieComponent $cookieComponent
) {
$this->parameterBag = $parameterBag;
$this->cookieComponent = $cookieComponent;
}

public function setCookieMerchantCurrent($response, MerchantInterface $merchant)
{
$response->headers->setCookie(
Cookie::create(
$this->parameterBag->get('app.cookie_name_merchant_current'),
$merchant->getId(),
0,
'/',
$this->cookieComponent->getCookieDomain()
)
);
}

}

+ 2
- 7
Builder/User/UserBuilder.php View File

use Lc\CaracoleBundle\Factory\User\UserPointSaleFactory; use Lc\CaracoleBundle\Factory\User\UserPointSaleFactory;
use Lc\CaracoleBundle\Model\PointSale\PointSaleInterface; use Lc\CaracoleBundle\Model\PointSale\PointSaleInterface;
use Lc\SovBundle\Model\User\UserInterface; use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Builder\User\UserBuilder as SovUserBuilder;


class UserBuilder
class UserBuilder extends SovUserBuilder
{ {
protected EntityManagerInterface $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}


public function linkToPointSale(UserInterface $user, PointSaleInterface $pointSale) public function linkToPointSale(UserInterface $user, PointSaleInterface $pointSale)
{ {

+ 77
- 0
Builder/User/VisitorBuilder.php View File

<?php

namespace Lc\CaracoleBundle\Builder\User;

use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Factory\User\VisitorFactory;
use Lc\CaracoleBundle\Model\User\VisitorInterface;
use Lc\SovBundle\Component\CookieComponent;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Cookie;

class VisitorBuilder
{
protected CookieComponent $cookieComponent;
protected ParameterBagInterface $parameterBag;
protected EntityManagerInterface $entityManager;

public function __construct(
EntityManagerInterface $entityManager,
CookieComponent $cookieComponent,
ParameterBagInterface $parameterBag
) {
$this->entityManager = $entityManager;
$this->cookieComponent = $cookieComponent;
$this->parameterBag = $parameterBag;
}

// addVisitor
public function create(string $cookie, string $ip)
{
$visitorFactory = new VisitorFactory();
$visitor = $visitorFactory->create($cookie, $ip);

$this->entityManager->persist($visitor);
$this->entityManager->flush();
}

public function update(VisitorInterface $visitor)
{
$totalVisit = $visitor->getTotalVisit() + 1;

$visitor->setTotalVisit($totalVisit);
$visitor->setLastAccess(new \DateTime());

$this->entityManager->persist($visitor);
$this->entityManager->flush();
}

// setCookieVisitor
public function setCookie($response, $cookie)
{
$response->headers->setCookie(
Cookie::create(
$this->parameterBag->get('app.cookie_name_visitor'),
$this->cookieComponent->cryptCookie($cookie),
new \DateTime('+2 months'),
'/',
$this->cookieComponent->getCookieDomain()
)
);
}

// updateVisitorCookie
public function updateCookie($response)
{
$response->headers->setCookie(
Cookie::create(
$this->parameterBag->get('app.cookie_name_visitor'),
$this->cookieComponent->cryptCookie($this->getVisitorCurrent()->getCookie()),
new \DateTime('+2 months'),
'/',
$this->cookieComponent->getCookieDomain()
)
);
}

}

+ 6
- 1
Factory/User/VisitorFactory.php View File



class VisitorFactory extends AbstractFactory class VisitorFactory extends AbstractFactory
{ {
public function create(): VisitorInterface
public function create(string $cookie, string $ip): VisitorInterface
{ {
$visitor = new Visitor(); $visitor = new Visitor();


$visitor->setCookie($cookie);
$visitor->setIp($ip);
$visitor->setTotalVisit(1);
$visitor->setLastAccess(new \DateTime());

return $visitor; return $visitor;
} }



+ 0
- 1
Repository/Config/TaxRateStore.php View File

$this->merchantResolver = $merchantResolver; $this->merchantResolver = $merchantResolver;
} }



public function getAsArray() public function getAsArray()
{ {
$query = $this->query->create(); $query = $this->query->create();

+ 7
- 0
Repository/Merchant/MerchantRepositoryQuery.php View File

{ {
parent::__construct($repository, 'r', $paginator); parent::__construct($repository, 'r', $paginator);
} }

public function filterByDevAlias(string $devAlias)
{
return $this->andWhere('.devAlias LIKE :devAlias')
->setParameter('devAlias', $devAlias);
}

} }

+ 11
- 0
Repository/Merchant/MerchantStore.php View File



namespace Lc\CaracoleBundle\Repository\Merchant; namespace Lc\CaracoleBundle\Repository\Merchant;


use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\SovBundle\Repository\AbstractStore; use Lc\SovBundle\Repository\AbstractStore;


class MerchantStore extends AbstractStore class MerchantStore extends AbstractStore
{ {
$this->query = $query; $this->query = $query;
} }

public function getOneByDevAlias(string $devAlias): ?MerchantInterface
{
$query = $this->query->create();

$query->filterByDevAlias($devAlias);

return $query->findOne();
}

} }

+ 88
- 1
Repository/Product/ProductFamilyStore.php View File

namespace Lc\CaracoleBundle\Repository\Product; namespace Lc\CaracoleBundle\Repository\Product;


use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface; use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
use Lc\CaracoleBundle\Resolver\Price\PriceResolver; use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
use Lc\SovBundle\Repository\AbstractStore; use Lc\SovBundle\Repository\AbstractStore;


class ProductFamilyStore extends AbstractStore class ProductFamilyStore extends AbstractStore
{ {
protected ProductFamilyRepositoryQuery $query; protected ProductFamilyRepositoryQuery $query;
protected PriceResolver $priceResolver;


public function __construct(ProductFamilyRepositoryQuery $query)
public function __construct(ProductFamilyRepositoryQuery $query, PriceResolver $priceResolver)
{ {
$this->query = $query; $this->query = $query;
$this->priceResolver = $priceResolver;
} }


public function isOneProductAvailableAddCart(array $products): bool public function isOneProductAvailableAddCart(array $products): bool


return false; return false;
} }

public function getMultiplyingFactor($productFamily)
{
if ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
if ($productFamily->getBuyingPrice() > 0) {
return number_format(
$this->priceResolver->getPriceWithTax($productFamily) / $productFamily->getBuyingPrice(),
3
);
}
} elseif ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
if ($productFamily->getBuyingPriceByRefUnit() > 0) {
return number_format(
$this->priceResolver->getPriceByRefUnitWithTax(
$productFamily
) / $productFamily->getBuyingPriceByRefUnit(),
3
);
}
}
}

public function getCheapestProduct(ProductFamilyInterface $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;
}
}
} }

+ 0
- 44
Repository/Product/ProductStore.php View File

class ProductStore extends AbstractStore class ProductStore extends AbstractStore
{ {
protected ProductRepositoryQuery $query; protected ProductRepositoryQuery $query;
protected PriceResolver $priceResolver;


public function __construct(ProductRepositoryQuery $query, PriceResolver $priceResolver) public function __construct(ProductRepositoryQuery $query, PriceResolver $priceResolver)
{ {
$this->query = $query; $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;
}
} }
} }

+ 7
- 0
Repository/Section/SectionRepositoryQuery.php View File

{ {
parent::__construct($repository, 'r', $paginator); parent::__construct($repository, 'r', $paginator);
} }

public function filterByDevAlias(string $devAlias)
{
return $this
->andWhere('.devAlias LIKE :devAlias')
->setParameter('devAlias', $devAlias);
}
} }

+ 23
- 0
Repository/Section/SectionStore.php View File



namespace Lc\CaracoleBundle\Repository\Section; namespace Lc\CaracoleBundle\Repository\Section;


use Lc\CaracoleBundle\Model\Section\SectionInterface;
use Lc\CaracoleBundle\Repository\MerchantStoreTrait;
use Lc\SovBundle\Repository\AbstractStore; use Lc\SovBundle\Repository\AbstractStore;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;


class SectionStore extends AbstractStore class SectionStore extends AbstractStore
{ {
use MerchantStoreTrait;

protected SectionRepositoryQuery $query; protected SectionRepositoryQuery $query;


public function __construct(SectionRepositoryQuery $query) public function __construct(SectionRepositoryQuery $query)
{ {
$this->query = $query; $this->query = $query;
} }

// getSection
public function getOneByDevAlias(string $devAlias): ?SectionInterface
{
$query = $this->query->create();

$query
->filterByMerchant($this->merchant)
->filterByDevAlias($devAlias);

$section = $query->findOne();

if (!$section) {
throw new NotFoundHttpException('La section ' . $devAlias . ' est introuvable');
}

return $section;
}
} }

+ 7
- 0
Repository/User/VisitorRepositoryQuery.php View File

{ {
parent::__construct($repository, 'r', $paginator); parent::__construct($repository, 'r', $paginator);
} }

public function filterByCookie(string $cookie)
{
return $this
->andWhere('.cookie LIKE :cookie')
->setParameter('cookie', $cookie);
}
} }

+ 9
- 0
Repository/User/VisitorStore.php View File

{ {
$this->query = $query; $this->query = $query;
} }

public function getOneByCookie(string $cookie)
{
$query = $this->query->create();

$query->filterByCookie($cookie);

return $query->findOne();
}
} }

+ 54
- 12
Resolver/MerchantResolver.php View File

use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\CaracoleBundle\Model\User\UserMerchantInterface; use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository; use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
use Lc\CaracoleBundle\Repository\User\UserMerchantRepository; use Lc\CaracoleBundle\Repository\User\UserMerchantRepository;
use Lc\SovBundle\Model\User\UserInterface; use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Resolver\UrlResolver; use Lc\SovBundle\Resolver\UrlResolver;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;


class MerchantResolver class MerchantResolver
{ {
protected $requestStack;
protected $em;
protected $urlResolver;
protected $security;
protected $currentMerchant;
protected $merchantRepository;
protected $userMerrchantRepository;
protected ?MerchantInterface $currentMerchant;
protected RequestStack $requestStack;
protected EntityManagerInterface $em;
protected UrlResolver $urlResolver;
protected Security $security;
protected MerchantRepository $merchantRepository;
protected UserMerchantRepository $userMerchantRepository;
protected UrlGeneratorInterface $router;
protected MerchantStore $merchantStore;
protected ParameterBagInterface $parameterBag;


public function __construct( public function __construct(
EntityManagerInterface $entityManager, EntityManagerInterface $entityManager,
UrlResolver $urlResolver, UrlResolver $urlResolver,
Security $security, Security $security,
MerchantRepository $merchantRepository, MerchantRepository $merchantRepository,
UserMerchantRepository $userMerchantRepository
UserMerchantRepository $userMerchantRepository,
UrlGeneratorInterface $router,
MerchantStore $merchantStore,
ParameterBagInterface $parameterBag
) { ) {
$this->requestStack = $requestStack; $this->requestStack = $requestStack;
$this->em = $entityManager; $this->em = $entityManager;
$this->security = $security; $this->security = $security;
$this->merchantRepository = $merchantRepository; $this->merchantRepository = $merchantRepository;
$this->userMerchantRepository = $userMerchantRepository; $this->userMerchantRepository = $userMerchantRepository;
$this->merchantStore = $merchantStore;
$this->router = $router;
$this->parameterBag = $parameterBag;
} }


public function getCurrent(): MerchantInterface public function getCurrent(): MerchantInterface
{ {
if ($this->currentMerchant) {
if (isset($this->currentMerchant) && $this->currentMerchant) {
return $this->currentMerchant; return $this->currentMerchant;
} }


$this->currentMerchant = false;
$this->currentMerchant = null;
$request = $this->requestStack->getCurrentRequest(); $request = $this->requestStack->getCurrentRequest();
$merchants = $this->merchantRepository->findAll(); $merchants = $this->merchantRepository->findAll();
$isCli = php_sapi_name() === 'cli'; $isCli = php_sapi_name() === 'cli';


if ($request || $isCli) {
if ($request || $isCli) {
if ($isCli || $this->urlResolver->isServerLocalhost()) { if ($isCli || $this->urlResolver->isServerLocalhost()) {
foreach ($merchants as $merchant) { foreach ($merchants as $merchant) {
if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) { if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) {
UserInterface $user = null, UserInterface $user = null,
MerchantInterface $merchant = null MerchantInterface $merchant = null
): ?UserMerchantInterface { ): ?UserMerchantInterface {

if ($user === null) { if ($user === null) {
$user = $this->security->getUser(); $user = $this->security->getUser();
} }
); );
} }


public function getUrl($merchant)
{
if ($this->urlResolver->isServerLocalhost()) {
return $this->router->generate('frontend_home', [], UrlGeneratorInterface::ABSOLUTE_URL);
} else {
return $merchant->getSettingValue(MerchantSettingDefinition::SETTING_URL);
}
}

public function getMerchantUser(UserInterface $user = null)
{
$merchants = $this->merchantStore->findAll();

if ($user) {
return $user->getMerchant();
} else {
$merchantCurrentId = $this->requestStack->getCurrentRequest()->cookies->getInt(
$this->parameterBag->get('app.cookie_name_merchant_current')
);
if ($merchantCurrentId) {
foreach ($merchants as $merchant) {
if ($merchant->getId() == $merchantCurrentId) {
return $merchant;
}
}
}
}

return false;
}

} }

+ 11
- 0
Resolver/OpeningResolver.php View File

return false; return false;
} }



public function getDateEndCurrentSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à')
{
// @TODO : à réécrire
}

public function getDateBeginNextSale(SectionInterface $section, $formatDate = '', $delimiterDayTime = 'à')
{
// @TODO : à réécrire
}

public function getMessages(): array public function getMessages(): array
{ {
return $this->messages; return $this->messages;

+ 1
- 1
Resolver/Price/OrderShopPriceResolver.php View File

{ {
use PriceResolverTrait; use PriceResolverTrait;


protected $orderProductPriceResolver;
protected OrderProductPriceResolver $orderProductPriceResolver;


public function __construct(OrderProductPriceResolver $orderProductPriceResolver) public function __construct(OrderProductPriceResolver $orderProductPriceResolver)
{ {

+ 43
- 0
Resolver/VisitorResolver.php View File

<?php

namespace Lc\CaracoleBundle\Resolver;

use Lc\CaracoleBundle\Repository\User\VisitorStore;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class VisitorResolver
{
protected array $visitor;
protected RequestStack $requestStack;
protected ParameterBagInterface $parameterBag;
protected VisitorStore $visitorStore;

public function __construct(
RequestStack $requestStack,
ParameterBagInterface $parameterBag,
VisitorStore $visitorStore
) {
$this->requestStack = $requestStack;
$this->parameterBag = $parameterBag;
$this->visitorStore = $visitorStore;
}

public function getCurrent()
{
$cookie = $this->requestStack->getCurrentRequest()->cookies->get(
$this->parameterBag->get('app.cookie_name_visitor')
);
return $this->getVisitor($cookie);
}

private function getVisitor($cookie)
{
if (!isset($this->visitor[$cookie])) {
$this->visitor[$cookie] = $this->visitorStore->getOneByCookie($cookie);
}

return $this->visitor[$cookie];
}

}

Loading…
Cancel
Save