Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix psalm errors in apps/user_ldap/lib/Jobs/CleanUp.php
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc authored and backportbot[bot] committed Feb 10, 2022
commit 32b71b6fe7c73539b532c0df10adf0597ffd4bb5
40 changes: 14 additions & 26 deletions apps/user_ldap/lib/Jobs/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @package OCA\User_LDAP\Jobs;
*/
class CleanUp extends TimedJob {
/** @var int $limit amount of users that should be checked per run */
/** @var ?int $limit amount of users that should be checked per run */
protected $limit;

/** @var int $defaultIntervalMin default interval in minutes */
Expand Down Expand Up @@ -77,7 +77,7 @@ public function __construct(User_Proxy $userBackend, DeletedUsersIndex $dui) {
* assigns the instances passed to run() to the class properties
* @param array $arguments
*/
public function setArguments($arguments) {
public function setArguments($arguments): void {
//Dependency Injection is not possible, because the constructor will
//only get values that are serialized to JSON. I.e. whatever we would
//pass in app.php we do add here, except something else is passed e.g.
Expand Down Expand Up @@ -120,38 +120,29 @@ public function setArguments($arguments) {
* makes the background job do its work
* @param array $argument
*/
public function run($argument) {
public function run($argument): void {
$this->setArguments($argument);

if (!$this->isCleanUpAllowed()) {
return;
}
$users = $this->mapping->getList($this->getOffset(), $this->getChunkSize());
if (!is_array($users)) {
//something wrong? Let's start from the beginning next time and
//abort
$this->setOffset(true);
return;
}
$resetOffset = $this->isOffsetResetNecessary(count($users));
$this->checkUsers($users);
$this->setOffset($resetOffset);
}

/**
* checks whether next run should start at 0 again
* @param int $resultCount
* @return bool
*/
public function isOffsetResetNecessary($resultCount) {
public function isOffsetResetNecessary(int $resultCount): bool {
return $resultCount < $this->getChunkSize();
}

/**
* checks whether cleaning up LDAP users is allowed
* @return bool
*/
public function isCleanUpAllowed() {
public function isCleanUpAllowed(): bool {
try {
if ($this->ldapHelper->haveDisabledConfigurations()) {
return false;
Expand All @@ -165,9 +156,8 @@ public function isCleanUpAllowed() {

/**
* checks whether clean up is enabled by configuration
* @return bool
*/
private function isCleanUpEnabled() {
private function isCleanUpEnabled(): bool {
return (bool)$this->ocConfig->getSystemValue(
'ldapUserCleanupInterval', (string)$this->defaultIntervalMin);
}
Expand All @@ -176,7 +166,7 @@ private function isCleanUpEnabled() {
* checks users whether they are still existing
* @param array $users result from getMappedUsers()
*/
private function checkUsers(array $users) {
private function checkUsers(array $users): void {
foreach ($users as $user) {
$this->checkUser($user);
}
Expand All @@ -186,7 +176,7 @@ private function checkUsers(array $users) {
* checks whether a user is still existing in LDAP
* @param string[] $user
*/
private function checkUser(array $user) {
private function checkUser(array $user): void {
if ($this->userBackend->userExistsOnLDAP($user['name'])) {
//still available, all good

Expand All @@ -198,29 +188,27 @@ private function checkUser(array $user) {

/**
* gets the offset to fetch users from the mappings table
* @return int
*/
private function getOffset() {
return (int)$this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', 0);
private function getOffset(): int {
return (int)$this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', '0');
}

/**
* sets the new offset for the next run
* @param bool $reset whether the offset should be set to 0
*/
public function setOffset($reset = false) {
public function setOffset(bool $reset = false): void {
$newOffset = $reset ? 0 :
$this->getOffset() + $this->getChunkSize();
$this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', $newOffset);
$this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', (string)$newOffset);
}

/**
* returns the chunk size (limit in DB speak)
* @return int
*/
public function getChunkSize() {
public function getChunkSize(): int {
if ($this->limit === null) {
$this->limit = (int)$this->ocConfig->getAppValue('user_ldap', 'cleanUpJobChunkSize', 50);
$this->limit = (int)$this->ocConfig->getAppValue('user_ldap', 'cleanUpJobChunkSize', '50');
}
return $this->limit;
}
Expand Down