. */ namespace SP\Domain\User\Services; use SP\Core\Application; use SP\Domain\Common\Services\Service; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\User\Models\UserToUserGroup as UserToUserGroupModel; use SP\Domain\User\Ports\UserToUserGroupRepository; use SP\Domain\User\Ports\UserToUserGroupService; /** * Class UserToUserGroup */ final class UserToUserGroup extends Service implements UserToUserGroupService { public function __construct( Application $application, private readonly UserToUserGroupRepository $userToUserGroupRepository ) { parent::__construct($application); } /** * @throws ConstraintException * @throws QueryException */ public function add(int $id, array $users): int { return $this->userToUserGroupRepository->add($id, $users)->getLastId(); } /** * @throws ConstraintException * @throws QueryException */ public function update(int $id, array $users): int { if (count($users) === 0) { return $this->userToUserGroupRepository->delete($id)->getAffectedNumRows(); } return $this->userToUserGroupRepository->update($id, $users)->getAffectedNumRows(); } /** * @param int $id * @return array */ public function getUsersByGroupId(int $id): array { return array_map( static fn(UserToUserGroupModel $userToUserGroup) => $userToUserGroup->getUserId(), $this->userToUserGroupRepository ->getById($id) ->getDataAsArray(UserToUserGroupModel::class) ); } /** * Returns the groups which the user belongs to * * @param int $userId * @return array */ public function getGroupsForUser(int $userId): array { return $this->userToUserGroupRepository ->getGroupsForUser($userId) ->getDataAsArray(UserToUserGroupModel::class); } }