Skip to content
Open
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
15 changes: 15 additions & 0 deletions src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ public function getAllUsers(): array
return $this->toEntities(get_users());
}

/** @return array<int> */
public function getUserIdsBatch(int $number, int $offset): array
{
return array_map(
'intval',
get_users([
'fields' => 'ID',
'number' => $number,
'offset' => $offset,
'orderby' => 'ID',
'order' => 'ASC',
]),
);
}

/** @return array<User> */
public function getAllMemberUsers(): array
{
Expand Down
15 changes: 11 additions & 4 deletions src/Service/MembershipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,18 @@ private function extendSectionsDates(int $userId): void

public function timeUnlockLevelsForAllUsers(): void
{
$users = $this->userRepository->getAllUsers();
$batchSize = 100;
$offset = 0;

foreach ($users as $user) {
$this->timeUnlockLevelsForUser($user->getId());
}
do {
$userIds = $this->userRepository->getUserIdsBatch($batchSize, $offset);

foreach ($userIds as $userId) {
$this->timeUnlockLevelsForUser($userId);
}

$offset += count($userIds);
} while (count($userIds) === $batchSize);
}

public function timeUnlockLevelsForUser(int $userId): void
Expand Down
56 changes: 56 additions & 0 deletions tests/access_logic_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,47 @@ public function getActiveWithAccessByUserId(int $userId): array
}
}

final class BatchedUserRepository extends UserRepository
{
/** @var array<array{int, int}> */
public array $calls = [];

public function __construct(private int $userCount)
{
}

public function getUserIdsBatch(int $number, int $offset): array
{
$this->calls[] = [$number, $offset];

if ($offset >= $this->userCount) {
return [];
}

return range(
$offset + 1,
min($offset + $number, $this->userCount),
);
}
}

final class BatchedMembershipService extends MembershipService
{
/** @var array<int> */
public array $processedUserIds = [];

public function __construct(UserRepository $userRepository)
{
$property = new ReflectionProperty(MembershipService::class, 'userRepository');
$property->setValue($this, $userRepository);
}

public function timeUnlockLevelsForUser(int $userId): void
{
$this->processedUserIds[] = $userId;
}
}

final class LoginLevelRepository extends LevelRepository
{
public function __construct(private MemberLevel $level)
Expand Down Expand Up @@ -431,4 +472,19 @@ function createUnlockController(
assertSameValue(null, $capturingService->savedMembership, 'Denied button unlock must not save a membership.');
}

$batchedUserRepository = new BatchedUserRepository(205);
$batchedMembershipService = new BatchedMembershipService($batchedUserRepository);
$batchedMembershipService->timeUnlockLevelsForAllUsers();

assertSameValue(
[[100, 0], [100, 100], [100, 200]],
$batchedUserRepository->calls,
'Users must be loaded in batches of 100.',
);
assertSameValue(
range(1, 205),
$batchedMembershipService->processedUserIds,
'Every user must be processed exactly once.',
);

echo "Access logic tests passed.\n";