Test Failed
Pull Request — master (#20)
by Alexander
04:07 queued 01:32
created

UserRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 7

7 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
A findAllOrderByLogin() 0 5 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\Data\Reader\Sort;
13
use Yiisoft\Yii\Cycle\DataReader\SelectDataReader;
14
15
final class UserRepository extends Select\Repository implements IdentityRepositoryInterface
16
{
17
    private ORMInterface $orm;
18
19
    public function __construct(Select $select, ORMInterface $orm)
20
    {
21
        $this->orm = $orm;
22
        parent::__construct($select);
23
    }
24
25
    public function findAllOrderByLogin(): SelectDataReader
26
    {
27
        return (new SelectDataReader($this->select()))
28
            ->withSort(
29
                (new Sort([]))->withOrderString('login')
30
            );
31
    }
32
33
    public function findIdentity(string $id): ?IdentityInterface
34
    {
35
        return $this->findIdentityBy('id', $id);
36
    }
37
38
    public function findIdentityByToken(string $token, string $type = null): ?IdentityInterface
39
    {
40
        return $this->findIdentityBy('token', $token);
41
    }
42
43
    public function findByLogin(string $login): ?IdentityInterface
44
    {
45
        return $this->findIdentityBy('login', $login);
46
    }
47
48
    public function save(IdentityInterface $user): void
49
    {
50
        $transaction = new Transaction($this->orm);
51
        $transaction->persist($user);
52
        $transaction->run();
53
    }
54
55
    private function findIdentityBy(string $field, string $value): ?IdentityInterface
56
    {
57
        /**
58
         * @var $identity IdentityInterface|null
59
         */
60
        return $this->findOne([$field => $value]);
61
    }
62
}
63