Passed
Push — master ( 2c3696...c338c9 )
by Alexander
03:12
created

UserRepository::findIdentityByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User;
6
7
use Cycle\ORM\ORMInterface;
8
use Cycle\ORM\Select;
9
use Cycle\ORM\Transaction;
10
use Yiisoft\Auth\IdentityInterface;
11
use Yiisoft\Auth\IdentityRepositoryInterface;
12
use Yiisoft\Auth\IdentityWithTokenRepositoryInterface;
13
14
final class UserRepository extends Select\Repository implements IdentityWithTokenRepositoryInterface, IdentityRepositoryInterface
15
{
16
    private ORMInterface $orm;
17
18 12
    public function __construct(Select $select, ORMInterface $orm)
19
    {
20 12
        $this->orm = $orm;
21 12
        parent::__construct($select);
22 12
    }
23
24
    public function findIdentity(string $id): ?IdentityInterface
25
    {
26
        return $this->findIdentityBy('id', $id);
27
    }
28
29 5
    public function findIdentityByToken(string $token, string $type = null): ?IdentityInterface
30
    {
31 5
        return $this->findIdentityBy('token', $token);
32
    }
33
34 1
    public function findByLogin(string $login): ?IdentityInterface
35
    {
36 1
        return $this->findIdentityBy('login', $login);
37
    }
38
39 2
    public function save(IdentityInterface $user): void
40
    {
41 2
        $transaction = new Transaction($this->orm);
42 2
        $transaction->persist($user);
43 2
        $transaction->run();
44 2
    }
45
46 6
    private function findIdentityBy(string $field, string $value): ?IdentityInterface
47
    {
48
        /**
49
         * @var $identity IdentityInterface|null
50
         */
51 6
        return $this->findOne([$field => $value]);
52
    }
53
}
54