No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

UserMerchantBuilder.php 3.3KB

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