Test Failed
Pull Request — master (#513)
by Wilmer
03:19
created

UserRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 63
ccs 13
cts 15
cp 0.8667
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A findByLoginWithAuthIdentity() 0 7 1
A findAll() 0 6 1
A save() 0 3 1
A getSort() 0 3 1
A findById() 0 3 1
A findBy() 0 3 1
A findByLogin() 0 3 1
A getReader() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User;
6
7
use Cycle\ORM\Select;
8
use Throwable;
9
use Yiisoft\Data\Reader\DataReaderInterface;
10
use Yiisoft\Data\Reader\Sort;
11
use Yiisoft\Yii\Cycle\Data\Reader\EntityReader;
12
use Yiisoft\Yii\Cycle\Data\Writer\EntityWriter;
13
14
final class UserRepository extends Select\Repository
15
{
16
    public function __construct(private EntityWriter $entityWriter, Select $select)
17 11
    {
18
        parent::__construct($select);
19 11
    }
20 11
21
    /**
22
     * @psalm-return DataReaderInterface<int, User>
23 1
     */
24
    public function getReader(): DataReaderInterface
25 1
    {
26
        return (new EntityReader($this->select()))->withSort($this->getSort());
27
    }
28
29
    private function getSort(): Sort
30
    {
31
        return Sort::only(['id', 'login'])->withOrder(['id' => 'asc']);
32
    }
33
34
    public function findAll(array $scope = [], array $orderBy = []): DataReaderInterface
35
    {
36
        return new EntityReader($this
37
            ->select()
38 3
            ->where($scope)
39
            ->orderBy($orderBy));
40 3
    }
41
42
    /**
43 4
     * @param string $id
44
     *
45 4
     * @return User|null
46
     */
47
    public function findById(string $id): ?User
48
    {
49
        return $this->findByPK($id);
50
    }
51 2
52
    public function findByLogin(string $login): ?User
53 2
    {
54
        return $this->findBy('login', $login);
55
    }
56 3
57
    public function findByLoginWithAuthIdentity(string $login): ?User
58 3
    {
59
        return $this
60
            ->select()
61
            ->where(['login' => $login])
62
            ->load('identity')
63
            ->fetchOne();
64
    }
65
66
    /**
67
     * @throws Throwable
68
     */
69
    public function save(User $user): void
70
    {
71
        $this->entityWriter->write([$user]);
72
    }
73
74
    private function findBy(string $field, string $value): ?User
75
    {
76
        return $this->findOne([$field => $value]);
77
    }
78
}
79