VoterWithPermissions::isGranted()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 15
rs 10
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\MessageBus;
13
14
/**
15
 * A voter with the knowledge of permissions attached to messages.
16
 */
17
final class VoterWithPermissions implements Voter
18
{
19
    /**
20
     * @param array<class-string, string[]> $permissionsByMessage
21
     *     Where _key_ is a command class and _value_ the permissions for that message.
22
     */
23
    public function __construct(
24
        private VoterProvider $voters,
25
        private array $permissionsByMessage,
26
    ) {
27
    }
28
29
    public function isGranted(object $message, Context $context): bool
30
    {
31
        $permissions = $this->permissionsByMessage[$message::class] ?? null;
32
33
        if (!$permissions) {
34
            return true;
35
        }
36
37
        foreach ($permissions as $permission) {
38
            if ($this->voters->getVoterForPermission($permission)?->isGranted($message, $context)) {
39
                return true;
40
            }
41
        }
42
43
        return false;
44
    }
45
}
46