Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

100 rindas
3.0KB

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