Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

113 lines
3.4KB

  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. }