-
-
Notifications
You must be signed in to change notification settings - Fork 546
/
Copy pathUserRepository.php
76 lines (62 loc) · 1.66 KB
/
UserRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Statamic\Stache\Repositories;
use Statamic\Auth\File\RoleRepository;
use Statamic\Auth\File\User as FileUser;
use Statamic\Auth\File\UserGroupRepository;
use Statamic\Auth\UserCollection;
use Statamic\Auth\UserRepository as BaseRepository;
use Statamic\Contracts\Auth\User;
use Statamic\Stache\Query\UserQueryBuilder;
use Statamic\Stache\Stache;
class UserRepository extends BaseRepository
{
protected $stache;
protected $store;
protected $config;
protected $roleRepository = RoleRepository::class;
protected $userGroupRepository = UserGroupRepository::class;
public function __construct(Stache $stache, array $config = [])
{
$this->stache = $stache;
$this->store = $stache->store('users');
$this->config = $config;
}
public function make(): User
{
return new FileUser;
}
public function all(): UserCollection
{
return $this->query()->get();
}
public function find($id): ?User
{
return $this->store->getItem($id);
}
public function findByEmail(string $email): ?User
{
return $this->query()->where('email', $email)->first();
}
public function query()
{
return new UserQueryBuilder($this->store);
}
public function save(User $user)
{
if (! $user->id()) {
$user->id($this->stache->generateId());
}
$this->store->save($user);
}
public function delete(User $user)
{
$this->store->delete($user);
}
public function fromUser($user): ?User
{
if ($user instanceof User) {
return $user;
}
return null;
}
}