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

UserRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 11
c 1
b 0
f 0
dl 0
loc 38
ccs 15
cts 17
cp 0.8824
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 5 1
A findIdentityBy() 0 6 1
A findByLogin() 0 3 1
A __construct() 0 4 1
A findIdentityByToken() 0 3 1
A findIdentity() 0 3 1
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