Completed
Branch feature/currentUserRefactoring (c13c1d)
by Schlaefer
04:13
created

PermissionsHelper::rolesSelectId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 8
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\View\Helper;
14
15
use Cake\View\Helper\HtmlHelper;
16
use Cake\View\Helper\UrlHelper;
17
use Saito\App\Registry;
18
use Saito\User\Permission\Permissions;
19
20
/**
21
 * Class UserHelper
22
 *
23
 * @package App\View\Helper
24
 * @property HtmlHelper $Html
25
 * @property UrlHelper $Url
26
 */
27
class PermissionsHelper extends AppHelper
28
{
29
    /**
30
     * Permissions
31
     *
32
     * @var Permissions
33
     */
34
    private $Permissions;
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function initialize(array $config)
40
    {
41
        parent::initialize($config);
42
43
        /** @var Permissions */
44
        $permissions = Registry::get('Permissions');
45
        $this->Permissions = $permissions;
0 ignored issues
show
Documentation Bug introduced by
It seems like $permissions of type object<Saito\App\object> is incompatible with the declared type object<Saito\User\Permission\Permissions> of property $Permissions.

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...
46
    }
47
48
    /**
49
     * Get an array to use as $option in a select field with role-IDs
50
     *
51
     * @param bool $includeAnon Include anon-user
52
     * @return array
53
     */
54 View Code Duplication
    public function rolesSelectId(bool $includeAnon = false): array
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...
55
    {
56
        $roles = $this->Permissions->getRoles()->getAvailable($includeAnon);
57
58
        return array_map(function ($role) {
59
            return ['value' => $role['id'], 'text' => $this->roleAsString($role['id'])];
60
        }, $roles);
61
    }
62
63
    /**
64
     * Get an array to use as $option in a select field with role-types
65
     *
66
     * @param bool $includeAnon Include anon-user
67
     * @return array
68
     */
69 View Code Duplication
    public function rolesSelectType(bool $includeAnon = false): array
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...
70
    {
71
        $roles = $this->Permissions->getRoles()->getAvailable($includeAnon);
72
73
        return array_map(function ($role) {
74
            return ['value' => $role['type'], 'text' => $this->roleAsString($role['id'])];
75
        }, $roles);
76
    }
77
78
    /**
79
     * L10n a role.
80
     *
81
     * @param int|string $id Role-ID or role-type.
82
     * @return string The localized string.
83
     */
84
    public function roleAsString($id): string
85
    {
86
        if (is_string($id)) {
87
            $id = $this->Permissions->getRoles()->typeToId($id);
88
        }
89
90
        return __d('nondynamic', 'permission.role.' . $id);
91
    }
92
}
93