Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

UserMerchantBuilder.php 3.4KB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, $flush = true): UserMerchantInterface
  22. {
  23. $userMerchant = $this->userMerchantStore
  24. ->setMerchant($merchant)
  25. ->getOneByUser($user);
  26. if (!$userMerchant) {
  27. $userMerchantFactory = new UserMerchantFactory();
  28. $userMerchant = $userMerchantFactory->create($merchant, $user);
  29. $this->entityManager->create($userMerchant);
  30. if($flush) {
  31. $this->entityManager->flush();
  32. }
  33. }elseif(!$userMerchant->getActive()){
  34. $userMerchant->setActive(true);
  35. $this->entityManager->update($userMerchant);
  36. if($flush) {
  37. $this->entityManager->flush();
  38. }
  39. }
  40. return $userMerchant;
  41. }
  42. public function init(
  43. UserInterface $user,
  44. MerchantInterface $merchant,
  45. bool $active = true,
  46. bool $creditActive = false,
  47. float $credit = null,
  48. bool $persist = true
  49. ): UserMerchantInterface {
  50. $userMerchant = $this->createIfNotExist($user, $merchant);
  51. $userMerchant->setActive($active);
  52. $userMerchant->setCreditActive($creditActive);
  53. $userMerchant->setCredit($credit);
  54. if ($persist) {
  55. //TODO create ou update ???
  56. $this->entityManager->persist($userMerchant);
  57. $this->entityManager->flush();
  58. }
  59. return $userMerchant;
  60. }
  61. public function activeCredit(UserMerchantInterface $userMerchant): UserMerchantInterface
  62. {
  63. return $this->updateCreditActive($userMerchant, true);
  64. }
  65. public function unactiveCredit(UserMerchantInterface $userMerchant): UserMerchantInterface
  66. {
  67. return $this->updateCreditActive($userMerchant, false);
  68. }
  69. public function updateCredit(
  70. UserMerchantInterface $userMerchant,
  71. CreditHistoryInterface $creditHistory,
  72. float $amount
  73. ): UserMerchantInterface {
  74. $userMerchant->setCredit($amount);
  75. $this->entityManager->update($creditHistory);
  76. $this->entityManager->update($userMerchant);
  77. $this->entityManager->flush();
  78. return $userMerchant;
  79. }
  80. public function updateCreditActive(
  81. UserMerchantInterface $userMerchant,
  82. $creditActive = true
  83. ): UserMerchantInterface {
  84. $userMerchant->setCreditActive($creditActive);
  85. //TODO create ou update ???
  86. $this->entityManager->persist($userMerchant);
  87. $this->entityManager->flush();
  88. return $userMerchant;
  89. }
  90. }