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.

67 satır
2.0KB

  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\Model\Newsletter\NewsletterInterface;
  7. use Lc\SovBundle\Model\User\UserInterface;
  8. use Lc\SovBundle\Repository\User\UserStore;
  9. use Lc\SovBundle\Solver\User\UserSolver;
  10. class UserBuilder
  11. {
  12. protected EntityManagerInterface $entityManager;
  13. protected UserStore $userStore;
  14. protected UserSolver $userSolver;
  15. public function __construct(EntityManagerInterface $entityManager, UserStore $userStore, UserSolver $userSolver)
  16. {
  17. $this->entityManager = $entityManager;
  18. $this->userStore = $userStore;
  19. $this->userSolver = $userSolver;
  20. }
  21. public function setNewsletter(UserInterface $user, NewsletterInterface $newsletter, bool $subscribeNewsletter): void
  22. {
  23. if ($subscribeNewsletter) {
  24. $user->addNewsletter($newsletter);
  25. } else {
  26. $user->removeNewsletter($newsletter);
  27. }
  28. $this->entityManager->persist($user);
  29. $this->entityManager->flush();
  30. }
  31. public function initBlameableSystem(EntityInterface $entity)
  32. {
  33. $userSystem = $this->userStore->getOneByDevAlias('system');
  34. $this->initBlameable($entity, $userSystem);
  35. return $entity;
  36. }
  37. public function initBlameableUpdatedSystem(EntityInterface $entity)
  38. {
  39. $userSystem = $this->userStore->getOneByDevAlias('system');
  40. $this->initBlameableUpdated($entity, $userSystem);
  41. return $entity;
  42. }
  43. public function initBlameable(EntityInterface $entity, UserInterface $user)
  44. {
  45. if ($entity instanceof BlameableInterface) {
  46. $entity->setCreatedBy($user);
  47. }
  48. $this->initBlameableUpdated($entity, $user);
  49. }
  50. public function initBlameableUpdated(EntityInterface $entity, UserInterface $user)
  51. {
  52. if ($entity instanceof BlameableInterface) {
  53. $entity->setUpdatedBy($user);
  54. }
  55. }
  56. }