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

@@ -0,0 +1,37 @@
<?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

@@ -6,15 +6,10 @@ use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Factory\User\UserPointSaleFactory;
use Lc\CaracoleBundle\Model\PointSale\PointSaleInterface;
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)
{

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

@@ -0,0 +1,77 @@
<?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

@@ -8,10 +8,15 @@ use Lc\SovBundle\Factory\AbstractFactory;

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

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

return $visitor;
}


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

@@ -16,7 +16,6 @@ class TaxRateStore extends AbstractStore
$this->merchantResolver = $merchantResolver;
}


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

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

@@ -11,4 +11,11 @@ class MerchantRepositoryQuery extends AbstractRepositoryQuery
{
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

@@ -2,6 +2,7 @@

namespace Lc\CaracoleBundle\Repository\Merchant;

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

class MerchantStore extends AbstractStore
@@ -12,4 +13,14 @@ class MerchantStore extends AbstractStore
{
$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

@@ -3,16 +3,19 @@
namespace Lc\CaracoleBundle\Repository\Product;

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

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

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

public function isOneProductAvailableAddCart(array $products): bool
@@ -25,4 +28,88 @@ class ProductFamilyStore extends AbstractStore

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

@@ -9,53 +9,9 @@ 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;
}
}
}

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

@@ -14,4 +14,11 @@ class SectionRepositoryQuery extends AbstractRepositoryQuery
{
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

@@ -2,14 +2,37 @@

namespace Lc\CaracoleBundle\Repository\Section;

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

class SectionStore extends AbstractStore
{
use MerchantStoreTrait;

protected SectionRepositoryQuery $query;

public function __construct(SectionRepositoryQuery $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

@@ -11,4 +11,11 @@ class VisitorRepositoryQuery extends AbstractRepositoryQuery
{
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

@@ -12,4 +12,13 @@ class VisitorStore extends AbstractStore
{
$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

@@ -13,21 +13,27 @@ use Lc\CaracoleBundle\Definition\MerchantSettingDefinitionInterface;
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
use Lc\CaracoleBundle\Repository\User\UserMerchantRepository;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Resolver\UrlResolver;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;

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(
EntityManagerInterface $entityManager,
@@ -35,7 +41,10 @@ class MerchantResolver
UrlResolver $urlResolver,
Security $security,
MerchantRepository $merchantRepository,
UserMerchantRepository $userMerchantRepository
UserMerchantRepository $userMerchantRepository,
UrlGeneratorInterface $router,
MerchantStore $merchantStore,
ParameterBagInterface $parameterBag
) {
$this->requestStack = $requestStack;
$this->em = $entityManager;
@@ -43,20 +52,23 @@ class MerchantResolver
$this->security = $security;
$this->merchantRepository = $merchantRepository;
$this->userMerchantRepository = $userMerchantRepository;
$this->merchantStore = $merchantStore;
$this->router = $router;
$this->parameterBag = $parameterBag;
}

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

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

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

if ($user === null) {
$user = $this->security->getUser();
}
@@ -110,4 +121,35 @@ class MerchantResolver
);
}

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

@@ -130,6 +130,17 @@ class OpeningResolver
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
{
return $this->messages;

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

@@ -11,7 +11,7 @@ class OrderShopPriceResolver
{
use PriceResolverTrait;

protected $orderProductPriceResolver;
protected OrderProductPriceResolver $orderProductPriceResolver;

public function __construct(OrderProductPriceResolver $orderProductPriceResolver)
{

+ 43
- 0
Resolver/VisitorResolver.php View File

@@ -0,0 +1,43 @@
<?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