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\Auth\IdentityWithTokenRepositoryInterface; |
13
|
|
|
use Yiisoft\Data\Reader\Sort; |
14
|
|
|
use Yiisoft\Yii\Cycle\Data\Reader\EntityReader; |
15
|
|
|
|
16
|
|
|
final class UserRepository extends Select\Repository implements IdentityWithTokenRepositoryInterface, IdentityRepositoryInterface |
17
|
|
|
{ |
18
|
|
|
private ORMInterface $orm; |
19
|
|
|
|
20
|
12 |
|
public function __construct(Select $select, ORMInterface $orm) |
21
|
|
|
{ |
22
|
12 |
|
$this->orm = $orm; |
23
|
12 |
|
parent::__construct($select); |
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
public function findAllOrderByLogin(): EntityReader |
27
|
|
|
{ |
28
|
1 |
|
return (new EntityReader($this->select())) |
29
|
1 |
|
->withSort( |
30
|
1 |
|
Sort::only(['login'])->withOrderString('login') |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function findIdentity(string $id): ?IdentityInterface |
35
|
|
|
{ |
36
|
|
|
return $this->findIdentityBy('id', $id); |
37
|
|
|
} |
38
|
|
|
|
39
|
9 |
|
public function findIdentityByToken(string $token, string $type = null): ?IdentityInterface |
40
|
|
|
{ |
41
|
9 |
|
return $this->findIdentityBy('token', $token); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function findByLogin(string $login): ?IdentityInterface |
45
|
|
|
{ |
46
|
1 |
|
return $this->findIdentityBy('login', $login); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
public function save(IdentityInterface $user): void |
50
|
|
|
{ |
51
|
2 |
|
$transaction = new Transaction($this->orm); |
|
|
|
|
52
|
2 |
|
$transaction->persist($user); |
53
|
2 |
|
$transaction->run(); |
54
|
|
|
} |
55
|
|
|
|
56
|
10 |
|
private function findIdentityBy(string $field, string $value): ?IdentityInterface |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* @var $identity IdentityInterface|null |
60
|
|
|
*/ |
61
|
10 |
|
return $this->findOne([$field => $value]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|