Skip to content

Commit

Permalink
feat(CirclesManager): Add flag to enforce synchronous event execution
Browse files Browse the repository at this point in the history
Required for testing (where only one php thread is available), but as
well for clients that require the event to be excecuted immediately.

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Nov 25, 2024
1 parent e56f569 commit 3d77c21
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
14 changes: 9 additions & 5 deletions lib/CirclesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class CirclesManager {
/** @var CirclesQueryHelper */
private $circlesQueryHelper;

private bool $forceSync = false;

/**
* CirclesManager constructor.
Expand Down Expand Up @@ -152,7 +153,8 @@ public function getLocalFederatedUser(string $userId): FederatedUser {
* @throws InvalidIdException
* @throws FederatedUserException
*/
public function startSession(?FederatedUser $federatedUser = null): void {
public function startSession(?FederatedUser $federatedUser = null, bool $forceSync = false): void {
$this->forceSync = $forceSync;
if (is_null($federatedUser)) {
$this->federatedUserService->initCurrentUser();
} else {
Expand All @@ -163,7 +165,8 @@ public function startSession(?FederatedUser $federatedUser = null): void {
/**
*
*/
public function startSuperSession(): void {
public function startSuperSession(bool $forceSync = false): void {
$this->forceSync = $forceSync;
$this->federatedUserService->unsetCurrentUser();
$this->federatedUserService->bypassCurrentUserCondition(true);
}
Expand Down Expand Up @@ -224,6 +227,7 @@ public function startOccSession(
public function stopSession(): void {
$this->federatedUserService->unsetCurrentUser();
$this->federatedUserService->bypassCurrentUserCondition(false);
$this->forceSync = false;
}


Expand Down Expand Up @@ -298,7 +302,7 @@ public function createCircle(
* @throws UnknownRemoteException
*/
public function destroyCircle(string $singleId): void {
$this->circleService->destroy($singleId);
$this->circleService->destroy($singleId, $this->forceSync);
}


Expand Down Expand Up @@ -426,7 +430,7 @@ public function flagAsAppManaged(string $circleId, bool $enabled = true): void {
* @throws UnknownRemoteException
*/
public function addMember(string $circleId, FederatedUser $federatedUser): Member {
$outcome = $this->memberService->addMember($circleId, $federatedUser);
$outcome = $this->memberService->addMember($circleId, $federatedUser, $this->forceSync);
$member = new Member();
$member->import($outcome);

Expand Down Expand Up @@ -475,7 +479,7 @@ public function levelMember(string $memberId, int $level): Member {
* @throws UnknownRemoteException
*/
public function removeMember(string $memberId): void {
$this->memberService->removeMember($memberId);
$this->memberService->removeMember($memberId, $this->forceSync);
}


Expand Down
11 changes: 11 additions & 0 deletions lib/Model/Federated/FederatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class FederatedEvent implements JsonSerializable {
/** @var int */
private $bypass = 0;

private bool $forceSync = false;


/**
* FederatedEvent constructor.
Expand Down Expand Up @@ -552,6 +554,15 @@ public function canBypass(int $flag): bool {
return (($this->bypass & $flag) !== 0);
}

public function forceSync(bool $forceSync): self {
$this->forceSync = $forceSync;
return $this;
}

public function isForceSync(): bool {
return $this->forceSync;
}


/**
* @param array $data
Expand Down
4 changes: 3 additions & 1 deletion lib/Service/CircleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public function create(

/**
* @param string $circleId
* @param bool $forceSync
*
* @return array
* @throws CircleNotFoundException
Expand All @@ -235,13 +236,14 @@ public function create(
* @throws RequestBuilderException
* @throws UnknownRemoteException
*/
public function destroy(string $circleId): array {
public function destroy(string $circleId, bool $forceSync = false): array {
$this->federatedUserService->mustHaveCurrentUser();

$circle = $this->getCircle($circleId);

$event = new FederatedEvent(CircleDestroy::class);
$event->setCircle($circle);
$event->forceSync($forceSync);
$this->federatedEventService->newEvent($event);

return $event->getOutcome();
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/FederatedEventService.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private function confirmSharedItem(FederatedEvent $event, IFederatedItem $item):
* @param IFederatedItem $item
*/
private function configureEvent(FederatedEvent $event, IFederatedItem $item) {
if ($item instanceof IFederatedItemAsyncProcess) {
if ($item instanceof IFederatedItemAsyncProcess && !$event->isForceSync()) {
$event->setAsync(true);
}
if ($item instanceof IFederatedItemLimitedToInstanceWithMembership) {
Expand Down
7 changes: 5 additions & 2 deletions lib/Service/MemberService.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function getMembers(string $circleId): array {
* @throws InvalidIdException
* @throws SingleCircleNotFoundException
*/
public function addMember(string $circleId, FederatedUser $federatedUser): array {
public function addMember(string $circleId, FederatedUser $federatedUser, bool $forceSync = false): array {
$this->federatedUserService->mustHaveCurrentUser();
$circle = $this->circleRequest->getCircle($circleId, $this->federatedUserService->getCurrentUser());

Expand All @@ -204,6 +204,7 @@ public function addMember(string $circleId, FederatedUser $federatedUser): array
$event = new FederatedEvent(SingleMemberAdd::class);
$event->setCircle($circle);
$event->setMember($member);
$event->forceSync($forceSync);

$this->federatedEventService->newEvent($event);

Expand Down Expand Up @@ -261,6 +262,7 @@ function (FederatedUser $federatedUser) use ($patron) {

/**
* @param string $memberId
* @param bool $forceSync
*
* @return array
* @throws FederatedEventException
Expand All @@ -274,7 +276,7 @@ function (FederatedUser $federatedUser) use ($patron) {
* @throws UnknownRemoteException
* @throws RequestBuilderException
*/
public function removeMember(string $memberId): array {
public function removeMember(string $memberId, bool $forceSync = false): array {
$this->federatedUserService->mustHaveCurrentUser();
$member = $this->memberRequest->getMemberById(
$memberId,
Expand All @@ -284,6 +286,7 @@ public function removeMember(string $memberId): array {
$event = new FederatedEvent(MemberRemove::class);
$event->setCircle($member->getCircle());
$event->setMember($member);
$event->forceSync($forceSync);

$this->federatedEventService->newEvent($event);

Expand Down

0 comments on commit 3d77c21

Please sign in to comment.