|
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
|
|
|
|