Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

105 lines
3.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Builder\User;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Factory\User\UserMerchantFactory;
  5. use Lc\CaracoleBundle\Model\Credit\CreditHistoryInterface;
  6. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  7. use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
  8. use Lc\CaracoleBundle\Repository\User\UserMerchantStore;
  9. use Lc\SovBundle\Model\User\UserInterface;
  10. class UserMerchantBuilder
  11. {
  12. protected EntityManagerInterface $entityManager;
  13. protected UserMerchantStore $userMerchantStore;
  14. public function __construct(
  15. EntityManagerInterface $entityManager,
  16. UserMerchantStore $userMerchantStore
  17. ) {
  18. $this->entityManager = $entityManager;
  19. $this->userMerchantStore = $userMerchantStore;
  20. }
  21. public function createIfNotExist(UserInterface $user, MerchantInterface $merchant): UserMerchantInterface
  22. {
  23. $userMerchant = $this->userMerchantStore
  24. ->setMerchant($merchant)
  25. ->getOneByUser($user);
  26. if (!$userMerchant) {
  27. $userMerchantFactory = new UserMerchantFactory();
  28. $userMerchant = $userMerchantFactory->create($user, $merchant);
  29. }
  30. return $userMerchant;
  31. }
  32. public function init(
  33. UserInterface $user,
  34. MerchantInterface $merchant,
  35. bool $active = true,
  36. bool $creditActive = false,
  37. float $credit = null,
  38. bool $persist = true
  39. ): UserMerchantInterface {
  40. $userMerchant = $this->createIfNotExist($user, $merchant);
  41. $userMerchant->setActive($active);
  42. $userMerchant->setCreditActive($creditActive);
  43. $userMerchant->setCredit($credit);
  44. if ($persist) {
  45. //TODO create ou update ???
  46. $this->entityManager->persist($userMerchant);
  47. $this->entityManager->flush();
  48. }
  49. return $userMerchant;
  50. }
  51. public function updateCreditActive(
  52. UserInterface $user,
  53. MerchantInterface $merchant,
  54. $creditActive = true
  55. ): UserMerchantInterface {
  56. $userMerchant = $this->createIfNotExist($user, $merchant);
  57. $userMerchant->setCreditActive($creditActive);
  58. //TODO create ou update ???
  59. $this->entityManager->persist($userMerchant);
  60. $this->entityManager->flush();
  61. return $userMerchant;
  62. }
  63. public function activeCredit(UserInterface $user, MerchantInterface $merchant): UserMerchantInterface
  64. {
  65. return $this->updateCreditActive($user, $merchant, true);
  66. }
  67. public function unactiveCredit(UserInterface $user, MerchantInterface $merchant): UserMerchantInterface
  68. {
  69. return $this->updateCreditActive($user, $merchant, false);
  70. }
  71. public function updateCredit(
  72. UserMerchantInterface $userMerchant,
  73. CreditHistoryInterface $creditHistory,
  74. float $amount
  75. ): UserMerchantInterface {
  76. $userMerchant->setCredit($amount);
  77. $this->entityManager->update($creditHistory);
  78. $this->entityManager->update($userMerchant);
  79. $this->entityManager->flush();
  80. return $userMerchant;
  81. }
  82. }