Passed
Pull Request — master (#138)
by Sergei
11:25
created

UserService::hasPermission()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service;
6
7
use App\Entity\User;
8
use App\Repository\UserRepository;
9
use Yiisoft\Access\AccessCheckerInterface;
10
use Yiisoft\Yii\Web\User\User as UserComponent;
11
12
final class UserService
13
{
14
    private UserComponent $user;
15
    private UserRepository $repository;
16
    private AccessCheckerInterface $accessChecker;
17
18
    public function __construct(UserComponent $user, UserRepository $repository, AccessCheckerInterface $accessChecker)
19
    {
20
        $this->user = $user;
21
        $this->repository = $repository;
22
        $this->accessChecker = $accessChecker;
23
    }
24
25
    public function getUser(): ?User
26
    {
27
        return $this->repository->findIdentity($this->user->getId());
0 ignored issues
show
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\Entity\User|null. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
It seems like $this->user->getId() can also be of type null; however, parameter $id of App\Repository\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

27
        return $this->repository->findIdentity(/** @scrutinizer ignore-type */ $this->user->getId());
Loading history...
28
    }
29
30
    public function hasPermission(string $permission): bool
31
    {
32
        $userId = $this->user->getId();
33
        return !is_null($userId) && $this->accessChecker->userHasPermission($userId, $permission);
34
    }
35
}
36