You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserBuilder.php 2.8KB

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