RolesDataSource::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Victoire\Bundle\CriteriaBundle\DataSource;
4
5
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
7
8
class RolesDataSource
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
9
{
10
    /**
11
     * @var TokenStorage
12
     */
13
    private $tokenStorage;
14
15
    /*
16
     * @var array
17
     */
18
    private $roleHierarchy;
19
20
    /**
21
     * RolesDataSource constructor.
22
     *
23
     * @param TokenStorage $tokenStorage
24
     * @param array        $roleHierarchy
25
     */
26
    public function __construct(TokenStorage $tokenStorage, $roleHierarchy)
27
    {
28
        $this->tokenStorage = $tokenStorage;
29
        $this->roleHierarchy = $roleHierarchy;
30
    }
31
32
    /**
33
     * @return mixed
34
     */
35
    public function getRoles()
36
    {
37
        $user = $this->tokenStorage->getToken()->getUser();
38
39
        return $user;
40
    }
41
42
    /**
43
     * @return array
44
     */
45 View Code Duplication
    public function getRolesFormParams()
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...
46
    {
47
        return [
48
            'type'    => ChoiceType::class,
49
            'options' => [
50
                'choices'           => $this->getAllAvailableRoles($this->roleHierarchy),
51
                'choices_as_values' => true,
52
                'choice_label'      => function ($value) {
53
                    return $value;
54
                },
55
            ],
56
        ];
57
    }
58
59
    /**
60
     * flatten the array of all roles defined in role_hierarchy.
61
     *
62
     * @param array $roleHierarchy
63
     *
64
     * @return array
65
     */
66
    public function getAllAvailableRoles($roleHierarchy)
67
    {
68
        $roles = [];
69
        foreach ($roleHierarchy as $key => $value) {
70
            if (is_array($value)) {
71
                $roles = array_merge($roles, $this->getAllAvailableRoles($value));
72
            }
73
            if (is_string($key)) {
74
                $roles[] = $key;
75
            }
76
            if (is_string($value)) {
77
                $roles[] = $value;
78
            }
79
        }
80
81
        return $roles;
82
    }
83
}
84