Passed
Push — master ( 419604...046546 )
by Paweł
03:31
created

UserManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 1
b 0
f 0
dl 0
loc 47
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A resetPassword() 0 5 1
A setGeneratedPasswordResetToken() 0 4 1
A addCourseByTitle() 0 5 2
A getOrCreateUser() 0 8 2
A __construct() 0 6 1
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