1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Manager; |
6
|
|
|
|
7
|
|
|
use App\Event\NewCourseAddedEvent; |
8
|
|
|
use App\Factory\UserFactoryInterface; |
9
|
|
|
use App\Generator\StringGenerator; |
10
|
|
|
use App\Model\CourseInterface; |
11
|
|
|
use App\Model\UserInterface; |
12
|
|
|
use App\Repository\CourseRepositoryInterface; |
13
|
|
|
use App\Repository\UserRepositoryInterface; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
16
|
|
|
|
17
|
|
|
final class UserManager implements UserManagerInterface |
18
|
|
|
{ |
19
|
|
|
private CourseRepositoryInterface $courseRepository; |
20
|
|
|
|
21
|
|
|
private UserRepositoryInterface $userRepository; |
22
|
|
|
|
23
|
|
|
private UserFactoryInterface $userFactory; |
24
|
|
|
|
25
|
|
|
private UserPasswordEncoderInterface $passwordEncoder; |
26
|
|
|
|
27
|
|
|
private EventDispatcherInterface $eventDispatcher; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
CourseRepositoryInterface $courseRepository, |
31
|
|
|
UserRepositoryInterface $userRepository, |
32
|
|
|
UserFactoryInterface $userFactory, |
33
|
|
|
UserPasswordEncoderInterface $passwordEncoder, |
34
|
|
|
EventDispatcherInterface $eventDispatcher |
35
|
|
|
) { |
36
|
|
|
$this->courseRepository = $courseRepository; |
37
|
|
|
$this->userRepository = $userRepository; |
38
|
|
|
$this->userFactory = $userFactory; |
39
|
|
|
$this->passwordEncoder = $passwordEncoder; |
40
|
|
|
$this->eventDispatcher = $eventDispatcher; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function addCourseByTitleOrSku(UserInterface $user, string $courseTitleOrSku): void |
44
|
|
|
{ |
45
|
|
|
$course = $this->courseRepository->getOneByTitleOrSku($courseTitleOrSku); |
46
|
|
|
if (null !== $course) { |
47
|
|
|
$user->addCourse($course); |
48
|
|
|
$this->eventDispatcher->dispatch(new NewCourseAddedEvent($user, $course)); |
49
|
|
|
|
50
|
|
|
/** @var CourseInterface $userCourse */ |
51
|
|
|
foreach ($user->getCourses() as $userCourse) { |
52
|
|
|
if ( |
53
|
|
|
null !== $userCourse->getParent() && |
54
|
|
|
$userCourse->getParent()->getId() === $course->getId() |
55
|
|
|
) { |
56
|
|
|
$user->removeCourse($userCourse); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function removeCourseByTitleOrSku(UserInterface $user, string $courseTitleOrSku): void |
63
|
|
|
{ |
64
|
|
|
$course = $this->courseRepository->getOneByTitleOrSku($courseTitleOrSku); |
65
|
|
|
if (null !== $course) { |
66
|
|
|
$user->removeCourse($course); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getOrCreateUser(string $email): UserInterface |
71
|
|
|
{ |
72
|
|
|
$user = $this->userRepository->getOneByEmail($email); |
73
|
|
|
if (null === $user) { |
74
|
|
|
$user = $this->userFactory->create(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $user; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function setGeneratedPasswordResetToken(UserInterface $user): void |
81
|
|
|
{ |
82
|
|
|
$user->setPasswordNeedToBeChanged(true); |
83
|
|
|
$user->setPasswordResetToken(StringGenerator::random(22)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function resetPassword(UserInterface $user, string $password): void |
87
|
|
|
{ |
88
|
|
|
$user->setPassword($this->passwordEncoder->encodePassword($user, $password)); |
89
|
|
|
$user->setPasswordNeedToBeChanged(false); |
90
|
|
|
$user->setPasswordResetToken(null); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|