UserValidator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isUserVerified() 0 6 2
A __construct() 0 4 1
A doesResetpasswordTokenValidateEmail() 0 8 2
A isUserTokenValid() 0 10 2
A retrieveUserFromToken() 0 11 2
A isUserVerifiedDateTime() 0 6 2
1
<?php
2
3
namespace App\Security;
4
5
6
use App\Entity\User;
7
use App\Exception\RedirectException;
8
use App\FlashMessage\AddFlashTrait;
9
use App\FlashMessage\FlashMessageCategory;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Doctrine\ORM\NonUniqueResultException;
12
use Exception;
13
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
15
class UserValidator
16
{
17
    use AddFlashTrait;
18
19
    /**
20
     * @var EntityManagerInterface
21
     */
22
    private $em;
23
24
    /**
25
     * @var User
26
     */
27
    private $user;
28
    /**
29
     * @var UrlGeneratorInterface
30
     */
31
    private $urlGenerator;
32
33
    public function __construct(EntityManagerInterface $em, UrlGeneratorInterface $urlGenerator)
34
    {
35
        $this->em = $em;
36
        $this->urlGenerator = $urlGenerator;
37
    }
38
39
    /**
40
     * @param string $token
41
     * @return bool
42
     * Check if the passed token is valid to register the mail
43
     * @throws NonUniqueResultException
44
     */
45
    public function isUserTokenValid(string $token):bool
46
    {
47
        $this->retrieveUserFromToken($token);
48
49
        if($this->isUserVerified()){
50
            $this->addFlashMessage(FlashMessageCategory::ERROR, 'Mail already verified');
51
            throw new RedirectException($this->urlGenerator->generate('app_login'));
52
        }
53
54
        return $this->isUserVerifiedDateTime();
55
56
    }
57
58
    /**
59
     * @param string $token
60
     * @return bool
61
     * Check if the reset password token is valid
62
     * @throws NonUniqueResultException
63
     */
64
    public function doesResetpasswordTokenValidateEmail(string $token):bool
65
    {
66
        $this->retrieveUserFromToken($token);
67
        if(!$this->isUserVerifiedDateTime()){
68
            $this->addFlashMessage(FlashMessageCategory::ERROR, 'Token is too old, please use this form to resend a link');
69
            throw new RedirectException($this->urlGenerator->generate('app_forgotpassword'));
70
        }
71
        return !$this->isUserVerified();
72
    }
73
74
    /**
75
     * @param string $token
76
     * @return User|null
77
     * @throws NonUniqueResultException
78
     * gets the user from the token and redirects on error
79
     */
80
    public function retrieveUserFromToken(string $token): ?User
81
    {
82
        $user = $this->em->getRepository(User::class)->findUserByhash($token);
83
        if (!$user) {
84
            //no user found
85
            $this->addFlashMessage(FlashMessageCategory::ERROR, 'Invalid Token, please use this form to resend a link');
86
            throw new RedirectException($this->urlGenerator->generate('app_forgotpassword'));
87
        }
88
89
        $this->user = $user;
90
        return $this->user;
91
    }
92
93
    /**
94
     * @return bool
95
     * @throws Exception
96
     * Checks if the token is still valid
97
     */
98
    private function isUserVerifiedDateTime():bool
99
    {
100
        if($this->user){
101
            return $this->user->isVerifiedDateTimeValid();
102
        }
103
        return false;
104
105
    }
106
107
    /**
108
     * @return bool
109
     * checks if the user has already validated his account
110
     */
111
    private function isUserVerified():bool
112
    {
113
        if($this->user){
114
            return $this->user->getVerified();
115
        }
116
        return false;
117
    }
118
119
}