Passed
Pull Request — master (#5329)
by Angel Fernando Quiroz
09:42
created

UserHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 18
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getCurrent() 0 12 3
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\ServiceHelper;
8
9
use Chamilo\CoreBundle\Entity\User;
10
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
class UserHelper
14
{
15
    public function __construct(
16
        private readonly TokenStorageInterface $tokenStorage,
17
    ) {}
18
19
    public function getCurrent(): ?User
20
    {
21
        $token = $this->tokenStorage->getToken();
22
23
        if (!$token) {
24
            return null;
25
        }
26
27
        /** @var User|null $user */
28
        $user = $token->getUser();
29
30
        return $user instanceof UserInterface ? $user : null;
31
    }
32
}
33