Completed
Push — Recipes ( c0466a...7632b6 )
by Laurent
04:22
created

UserController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A persistEntity() 0 9 3
A encodePassword() 0 8 2
A __construct() 0 3 1
A updateEntity() 0 9 3
1
<?php
2
/**
3
 * UserController Controller of User entity.
4
 *
5
 * PHP Version 7
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2018 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version GIT: $Id$
12
 *
13
 * @see https://github.com/Dev-Int/glsr
14
 */
15
16
namespace App\Controller;
17
18
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController as BaseAdminController;
19
use Symfony\Component\Security\Core\User\UserInterface;
20
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
21
22
/**
23
 * User Controller override EasyAdminBundle::AdminController.
24
 *
25
 * @category Controller
26
 */
27
class UserController extends BaseAdminController
28
{
29
    /**
30
     * @var UserPasswordEncoderInterface
31
     */
32
    protected $passwordEncoder;
33
34
    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
35
    {
36
        $this->passwordEncoder = $passwordEncoder;
37
    }
38
39
    public function persistEntity($entity): void
40
    {
41
        if ($entity->getAdmin()) {
42
            $entity->setRoles(['ROLE_SUPER_ADMIN']);
43
        } elseif ($entity->getAssistant()) {
44
            $entity->setRoles(['ROLE_ADMIN']);
45
        }
46
        $this->encodePassword($entity);
47
        parent::persistEntity($entity);
48
    }
49
50
    public function updateEntity($entity): void
51
    {
52
        if ($entity->getAdmin()) {
53
            $entity->setRoles(['ROLE_SUPER_ADMIN']);
54
        } elseif ($entity->getAssistant()) {
55
            $entity->setRoles(['ROLE_ADMIN']);
56
        }
57
        $this->encodePassword($entity);
58
        parent::persistEntity($entity);
59
    }
60
61
    /**
62
     * Encode the password.
63
     *
64
     * @param UserInterface $user
65
     */
66
    protected function encodePassword(UserInterface $user): void
67
    {
68
        if (!$user instanceof UserInterface) {
0 ignored issues
show
introduced by
$user is always a sub-type of Symfony\Component\Security\Core\User\UserInterface.
Loading history...
69
            return;
70
        }
71
72
        $user->setPassword(
0 ignored issues
show
Bug introduced by
The method setPassword() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as App\Entity\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $user->/** @scrutinizer ignore-call */ 
73
               setPassword(
Loading history...
73
            $this->passwordEncoder->encodePassword($user, $user->getPassword())
74
        );
75
    }
76
}
77