diff --git a/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php b/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php index 956cffbd2..fb045111c 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php @@ -131,6 +131,25 @@ class StoredObject implements Document, TrackCreationInterface $this->locks->removeElement($lock); } + public function isLockedAt(\DateTimeImmutable $at): bool + { + foreach ($this->locks as $lock) { + if ($lock->isActiveAt($at)) { + return true; + } + } + + return false; + } + + /** + * @return Collection + */ + public function getLocks(): Collection + { + return $this->locks; + } + public function addGenerationTrial(): self { ++$this->generationTrialsCounter; diff --git a/src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php b/src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php index 40fe9b284..2f7a3fcf7 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/StoredObjectLock.php @@ -118,4 +118,15 @@ class StoredObjectLock { return $this->expireAt; } + + /** + * Return true if the lock must be considered as active. + * + * A StoredObjectLock is active if there isn't any expiration date, or + * if the expiration date and time is before the given time. + */ + public function isActiveAt(\DateTimeImmutable $at): bool + { + return null === $this->getExpireAt() || $at < $this->getExpireAt(); + } } diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectLockTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectLockTest.php new file mode 100644 index 000000000..a45bcb1c7 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectLockTest.php @@ -0,0 +1,76 @@ +isActiveAt($now)); + self::assertTrue($lock->isActiveAt($now->modify('+1 year'))); + } + + public function testIsActiveAtWithFutureExpiration(): void + { + $storedObject = new StoredObject(); + $now = new \DateTimeImmutable(); + $expireAt = $now->modify('+1 hour'); + $lock = new StoredObjectLock( + $storedObject, + StoredObjectLockMethodEnum::WOPI, + $now, + 'token', + $expireAt + ); + + self::assertTrue($lock->isActiveAt($now)); + self::assertTrue($lock->isActiveAt($now->modify('+30 minutes'))); + } + + public function testIsActiveAtWithPastExpiration(): void + { + $storedObject = new StoredObject(); + $now = new \DateTimeImmutable(); + $expireAt = $now->modify('-1 hour'); + $lock = new StoredObjectLock( + $storedObject, + StoredObjectLockMethodEnum::WOPI, + $now->modify('-2 hours'), + 'token', + $expireAt + ); + + self::assertFalse($lock->isActiveAt($now)); + self::assertFalse($lock->isActiveAt($expireAt)); + self::assertFalse($lock->isActiveAt($expireAt->modify('+1 second'))); + } +}