You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 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($merchant, $user);
  29. $this->entityManager->create($userMerchant);
  30. $this->entityManager->flush();
  31. }
  32. return $userMerchant;
  33. }
  34. public function init(
  35. UserInterface $user,
  36. MerchantInterface $merchant,
  37. bool $active = true,
  38. bool $creditActive = false,
  39. float $credit = null,
  40. bool $persist = true
  41. ): UserMerchantInterface {
  42. $userMerchant = $this->createIfNotExist($user, $merchant);
  43. $userMerchant->setActive($active);
  44. $userMerchant->setCreditActive($creditActive);
  45. $userMerchant->setCredit($credit);
  46. if ($persist) {
  47. //TODO create ou update ???
  48. $this->entityManager->persist($userMerchant);
  49. $this->entityManager->flush();
  50. }
  51. return $userMerchant;
  52. }
  53. public function activeCredit(UserMerchantInterface $userMerchant): UserMerchantInterface
  54. {
  55. return $this->updateCreditActive($userMerchant, true);
  56. }
  57. public function unactiveCredit(UserMerchantInterface $userMerchant): UserMerchantInterface
  58. {
  59. return $this->updateCreditActive($userMerchant, false);
  60. }
  61. public function updateCredit(
  62. UserMerchantInterface $userMerchant,
  63. CreditHistoryInterface $creditHistory,
  64. float $amount
  65. ): UserMerchantInterface {
  66. $userMerchant->setCredit($amount);
  67. $this->entityManager->update($creditHistory);
  68. $this->entityManager->update($userMerchant);
  69. $this->entityManager->flush();
  70. return $userMerchant;
  71. }
  72. public function updateCreditActive(
  73. UserMerchantInterface $userMerchant,
  74. $creditActive = true
  75. ): UserMerchantInterface {
  76. $userMerchant->setCreditActive($creditActive);
  77. //TODO create ou update ???
  78. $this->entityManager->persist($userMerchant);
  79. $this->entityManager->flush();
  80. return $userMerchant;
  81. }
  82. }