Passed
Push — master ( 7a227f...bebcd3 )
by Alexander
09:27 queued 03:41
created

UserService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 22
ccs 0
cts 10
cp 0
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 3 1
A hasPermission() 0 4 2
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User;
6
7
use Yiisoft\Access\AccessCheckerInterface;
8
use Yiisoft\User\User as UserComponent;
9
10
final class UserService
11
{
12
    private UserComponent $user;
13
    private UserRepository $repository;
14
    private AccessCheckerInterface $accessChecker;
15
16
    public function __construct(UserComponent $user, UserRepository $repository, AccessCheckerInterface $accessChecker)
17
    {
18
        $this->user = $user;
19
        $this->repository = $repository;
20
        $this->accessChecker = $accessChecker;
21
    }
22
23
    public function getUser(): ?User
24
    {
25
        return $this->repository->findIdentity($this->user->getId());
0 ignored issues
show
Bug introduced by
It seems like $this->user->getId() can also be of type null; however, parameter $id of App\User\UserRepository::findIdentity() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        return $this->repository->findIdentity(/** @scrutinizer ignore-type */ $this->user->getId());
Loading history...
Bug Best Practice introduced by
The expression return $this->repository...y($this->user->getId()) could return the type Yiisoft\Auth\IdentityInterface which includes types incompatible with the type-hinted return App\User\User|null. Consider adding an additional type-check to rule them out.
Loading history...
26
    }
27
28
    public function hasPermission(string $permission): bool
29
    {
30
        $userId = $this->user->getId();
31
        return null !== $userId && $this->accessChecker->userHasPermission($userId, $permission);
32
    }
33
}
34