Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

99 lines
2.9KB

  1. <?php
  2. namespace Lc\SovBundle\Builder\User;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Model\Address\AddressInterface;
  5. use Lc\SovBundle\Doctrine\EntityInterface;
  6. use Lc\SovBundle\Doctrine\Extension\BlameableInterface;
  7. use Lc\SovBundle\Factory\User\UserFactory;
  8. use Lc\SovBundle\Model\Newsletter\NewsletterInterface;
  9. use Lc\SovBundle\Model\Ticket\TicketInterface;
  10. use Lc\SovBundle\Model\User\UserInterface;
  11. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  12. use Lc\SovBundle\Repository\User\UserStore;
  13. use Lc\SovBundle\Solver\User\UserSolver;
  14. class UserBuilder
  15. {
  16. protected EntityManagerInterface $entityManager;
  17. protected UserStore $userStore;
  18. protected UserSolver $userSolver;
  19. protected UserFactory $userFactory;
  20. public function __construct(EntityManagerInterface $entityManager, UserStore $userStore, UserSolver $userSolver, UserFactory $userFactory)
  21. {
  22. $this->entityManager = $entityManager;
  23. $this->userStore = $userStore;
  24. $this->userSolver = $userSolver;
  25. $this->userFactory = $userFactory;
  26. }
  27. public function create(string $email, string $password, array $roles = [])
  28. {
  29. $user = $this->userFactory->create();
  30. $user->setEmail($email);
  31. $user->setPassword($password);
  32. $user->setRoles($roles);
  33. $this->entityManager->create($user);
  34. $this->entityManager->flush();
  35. return $user;
  36. }
  37. public function setNewsletter(UserInterface $user, NewsletterInterface $newsletter, bool $subscribeNewsletter): void
  38. {
  39. if ($subscribeNewsletter) {
  40. $user->addNewsletter($newsletter);
  41. } else {
  42. $user->removeNewsletter($newsletter);
  43. }
  44. $this->entityManager->persist($user);
  45. $this->entityManager->flush();
  46. }
  47. public function initBlameableSystem(EntityInterface $entity)
  48. {
  49. $userSystem = $this->userStore->getOneByDevAlias('system');
  50. $this->initBlameable($entity, $userSystem);
  51. return $entity;
  52. }
  53. public function initBlameableUpdatedSystem(EntityInterface $entity)
  54. {
  55. $userSystem = $this->userStore->getOneByDevAlias('system');
  56. $this->initBlameableUpdated($entity, $userSystem);
  57. return $entity;
  58. }
  59. public function initBlameable(EntityInterface $entity, UserInterface $user)
  60. {
  61. if ($entity instanceof BlameableInterface) {
  62. $entity->setCreatedBy($user);
  63. }
  64. $this->initBlameableUpdated($entity, $user);
  65. }
  66. public function initBlameableUpdated(EntityInterface $entity, UserInterface $user)
  67. {
  68. if ($entity instanceof BlameableInterface) {
  69. $entity->setUpdatedBy($user);
  70. }
  71. }
  72. public function setLastLogin(?UserInterface $user)
  73. {
  74. if ($user instanceof UserInterface) {
  75. $user->setLastLogin(new \DateTime());
  76. $this->entityManager->update($user);
  77. $this->entityManager->flush();
  78. }
  79. }
  80. }