Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
UserContext
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
8 / 8
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 can
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 canAny
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 canAll
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getPermissions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAuthenticated
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10namespace AppDemo\Shared\Domain;
11
12use AppDemo\Shared\Domain\Interface\RbacPermissionServiceInterface;
13use AppDemo\User\Domain\User;
14use Phexium\Domain\Id\IdInterface;
15
16final readonly class UserContext
17{
18    public function __construct(
19        private ?User $user,
20        private RbacPermissionServiceInterface $rbacService
21    ) {}
22
23    public function can(string $permission): bool
24    {
25        if (!$this->isAuthenticated()) {
26            return false;
27        }
28
29        return $this->rbacService->can($this->user->getGroup(), $permission);
30    }
31
32    public function canAny(array $permissions): bool
33    {
34        if (!$this->isAuthenticated()) {
35            return false;
36        }
37
38        return $this->rbacService->canAny($this->user->getGroup(), $permissions);
39    }
40
41    public function canAll(array $permissions): bool
42    {
43        if (!$this->isAuthenticated()) {
44            return false;
45        }
46
47        return $this->rbacService->canAll($this->user->getGroup(), $permissions);
48    }
49
50    public function getPermissions(): array
51    {
52        if (!$this->isAuthenticated()) {
53            return [];
54        }
55
56        return $this->rbacService->getPermissions($this->user->getGroup());
57    }
58
59    public function getUser(): ?User
60    {
61        return $this->user;
62    }
63
64    public function getUserId(): ?IdInterface
65    {
66        return $this->user?->getId();
67    }
68
69    public function isAuthenticated(): bool
70    {
71        return $this->user instanceof User;
72    }
73}