1 | <?php |
||
2 | |||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
3 | namespace BristolSU\ControlDB\Observers\NotifyObservers; |
||
4 | |||
5 | use BristolSU\ControlDB\Contracts\Models\User as UserModel; |
||
6 | use BristolSU\ControlDB\Contracts\Repositories\User as UserRepository; |
||
7 | use BristolSU\ControlDB\Observers\NotifyObservers\Framework\Notifier; |
||
8 | use BristolSU\ControlDB\Observers\NotifyObservers\Framework\ObserverStore; |
||
9 | use Illuminate\Support\Collection; |
||
10 | |||
11 | class UserNotifier extends Notifier implements UserRepository |
||
0 ignored issues
–
show
|
|||
12 | { |
||
13 | |||
14 | /** |
||
0 ignored issues
–
show
|
|||
15 | * @var UserRepository |
||
16 | */ |
||
17 | private $userRepository; |
||
0 ignored issues
–
show
|
|||
18 | |||
19 | 47 | public function __construct(UserRepository $userRepository, ObserverStore $observerStore) |
|
0 ignored issues
–
show
|
|||
20 | { |
||
21 | 47 | parent::__construct($observerStore, UserRepository::class); |
|
22 | 47 | $this->userRepository = $userRepository; |
|
23 | 47 | } |
|
24 | |||
25 | /** |
||
26 | * Get a user by ID |
||
27 | * |
||
28 | * @param int $id ID of the user |
||
29 | * @return UserModel |
||
0 ignored issues
–
show
|
|||
30 | */ |
||
31 | 36 | public function getById(int $id): UserModel |
|
32 | { |
||
33 | 36 | return $this->userRepository->getById($id); |
|
34 | } |
||
35 | |||
36 | /** |
||
37 | * Get a user by its data provider ID |
||
38 | * |
||
39 | * @param int $dataProviderId |
||
0 ignored issues
–
show
|
|||
40 | * @return UserModel |
||
0 ignored issues
–
show
|
|||
41 | */ |
||
42 | 2 | public function getByDataProviderId(int $dataProviderId): UserModel |
|
43 | { |
||
44 | 2 | return $this->userRepository->getByDataProviderId($dataProviderId); |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get all users |
||
49 | * |
||
50 | * @return Collection|UserModel[] |
||
51 | */ |
||
52 | 2 | public function all(): Collection |
|
53 | { |
||
54 | 2 | return $this->userRepository->all(); |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Create a new user |
||
59 | * |
||
60 | * @param int $dataProviderId |
||
0 ignored issues
–
show
|
|||
61 | * @return UserModel |
||
0 ignored issues
–
show
|
|||
62 | */ |
||
63 | 2 | public function create(int $dataProviderId): UserModel |
|
64 | { |
||
65 | 2 | $userModel = $this->userRepository->create($dataProviderId); |
|
66 | 2 | $this->notify('create', $userModel); |
|
67 | 2 | return $userModel; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Delete a user by ID |
||
72 | * |
||
73 | * @param int $id |
||
0 ignored issues
–
show
|
|||
74 | */ |
||
0 ignored issues
–
show
|
|||
75 | 1 | public function delete(int $id): void |
|
76 | { |
||
77 | 1 | $userModel = $this->getById($id); |
|
78 | 1 | $this->userRepository->delete($id); |
|
79 | 1 | $this->notify('delete', $userModel); |
|
80 | 1 | } |
|
81 | |||
82 | /** |
||
83 | * Update the user model |
||
84 | * |
||
85 | * @param int $id |
||
0 ignored issues
–
show
|
|||
86 | * @param int $dataProviderId New data provider ID |
||
87 | * @return UserModel |
||
0 ignored issues
–
show
|
|||
88 | */ |
||
89 | 1 | public function update(int $id, int $dataProviderId): UserModel |
|
90 | { |
||
91 | 1 | $oldUserModel = $this->getById($id); |
|
92 | 1 | $newUserModel = $this->userRepository->update($id, $dataProviderId); |
|
93 | 1 | $this->notify('update', $oldUserModel, $newUserModel); |
|
94 | 1 | return $newUserModel; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Paginate through all the users |
||
99 | * |
||
100 | * @param int $page The page number to return |
||
0 ignored issues
–
show
|
|||
101 | * @param int $perPage The number of results to return per page |
||
102 | * |
||
103 | * @return Collection|UserModel[] |
||
104 | */ |
||
105 | 2 | public function paginate(int $page, int $perPage): Collection |
|
106 | { |
||
107 | 2 | return $this->userRepository->paginate($page, $perPage); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get the number of users |
||
112 | * |
||
113 | * @return int |
||
114 | */ |
||
115 | 2 | public function count(): int |
|
116 | { |
||
117 | 2 | return $this->userRepository->count(); |
|
118 | } |
||
119 | } |