Passed
Push — master ( d82392...67272d )
by Alexander
04:51
created

UserRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A findAll() 0 3 1
A findById() 0 3 1
A __construct() 0 4 1
A findByLoginWithAuthIdentity() 0 3 1
A save() 0 3 1
A findBy() 0 3 1
A findByLogin() 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\Yii\Cycle\Data\Reader\EntityReader;
11
use Yiisoft\Yii\Cycle\Data\Writer\EntityWriter;
12
13
final class UserRepository extends Select\Repository
14
{
15
    private EntityWriter $entityWriter;
16
17 11
    public function __construct(Select $select, EntityWriter $entityWriter)
18
    {
19 11
        $this->entityWriter = $entityWriter;
20 11
        parent::__construct($select);
21
    }
22
23 1
    public function findAll(array $scope = [], array $orderBy = []): DataReaderInterface
24
    {
25 1
        return new EntityReader($this->select()->where($scope)->orderBy($orderBy));
26
    }
27
28
    /**
29
     * @param string $id
30
     *
31
     * @return User|null
32
     */
33
    public function findById(string $id): ?User
34
    {
35
        return $this->findByPK($id);
36
    }
37
38 3
    public function findByLogin(string $login): ?User
39
    {
40 3
        return $this->findBy('login', $login);
41
    }
42
43 4
    public function findByLoginWithAuthIdentity(string $login): ?User
44
    {
45 4
        return $this->select()->where(['login' => $login])->load('identity')->fetchOne();
46
    }
47
48
    /**
49
     * @throws Throwable
50
     */
51 2
    public function save(User $user): void
52
    {
53 2
        $this->entityWriter->write([$user]);
54
    }
55
56 3
    private function findBy(string $field, string $value): ?User
57
    {
58 3
        return $this->findOne([$field => $value]);
59
    }
60
}
61