Completed
Push — master ( a5d24e...028b40 )
by Gabriel
02:43
created

UserController::getUserStatusLabel()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 12
nc 4
nop 1
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\InvalidUserException;
8
9
class UserController
10
{
11
    private $container;
12
13
    public function __construct(ContainerInterface $container)
14
    {
15
        if ($container instanceof Container) {
16
            $this->container = $container;
17
        } else {
18
            $this->container = new Container($container);
19
        }
20
    }
21
22
    public function createUser(UserEntityInterface $user): UserEntityInterface
23
    {
24
        /** @var UserValidatorInterface $userValidator */
25
        $userValidator = $this->container->get(UserValidatorInterface::class);
26
        $errors = $userValidator($user);
27
28
        if (!empty($errors)) {
29
            throw new InvalidUserException($errors);
30
        }
31
32
        /** @var UserRepositoryInterface $userRepository */
33
        $userRepository = $this->container->get(UserRepositoryInterface::class);
34
        $userRepository->save($user);
35
36
        $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...
37
            ->sendConfirmationEmail($user);
38
39
        return $user;
40
    }
41
42
    /**
43
     * @param UserEntity $userEntity
44
     * @param array      $parameters
45
     */
46
    public function updateUser(UserEntity $userEntity, array $parameters)
47
    {
48
        $options = [UserValidator::OPTION_SKIP_PASSWORD_VALIDATION];
49
50
        //todo : refactored this :
51
52
        $parameters = array_merge($userEntity->toArray(), $parameters);
53
54
        if ($userEntity->getEmail() === $parameters['email']) {
55
            $options[] = UserValidator::OPTION_SKIP_EMAIL_UNIQUENESS;
56
        }
57
58
        $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...
59
60
        if ($userEntity->getEmail() !== $parameters['email']) {
61
            $userEntity->setPendingEmail($parameters['email']);
62
63
            (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...
64
        }
65
66
        $userEntity
67
            ->setName($parameters['name'])
68
            ->setPhone($parameters['phone']);
69
70
        $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...
71
        $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...
72
    }
73
74
    /**
75
     * @param userEntity $userEntity
76
     * @param array      $parameters
77
     * @throws AuthenticationException
78
     */
79
    public function updatePassword(userEntity $userEntity, array $parameters)
80
    {
81
        if (!$userEntity->testPassword($parameters['current-password'])) {
82
            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...
83
        }
84
85
        $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...
86
            [UserValidator::OPTION_JUST_PASSWORD]);
87
88
        $userEntity->setPassword($parameters['password']);
89
        $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...
90
        $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...
91
    }
92
93
    /**
94
     * @param userEntity $userEntity
95
     */
96 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...
97
    {
98
        $userEntity->setStatus(UserEntity::STATUS_DELETED);
99
        $userEntity->setDeletedEmail($userEntity->getEmail());
100
        $userEntity->setEmail(null);
101
        $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...
102
        $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...
103
    }
104
105
    /**
106
     * @param userEntity $userEntity
107
     */
108 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...
109
    {
110
        $userEntity->setStatus(UserEntity::STATUS_BANNED);
111
        $userEntity->setDeletedEmail($userEntity->getEmail());
112
        $userEntity->setEmail(null);
113
        $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...
114
        $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...
115
    }
116
117
    /**
118
     * @param $status
119
     *
120
     * @return mixed
121
     */
122
    public function getUserStatusLabel($status)
123
    {
124
        switch ($status) {
125
            case UserEntity::STATUS_ACTIVE:
126
                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...
127
            case UserEntity::STATUS_DELETED:
128
                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...
129
                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...
130
            case UserEntity::STATUS_BANNED:
131
                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...
132
                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...
133
            default:
134
                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...
135
        }
136
    }
137
}
138