RoleController::setRoleAction()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 0
1
<?php
2
3
namespace JhUser\Controller;
4
5
use JhUser\Entity\HierarchicalRole;
6
use JhUser\Entity\Permission;
7
use Zend\Mvc\Controller\AbstractActionController;
8
use Zend\Console\Request as ConsoleRequest;
9
use Doctrine\Common\Persistence\ObjectManager;
10
use JhUser\Repository\UserRepositoryInterface;
11
use JhUser\Repository\RoleRepositoryInterface;
12
13
/**
14
 * Class RoleController
15
 * @package JhUser\Controller
16
 * @author Aydin Hassan <[email protected]>
17
 */
18
class RoleController extends AbstractActionController
19
{
20
21
    /**
22
     * @var \ObjectManager
23
     */
24
    protected $objectManager;
25
26
    /**
27
     * @var \JhUser\Repository\UserRepositoryInterface
28
     */
29
    protected $userRepository;
30
31
    /**
32
     * @var \JhUser\Repository\RoleRepositoryInterface
33
     */
34
    protected $roleRepository;
35
36
    /**
37
     * @param ObjectManager $objectManager
38
     * @param UserRepositoryInterface $userRepository
39
     * @param RoleRepositoryInterface $roleRepository
40
     */
41
    public function __construct(
42
        ObjectManager $objectManager,
43
        UserRepositoryInterface $userRepository,
44
        RoleRepositoryInterface $roleRepository
45
    ) {
46
        $this->objectManager    = $objectManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $objectManager of type object<Doctrine\Common\Persistence\ObjectManager> is incompatible with the declared type object<ObjectManager> of property $objectManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
        $this->userRepository   = $userRepository;
48
        $this->roleRepository   = $roleRepository;
49
    }
50
51
    /**
52
     * Set a Users role,
53
     * Removes all existing roles
54
     *
55
     * @throws \RuntimeException
56
     */
57
    public function setRoleAction()
58
    {
59
        $request = $this->getRequest();
60
61
        $email  = $request->getParam('userEmail');
62
        $roleId = $request->getParam('role');
63
64
        $user   = $this->userRepository->findOneByEmail($email);
65
66
        if (!$user) {
67
            throw new \RuntimeException(sprintf('User with email: "%s" could not be found', $email));
68
        }
69
70
        $newRole = $this->roleRepository->findByName($roleId);
71
72
        if (!$newRole) {
73
            throw new \RuntimeException(sprintf('Role: "%s" could not be found', $roleId));
74
        }
75
76
        foreach ($user->getRoles() as $role) {
77
            $user->removeRole($role);
78
        }
79
80
        $user->addRole($newRole);
81
        $this->objectManager->flush();
82
    }
83
}
84