Completed
Push — master ( 028b40...025e52 )
by Gabriel
08:34
created

UserController::generateEmailConfirmationToken()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 15
nc 4
nop 4
1
<?php
2
3
namespace Sinergi\Users\User;
4
5
use Interop\Container\ContainerInterface;
6
use Sinergi\Users\Container;
7
use Sinergi\Users\User\Exception\EmailAlreadyConfirmedException;
8
use Sinergi\Users\User\Exception\EmailConfirmationTokenBlockedException;
9
use Sinergi\Users\User\Exception\EmailConfirmationTokenExpiredException;
10
use Sinergi\Users\User\Exception\InvalidEmailConfirmationTokenException;
11
use Sinergi\Users\User\Exception\InvalidUserException;
12
use Sinergi\Users\User\Exception\TooManyEmailConfirmationTokenAttemptsException;
13
use DateInterval;
14
15
class UserController
16
{
17
    private $container;
18
19
    public function __construct(ContainerInterface $container)
20
    {
21
        if ($container instanceof Container) {
22
            $this->container = $container;
23
        } else {
24
            $this->container = new Container($container);
25
        }
26
    }
27
28
    public function generateEmailConfirmationToken(
29
        UserEntityInterface $user,
30
        string $token = null,
31
        DateInterval $expiration = null,
32
        bool $save = true
33
    ): UserEntityInterface {
34
        if ($user->isEmailConfirmed()) {
35
            throw new EmailAlreadyConfirmedException;
36
        } elseif ($user->getEmailConfirmationToken() && !$user->hasEmailConfirmationTokenCooldownExpired()) {
37
            throw new EmailConfirmationTokenBlockedException;
38
        }
39
40
        $user->generateEmailConfirmationToken($token, $expiration);
41
        if ($save) {
42
            /** @var UserRepositoryInterface $userRepository */
43
            $userRepository = $this->container->get(UserRepositoryInterface::class);
44
            $userRepository->save($user);
45
        }
46
        return $user;
47
    }
48
49
    public function confirmEmail(UserEntityInterface $user, string $emailConfirmationToken): UserEntityInterface
50
    {
51
        if ($user->isEmailConfirmed()) {
52
            throw new EmailAlreadyConfirmedException;
53
        } elseif ($user->hasEmailConfirmationTokenExpired()) {
54
            throw new EmailConfirmationTokenExpiredException;
55
        } elseif ($user->hasTooManyEmailConfirmationTokenAttempts()) {
56
            throw new TooManyEmailConfirmationTokenAttemptsException;
57
        }
58
59
        /** @var UserRepositoryInterface $userRepository */
60
        $userRepository = $this->container->get(UserRepositoryInterface::class);
61
62
        if ($user->getEmailConfirmationToken() !== $emailConfirmationToken) {
63
            $user->setEmailConfirmationTokenAttempts($user->getEmailConfirmationTokenAttempts() + 1);
64
            $userRepository->save($user);
65
            throw new InvalidEmailConfirmationTokenException;
66
        }
67
68
        $user->setIsEmailConfirmed(true);
69
        $user->setEmailConfirmationToken(null);
70
        $user->setEmailConfirmationTokenAttempts(0);
71
        $user->setEmailConfirmationTokenExpirationDatetime(null);
72
        $userRepository->save($user);
73
        return $user;
74
    }
75
76
    public function createUser(UserEntityInterface $user): UserEntityInterface
77
    {
78
        /** @var UserValidatorInterface $userValidator */
79
        $userValidator = $this->container->get(UserValidatorInterface::class);
80
        $errors = $userValidator($user);
81
82
        if (!empty($errors)) {
83
            throw new InvalidUserException($errors);
84
        }
85
86
        /** @var UserRepositoryInterface $userRepository */
87
        $userRepository = $this->container->get(UserRepositoryInterface::class);
88
        $userRepository->save($user);
89
90
        $this->getEmailConfirmationController()
0 ignored issues
show
Bug introduced by
The method getEmailConfirmationController() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
            ->sendConfirmationEmail($user);
92
93
        return $user;
94
    }
95
96
    /**
97
     * @param UserEntity $userEntity
98
     * @param array      $parameters
99
     */
100
    public function updateUser(UserEntity $userEntity, array $parameters)
101
    {
102
        $options = [UserValidator::OPTION_SKIP_PASSWORD_VALIDATION];
103
104
        //todo : refactored this :
105
106
        $parameters = array_merge($userEntity->toArray(), $parameters);
107
108
        if ($userEntity->getEmail() === $parameters['email']) {
109
            $options[] = UserValidator::OPTION_SKIP_EMAIL_UNIQUENESS;
110
        }
111
112
        $this->getUserValidator()->validateParameters($parameters, $options);
0 ignored issues
show
Bug introduced by
The method getUserValidator() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
114
        if ($userEntity->getEmail() !== $parameters['email']) {
115
            $userEntity->setPendingEmail($parameters['email']);
116
117
            (new EmailConfirmationController($this->getContainer()))->emailUpdated($userEntity);
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
        }
119
120
        $userEntity
121
            ->setName($parameters['name'])
122
            ->setPhone($parameters['phone']);
123
124
        $this->getEntityManager()->persist($userEntity);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
        $this->getEntityManager()->flush($userEntity);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
    }
127
128
    /**
129
     * @param userEntity $userEntity
130
     * @param array      $parameters
131
     * @throws AuthenticationException
132
     */
133
    public function updatePassword(userEntity $userEntity, array $parameters)
134
    {
135
        if (!$userEntity->testPassword($parameters['current-password'])) {
136
            throw new AuthenticationException($this->getDictionary()->get('user.password.error.wrong_password'));
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
        }
138
139
        $this->getUserValidator()->validateParameters($parameters,
0 ignored issues
show
Bug introduced by
The method getUserValidator() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
            [UserValidator::OPTION_JUST_PASSWORD]);
141
142
        $userEntity->setPassword($parameters['password']);
143
        $this->getEntityManager()->persist($userEntity);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        $this->getEntityManager()->flush($userEntity);
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
    }
146
147
    /**
148
     * @param userEntity $userEntity
149
     */
150 View Code Duplication
    public function deleteUser(userEntity $userEntity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $userEntity->setStatus(UserEntity::STATUS_DELETED);
153
        $userEntity->setDeletedEmail($userEntity->getEmail());
154
        $userEntity->setEmail(null);
155
        $this->getContainer()->getEntityManager()->persist($userEntity);
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
156
        $this->getContainer()->getEntityManager()->flush($userEntity);
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
    }
158
159
    /**
160
     * @param userEntity $userEntity
161
     */
162 View Code Duplication
    public function banUser(userEntity $userEntity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        $userEntity->setStatus(UserEntity::STATUS_BANNED);
165
        $userEntity->setDeletedEmail($userEntity->getEmail());
166
        $userEntity->setEmail(null);
167
        $this->getContainer()->getEntityManager()->persist($userEntity);
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
168
        $this->getContainer()->getEntityManager()->flush($userEntity);
0 ignored issues
show
Bug introduced by
The method getContainer() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
    }
170
171
    /**
172
     * @param $status
173
     *
174
     * @return mixed
175
     */
176
    public function getUserStatusLabel($status)
177
    {
178
        switch ($status) {
179
            case UserEntity::STATUS_ACTIVE:
180
                return $this->getDictionary()->get('user.status.active');
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
181
            case UserEntity::STATUS_DELETED:
182
                return $this->getDictionary()->get('user.status.deleted');
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
183
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
184
            case UserEntity::STATUS_BANNED:
185
                return $this->getDictionary()->get('user.status.banned');
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
187
            default:
188
                return $this->getDictionary()->get('user.status.inactive');
0 ignored issues
show
Bug introduced by
The method getDictionary() does not seem to exist on object<Sinergi\Users\User\UserController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
189
        }
190
    }
191
}
192