Skip to content

Commit

Permalink
Provide sync session info on each entry / referral / syncIdSet handle…
Browse files Browse the repository at this point in the history
…r as an optional parameter. This contains potentially needed information, such as the current cookie or the current stage / phase of the sync process.
  • Loading branch information
ChadSikorra committed Jun 19, 2023
1 parent 75a8eb0 commit 0ca4834
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 31 deletions.
14 changes: 14 additions & 0 deletions src/FreeDSx/Ldap/Operation/Request/SyncRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class SyncRequest extends SearchRequest
{
private ?Closure $syncIdSetHandler = null;

private ?Closure $phaseChangeHandler = null;

public function useSyncIdSetHandler(?Closure $syncIdSetHandler): self
{
$this->syncIdSetHandler = $syncIdSetHandler;
Expand All @@ -21,4 +23,16 @@ public function getSyncIdSetHandler(): ?Closure
{
return $this->syncIdSetHandler;
}

public function usePhaseChangeHandler(?Closure $phaseChangeHandler): self
{
$this->phaseChangeHandler = $phaseChangeHandler;

return $this;
}

public function getPhaseChangeHandler(): ?Closure
{
return $this->phaseChangeHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ private function initializeSync(LdapMessageRequest $messageTo): void
->getByClass(SyncRequestControl::class) ?? throw new RuntimeException(sprintf(
'Expected a "%s", but there is none.',
SyncRequestControl::class,
));;
));

if ($this->isContentUpdatePoll()) {
$syncStage = Session::CONTENT_UPDATE;
$syncStage = Session::STAGE_PERSIST;
} else {
$syncStage = Session::INITIAL_CONTENT;
$syncStage = Session::STAGE_REFRESH;
}

$this->session = new Session(
phase: $syncStage,
stage: $syncStage,
cookie: $this->syncRequestControl->getCookie(),
);

Expand Down Expand Up @@ -138,7 +138,8 @@ private function processSyncEntry(EntryResult $entryResult): void

call_user_func(
$this->syncEntryHandler,
new SyncEntryResult($entryResult)
new SyncEntryResult($entryResult),
$this->session,
);
}

Expand All @@ -150,7 +151,8 @@ private function processSyncReferral(ReferralResult $referralResult): void

call_user_func(
$this->syncReferralHandler,
new SyncReferralResult($referralResult)
new SyncReferralResult($referralResult),
$this->session,
);
}

Expand All @@ -161,12 +163,12 @@ private function processIntermediateResponse(LdapMessageResponse $messageFrom):
if ($response instanceof SyncRefreshDelete) {
$this->syncRequestControl->setCookie($response->getCookie());
$this->session
->updatePhase(Session::REFRESH)
->updatePhase(Session::PHASE_DELETE)
->updateCookie($response->getCookie());
} elseif ($response instanceof SyncRefreshPresent) {
$this->syncRequestControl->setCookie($response->getCookie());
$this->session
->updatePhase(Session::REFRESH)
->updatePhase(Session::PHASE_PRESENT)
->updateCookie($response->getCookie());
} elseif ($response instanceof SyncNewCookie) {
$this->session->updateCookie($response->getCookie());
Expand Down Expand Up @@ -198,4 +200,3 @@ private function isRefreshRequired(LdapMessageResponse $response): bool
return $result->getResultCode() === ResultCode::SYNCHRONIZATION_REFRESH_REQUIRED;
}
}

68 changes: 48 additions & 20 deletions src/FreeDSx/Ldap/Sync/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,79 @@

final class Session
{
public const INITIAL_CONTENT = 0;
public const PHASE_DELETE = 0;

public const CONTENT_UPDATE = 1;
public const PHASE_PRESENT = 1;

public const REFRESH_AND_PERSIST = 2;
public const STAGE_REFRESH = 0;

public const REFRESH = 3;

public const PERSIST = 4;
public const STAGE_PERSIST = 1;

public function __construct(
private int $phase,
private int $stage,
private ?string $cookie,
)
{}
private ?int $phase = null,
) {}

/**
* The cookie that represents this sync session.
*/
public function getCookie(): ?string
{
return $this->cookie;
}

/**
* The phase currently in progress for the sync session. One of:
*
* - {@see self::INITIAL_CONTENT}
* - {@see self::CONTENT_UPDATE}
* - {@see self::REFRESH_AND_PERSIST}
* - {@see self::REFRESH}
* - {@see self::PERSIST}
* - {@see self::PHASE_DELETE}
* - {@see self::PHASE_PRESENT}
*
* May return null if neither of those phases are currently active.
*/
public function getPhase(): int
public function getPhase(): ?int
{
return $this->phase;
}

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

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


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

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


/**
* The cookie that represents this sync session.
* @internal
*/
public function getCookie(): ?string
public function updatePhase(?int $phase): self
{
return $this->cookie;
$this->phase = $phase;

return $this;
}

/**
* @internal
*/
public function updatePhase(int $phase): self
public function updateStage(int $stage): self
{
$this->phase = $phase;
$this->stage = $stage;

return $this;
}
Expand Down
16 changes: 14 additions & 2 deletions src/FreeDSx/Ldap/Sync/SyncRepl.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,32 @@ public function getCookie(): ?string
return $this->cookie;
}

public function useRefreshOnlyMode(): self
public function refreshOnly(): self
{
$this->mode = SyncRequestControl::MODE_REFRESH_ONLY;

return $this;
}

public function useRefreshAndPersistMode(): self
public function refreshAndPersist(): self
{
$this->mode = SyncRequestControl::MODE_REFRESH_AND_PERSIST;

return $this;
}

public function noTimeout(): self
{
$this->client->setOptions(
options: $this->client
->getOptions()
->setTimeoutRead(-1),
forceDisconnect: true,
);

return $this;
}

public function sync(): void
{
$this->getResponseAndUpdateCookie(
Expand Down

0 comments on commit 0ca4834

Please sign in to comment.