|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
-
- namespace Lc\SovBundle\Builder\User;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\SovBundle\Doctrine\EntityInterface;
- use Lc\SovBundle\Doctrine\Extension\BlameableInterface;
- use Lc\SovBundle\Factory\User\UserFactory;
- use Lc\SovBundle\Model\Newsletter\NewsletterInterface;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\SovBundle\Repository\User\UserStore;
- use Lc\SovBundle\Solver\User\UserSolver;
-
- class UserBuilder
- {
- protected EntityManagerInterface $entityManager;
- protected UserStore $userStore;
- protected UserSolver $userSolver;
- protected UserFactory $userFactory;
-
- public function __construct(EntityManagerInterface $entityManager, UserStore $userStore, UserSolver $userSolver, UserFactory $userFactory)
- {
- $this->entityManager = $entityManager;
- $this->userStore = $userStore;
- $this->userSolver = $userSolver;
- $this->userFactory = $userFactory;
- }
-
-
- public function create( string $email, string $password, array $roles=[])
- {
- $user = $this->userFactory->create();
- $user->setEmail($email);
- $user->setPassword($password);
-
- $user->setRoles($roles);
- $this->entityManager->create($user);
- $this->entityManager->flush();
-
- return $user;
-
- }
-
- public function setNewsletter(UserInterface $user, NewsletterInterface $newsletter, bool $subscribeNewsletter): void
- {
- if ($subscribeNewsletter) {
- $user->addNewsletter($newsletter);
- } else {
- $user->removeNewsletter($newsletter);
- }
-
- $this->entityManager->persist($user);
- $this->entityManager->flush();
- }
-
- public function initBlameableSystem(EntityInterface $entity)
- {
- $userSystem = $this->userStore->getOneByDevAlias('system');
- $this->initBlameable($entity, $userSystem);
-
- return $entity;
- }
-
- public function initBlameableUpdatedSystem(EntityInterface $entity)
- {
- $userSystem = $this->userStore->getOneByDevAlias('system');
- $this->initBlameableUpdated($entity, $userSystem);
-
- return $entity;
- }
-
- public function initBlameable(EntityInterface $entity, UserInterface $user)
- {
- if ($entity instanceof BlameableInterface) {
- $entity->setCreatedBy($user);
- }
- $this->initBlameableUpdated($entity, $user);
- }
-
- public function initBlameableUpdated(EntityInterface $entity, UserInterface $user)
- {
- if ($entity instanceof BlameableInterface) {
- $entity->setUpdatedBy($user);
- }
- }
-
- public function setLastLogin(?UserInterface $user){
- if ($user instanceof UserInterface) {
- $user->setLastLogin(new \DateTime());
- $this->entityManager->update($user);
- $this->entityManager->flush();
- }
- }
- }
|