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

UserBuilder.php 2.5KB

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