SymfonyDatabaseLoggedInUserProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 28
ccs 9
cts 11
cp 0.8182
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getUser() 0 13 4
1
<?php
2
3
namespace App\Modules\Security\Domain\Provider;
4
5
use App\Infrastructure\Security\LoggedInUser;
6
use App\Infrastructure\Security\LoggedInUserProviderInterface;
7
use App\Modules\Security\Persistence\Doctrine\Entity\User;
8
use Symfony\Component\Security\Core\Security;
9
10
class SymfonyDatabaseLoggedInUserProvider implements LoggedInUserProviderInterface
11
{
12
13
    /**
14
     * @param Security $security
15
     */
16 16
    public function __construct(
17
        private Security $security
18
    )
19
    {
20 16
    }
21
22
    /**
23
     * @return LoggedInUser
24
     */
25 16
   public function getUser(): LoggedInUser
26
    {
27 16
        $user = $this->security->getUser();
28 16
        if ($user == null) {
29
            throw new \RuntimeException("You are not logged in!");
30
        }
31 16
        if ($user instanceof LoggedInUser) {
32 13
            return $user;
33
        }
34 3
        if ($user instanceof User) {
35 3
            return new LoggedInUser($user->getId(), $user->getUserIdentifier(), $user->getRoles());
36
        }
37
        throw new \RuntimeException("Unsupported User Class: " . $user::class);
38
    }
39
}