您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UserMerchantBuilder.php 3.1KB

3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }