Add cron job for removing expired stored objects

Introduced `RemoveExpiredStoredObjectCronJob` to automate the deletion of expired stored objects every 7 days. Enhanced associated tests and updated relevant interfaces and classes to support the new cron job functionality.
This commit is contained in:
2024-08-28 11:41:43 +02:00
parent c38f7c1179
commit 0db2652f08
10 changed files with 344 additions and 12 deletions

View File

@@ -90,7 +90,7 @@ class StoredObject implements Document, TrackCreationInterface
/**
* @var Collection<int, StoredObjectVersion>
*/
#[ORM\OneToMany(targetEntity: StoredObjectVersion::class, cascade: ['persist'], mappedBy: 'storedObject')]
#[ORM\OneToMany(targetEntity: StoredObjectVersion::class, cascade: ['persist'], mappedBy: 'storedObject', orphanRemoval: true)]
private Collection $versions;
/**
@@ -333,6 +333,15 @@ class StoredObject implements Document, TrackCreationInterface
return $version;
}
public function removeVersion(StoredObjectVersion $storedObjectVersion): void
{
if (!$this->versions->contains($storedObjectVersion)) {
throw new \UnexpectedValueException('This stored object does not contains this version');
}
$this->versions->removeElement($storedObjectVersion);
$storedObjectVersion->resetStoredObject();
}
/**
* @deprecated
*/
@@ -359,4 +368,20 @@ class StoredObject implements Document, TrackCreationInterface
return uniqid(more_entropy: true);
}
}
/**
* Checks if a stored object can be deleted.
*
* Currently, return true if the deletedAt date is below the current date, and the object
* does not contains any version (which must be removed first).
*
* @param \DateTimeImmutable $now the current date and time
* @param StoredObject $storedObject the stored object to check
*
* @return bool returns true if the stored object can be deleted, false otherwise
*/
public static function canBeDeleted(\DateTimeImmutable $now, StoredObject $storedObject): bool
{
return $storedObject->getDeleteAt() < $now && $storedObject->getVersions()->isEmpty();
}
}

View File

@@ -33,6 +33,13 @@ class StoredObjectVersion implements TrackCreationInterface
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER)]
private ?int $id = null;
/**
* The stored object associated with this version.
*/
#[ORM\ManyToOne(targetEntity: StoredObject::class, inversedBy: 'versions')]
#[ORM\JoinColumn(name: 'stored_object_id', nullable: true)]
private ?StoredObject $storedObject;
/**
* filename of the version in the stored object.
*/
@@ -40,12 +47,7 @@ class StoredObjectVersion implements TrackCreationInterface
private string $filename = '';
public function __construct(
/**
* The stored object associated with this version.
*/
#[ORM\ManyToOne(targetEntity: StoredObject::class, inversedBy: 'versions')]
#[ORM\JoinColumn(name: 'stored_object_id', nullable: true)]
private StoredObject $storedObject,
StoredObject $storedObject,
/**
* The incremental version.
@@ -76,6 +78,7 @@ class StoredObjectVersion implements TrackCreationInterface
private string $type = '',
?string $filename = null,
) {
$this->storedObject = $storedObject;
$this->filename = $filename ?? self::generateFilename($this);
}
@@ -124,4 +127,12 @@ class StoredObjectVersion implements TrackCreationInterface
{
return $this->version;
}
/**
* @internal to be used by StoredObject::removeVersion
*/
public function resetStoredObject(): void
{
$this->storedObject = null;
}
}