Passed
Push — Recipes ( 7632b6...2e7935 )
by Laurent
02:54
created

UserController::encodePassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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->passwordEncoder->encodePassword($entity, $entity->getPassword());
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->passwordEncoder->encodePassword($entity, $entity->getPassword());
58
        parent::persistEntity($entity);
59
    }
60
}
61