|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Manager; |
|
6
|
|
|
|
|
7
|
|
|
use App\Factory\UserFactoryInterface; |
|
8
|
|
|
use App\Generator\StringGenerator; |
|
9
|
|
|
use App\Model\UserInterface; |
|
10
|
|
|
use App\Repository\CourseRepositoryInterface; |
|
11
|
|
|
use App\Repository\UserRepositoryInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class UserManager implements UserManagerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private $courseRepository; |
|
17
|
|
|
|
|
18
|
|
|
private $userRepository; |
|
19
|
|
|
|
|
20
|
|
|
private $userFactory; |
|
21
|
|
|
|
|
22
|
|
|
private $passwordEncoder; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(CourseRepositoryInterface $courseRepository, UserRepositoryInterface $userRepository, UserFactoryInterface $userFactory, UserPasswordEncoderInterface $passwordEncoder) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->courseRepository = $courseRepository; |
|
27
|
|
|
$this->userRepository = $userRepository; |
|
28
|
|
|
$this->userFactory = $userFactory; |
|
29
|
|
|
$this->passwordEncoder = $passwordEncoder; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function addCourseByTitle(UserInterface $user, string $courseTitle): void |
|
33
|
|
|
{ |
|
34
|
|
|
$course = $this->courseRepository->getOneByTitle($courseTitle); |
|
35
|
|
|
if (null !== $course) { |
|
36
|
|
|
$user->addCourse($course); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getOrCreateUser(string $email): UserInterface |
|
41
|
|
|
{ |
|
42
|
|
|
$user = $this->userRepository->getOneByEmail($email); |
|
43
|
|
|
if (null === $user) { |
|
44
|
|
|
$user = $this->userFactory->create(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $user; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function setGeneratedPasswordResetToken(UserInterface $user): void |
|
51
|
|
|
{ |
|
52
|
|
|
$user->setPasswordNeedToBeChanged(true); |
|
53
|
|
|
$user->setPasswordResetToken(StringGenerator::random(22)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function resetPassword(UserInterface $user, string $password): void |
|
57
|
|
|
{ |
|
58
|
|
|
$user->setPassword($this->passwordEncoder->encodePassword($user, $password)); |
|
59
|
|
|
$user->setPasswordNeedToBeChanged(false); |
|
60
|
|
|
$user->setPasswordResetToken(null); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|