Test Failed
Branch main (8f4107)
by Bingo
08:27 queued 02:15
created

ResourceTypeUtil::getPermissionsByResourceType()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Jabe\Engine\Impl\Util;
4
5
use Jabe\Engine\Authorization\{
6
    BatchPermissions,
7
    HistoricProcessInstancePermissions,
8
    HistoricTaskPermissions,
9
    OptimizePermissions,
10
    PermissionInterface,
11
    Permissions,
12
    ProcessDefinitionPermissions,
13
    ProcessInstancePermissions,
14
    ResourceInterface,
15
    Resources,
16
    SystemPermissions,
17
    UserOperationLogCategoryPermissions
18
};
19
20
class ResourceTypeUtil
21
{
22
    /**
23
     * A map containing all {@link Resources} as a key and
24
     * the respective {@link Permission} Enum class for this resource.<p>
25
     * NOTE: In case of new {@link Permission} Enum class, please adjust the map accordingly
26
     */
27
    protected static $PERMISSION_ENUMS = [];
28
29
    /**
30
     * @return bool true in case the resource with the provided resourceTypeId is contained by the specified list
31
     */
32
    public static function resourceIsContainedInArray(int $resourceTypeId, array $resources): bool
33
    {
34
        foreach ($resources as $resource) {
35
            if ($resourceTypeId == $resource->resourceType()) {
36
                return true;
37
            }
38
        }
39
        return false;
40
    }
41
42
    /**
43
     * @return See {@link ResourceTypeUtil#PERMISSION_ENUMS}
44
     */
45
    public static function getPermissionEnums(): array
46
    {
47
        if (empty(self::$PERMISSION_ENUMS)) {
48
            self::$PERMISSION_ENUMS = [
49
                Resources::batch()->resourceType() => BatchPermissions::class,
50
                Resources::processDefinition()->resourceType() => ProcessDefinitionPermissions::class,
51
                Resources::processInstance()->resourceType() => ProcessInstancePermissions::class,
52
                Resources::task()->resourceType() => TaskPermissions::class,
53
                Resources::historicTask()->resourceType() => HistoricTaskPermissions::class,
54
                Resources::historicProcessInstance()->resourceType() => HistoricProcessInstancePermissions::class,
55
                Resources::operationLogCategory()->resourceType() => UserOperationLogCategoryPermissions::class,
56
                Resources::optimize()->resourceType() => OptimizePermissions::class,
57
                Resources::system()->resourceType() => SystemPermissions::class
58
            ];
59
60
            // the rest
61
            foreach (Permissions::values() as $permission) {
62
                if ($permission == Permissions::all() || $permission == Permissions::none()) {
63
                    continue;
64
                }
65
                foreach ($permission->getTypes() as $resource) {
66
                    $resourceType = $resource->resourceType();
67
                    if (!array_key_exists($resourceType, self::$PERMISSION_ENUMS)) {
68
                        self::$PERMISSION_ENUMS[$resourceType] = Permissions::class;
69
                    }
70
                }
71
            }
72
        }
73
        return self::$PERMISSION_ENUMS;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::PERMISSION_ENUMS returns the type array|array<integer,string> which is incompatible with the documented return type Jabe\Engine\Impl\Util\See.
Loading history...
74
    }
75
76
    /**
77
     * Retrieves the {@link Permission} array based on the predifined {@link ResourceTypeUtil#PERMISSION_ENUMS PERMISSION_ENUMS}
78
     */
79
    public static function getPermissionsByResourceType(int $givenResourceType): array
80
    {
81
        $clazz = null;
82
        if (array_key_exists($givenResourceType, self::getPermissionEnums())) {
83
            $clazz = self::$PERMISSION_ENUMS[$givenResourceType];
84
        }
85
        if ($clazz === null) {
86
            return Permissions::values();
87
        }
88
        return $clazz::values();
89
    }
90
91
    /**
92
     * Currently used only in the Rest API
93
     * Returns a {@link Permission} based on the specified <code>permissionName</code> and <code>resourceType</code>
94
     * @throws BadUserRequestException in case the permission is not valid for the specified resource type
95
     */
96
    public static function getPermissionByNameAndResourceType(string $permissionName, int $resourceType): PermissionInterface
97
    {
98
        foreach (self::getPermissionsByResourceType($resourceType) as $permission) {
99
            if ($permission->getName() == $permissionName) {
100
                return $permission;
101
            }
102
        }
103
        throw new BadUserRequestException(
104
            sprintf("The permission '%s' is not valid for '%s' resource type.", $permissionName, self::getResourceByType($resourceType))
0 ignored issues
show
Bug introduced by
It seems like self::getResourceByType($resourceType) can also be of type Jabe\Engine\Authorization\ResourceInterface; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

104
            sprintf("The permission '%s' is not valid for '%s' resource type.", $permissionName, /** @scrutinizer ignore-type */ self::getResourceByType($resourceType))
Loading history...
105
        );
106
    }
107
108
    /**
109
     * Iterates over the {@link Resources} and
110
     * returns either the resource with specified <code>resourceType</code> or <code>null</code>.
111
     */
112
    public static function getResourceByType(int $resourceType): ?ResourceInterface
113
    {
114
        foreach (Resources::values() as $resource) {
0 ignored issues
show
Bug introduced by
The method values() does not exist on Jabe\Engine\Authorization\Resources. ( Ignorable by Annotation )

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

114
        foreach (Resources::/** @scrutinizer ignore-call */ values() as $resource) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
            if ($resource->resourceType() == $resourceType) {
116
                return $resource;
117
            }
118
        }
119
        return null;
120
    }
121
}
122