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

UserRepository::findIdentityBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 6
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\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