SymfonyDatabaseLoggedInUserProvider::getUser()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 13
ccs 7
cts 9
cp 0.7778
crap 4.1755
rs 10
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
}