Passed
Push — master ( 786b21...4a8f5f )
by Stone
06:47 queued 42s
created

UserValidator   A

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