Code cleanup

This commit is contained in:
James Cole
2024-12-22 08:43:12 +01:00
parent 5751f7e5a3
commit 565bd87959
574 changed files with 4600 additions and 4604 deletions

View File

@@ -174,6 +174,18 @@ class UserGroupRepository implements UserGroupRepositoryInterface
return UserGroup::all();
}
#[\Override]
public function getById(int $id): ?UserGroup
{
return UserGroup::find($id);
}
#[\Override]
public function getMembershipsFromGroupId(int $groupId): Collection
{
return $this->user->groupMemberships()->where('user_group_id', $groupId)->get();
}
public function setUser(null|Authenticatable|User $user): void
{
app('log')->debug(sprintf('Now in %s', __METHOD__));
@@ -207,7 +219,7 @@ class UserGroupRepository implements UserGroupRepositoryInterface
$user = User::find($data['id']);
app('log')->debug('Found user by ID');
}
if (array_key_exists('email', $data) && '' !== (string)$data['email']) {
if (array_key_exists('email', $data) && '' !== (string) $data['email']) {
/** @var null|User $user */
$user = User::whereEmail($data['email'])->first();
app('log')->debug('Found user by email');
@@ -225,13 +237,13 @@ class UserGroupRepository implements UserGroupRepositoryInterface
if (1 === $membershipCount) {
$lastUserId = $userGroup->groupMemberships()->distinct()->first(['group_memberships.user_id'])->user_id;
// if this is also the user we're editing right now, and we remove all of their roles:
if ($lastUserId === (int)$user->id && 0 === count($data['roles'])) {
if ($lastUserId === (int) $user->id && 0 === count($data['roles'])) {
app('log')->debug('User is last in this group, refuse to act');
throw new FireflyException('You cannot remove the last member from this user group. Delete the user group instead.');
}
// if this is also the user we're editing right now, and do not grant them the owner role:
if ($lastUserId === (int)$user->id && count($data['roles']) > 0 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)) {
if ($lastUserId === (int) $user->id && count($data['roles']) > 0 && !in_array(UserRoleEnum::OWNER->value, $data['roles'], true)) {
app('log')->debug('User needs to have owner role in this group, refuse to act');
throw new FireflyException('The last member in this user group must get or keep the "owner" role.');
@@ -295,16 +307,4 @@ class UserGroupRepository implements UserGroupRepositoryInterface
$this->user->user_group_id = $userGroup->id;
$this->user->save();
}
#[\Override]
public function getMembershipsFromGroupId(int $groupId): Collection
{
return $this->user->groupMemberships()->where('user_group_id', $groupId)->get();
}
#[\Override]
public function getById(int $id): ?UserGroup
{
return UserGroup::find($id);
}
}

View File

@@ -36,13 +36,13 @@ interface UserGroupRepositoryInterface
{
public function destroy(UserGroup $userGroup): void;
public function getMembershipsFromGroupId(int $groupId): Collection;
public function get(): Collection;
public function getAll(): Collection;
public function getById(int $id): ?UserGroup;
public function getAll(): Collection;
public function getMembershipsFromGroupId(int $groupId): Collection;
public function setUser(null|Authenticatable|User $user): void;