Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to lock (toggle) users. #98

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions examples/user_usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,30 @@ public function createUser()
}
}

public function deleteUser()
public function deleteUser($userId)
{
$userId = '';

try {
$client = new User($this->host, $this->apiKey);
var_dump($client->deleteUser($userId));
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
}

public function setUserLock($userId, $toggle)
{
try {
$client = new User($this->host, $this->apiKey);
var_dump($client->setUserLock($userId, $toggle));
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
}
}

$users = new Users();
//$users->getCurrentUserInformation();
//$users->deleteCurrentUserProfile();
//$users->createUser();
//$users->deleteUser();
//$users->deleteUser("test@example.com");
//$users->setUserLock("test@example.com", true);
22 changes: 22 additions & 0 deletions src/Client/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,26 @@ public function deleteUser(string $userId): bool
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}

public function setUserLock(string $userId, bool $locked): bool
{
$url = $this->getApiUrl() . 'users/' . urlencode($userId) . '/lock';
$headers = $this->getRequestHeaders();
$method = 'POST';

$body = json_encode(
[
'locked' => $locked,
],
JSON_THROW_ON_ERROR
);

$response = $this->getHttpClient()->request($method, $url, $headers, $body);

if ($response->getStatus() === 200) {
return true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return true if you're never going to return false? Just return void and let us be happy there was no exception.

Copy link
Collaborator Author

@ndeet ndeet Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you search the code we have that in many places like this. The benefit over void is that you can rely on it if it was successful? e.g. if (someFunction()) {} ? A void return type would return null and you can't be sure it worked? On the other hand you have it in a try catch block anyway, so yeah not sure. Maybe keep it for now as of consistency with the other places we use it like this and refactor on a major release?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on sticking with existing code style. Would be better to refactor in next major version.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tracking it here #99

} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
}
}