Passed
Pull Request — master (#408)
by Wilmer
03:58
created

UserRepository::findByLogin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
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 11
    }
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 2
    public function findByLogin(string $login): ?User
39
    {
40 2
        return $this->findBy('login', $login);
41
    }
42
43 3
    public function findByLoginWithAuthIdentity(string $login): ?User
44
    {
45 3
        return $this->select()->where(['login' => $login])->load('identity')->fetchOne();
46
    }
47
48
    /**
49
     * @throws Throwable
50
     */
51 1
    public function save(User $user): void
52
    {
53 1
        $this->entityWriter->write([$user]);
54 1
    }
55
56 2
    private function findBy(string $field, string $value): ?User
57
    {
58 2
        return $this->findOne([$field => $value]);
59
    }
60
}
61