Skip to content

Commit

Permalink
Update API output shape of resource versions
Browse files Browse the repository at this point in the history
  • Loading branch information
subiabre committed Nov 26, 2024
1 parent e7f157a commit 56505f1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/ApiResource/EmbeddedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class EmbeddedResource

/**
* Actual object data of the embedded resource.
*
*
* @var array<string, mixed>
*/
public object $resource;
public mixed $resource;
}
60 changes: 13 additions & 47 deletions src/ApiResource/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
use ApiPlatform\Metadata as API;
use App\Filter\ResourceVersionResourceFilter;
use App\Filter\ResourceVersionResourceIdFilter;
use App\Service\ApiService;
use App\State\ResourceVersionStateProvider;
use Gedmo\Loggable\Entity\LogEntry;

/**
* Some resources are versioned. This means v4 keeps track of the changes performed in subsets of specific properties within these resources.\
Expand All @@ -21,64 +19,32 @@
#[API\Get(provider: ResourceVersionStateProvider::class)]
class Version
{
public function __construct(
private readonly LogEntry $log,
private readonly object $entity,
) {}
public int $id;

/**
* The ID of the version record.
* Version number for the resource object.
*/
public function getId(): ?int
{
return $this->log->getId();
}
public int $version;

/**
* The ID of the version for this specific resource.
* The action-type that performed the version change.
*/
public function getVersion(): ?int
{
return $this->log->getVersion();
}
public string $action;

/**
* The type of action that performed the recorded changes.
* The changes made by the action for this version.
*
* @return array<string, mixed>
*/
public function getAction(): ?string
{
return $this->log->getAction();
}
public array $changes;

/**
* The type of the recorded resource.
* Reconstructed resource data for this version.
*/
public function getResource(): string
{
return ApiService::toResource($this->log->getObjectClass());
}
public EmbeddedResource $resource;

/**
* The ID of the recorded resource.
* Version creation timestamp.
*/
public function getResourceId(): int
{
return $this->log->getObjectId();
}

/**
* The changed resource data, i.e the new values of the changed properties.
*/
public function getResourceChanges()
{
return $this->log->getData();
}

/**
* The date at which this version was created.
*/
public function getDateCreated(): ?\DateTimeInterface
{
return $this->log->getLoggedAt();
}
public \DateTimeInterface $dateCreated;
}
41 changes: 36 additions & 5 deletions src/State/ResourceVersionStateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace App\State;

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Metadata as API;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\EmbeddedResource;
use App\ApiResource\Version;
use App\Service\ApiResourceNormalizer;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Mapping\MappingException;
use Gedmo\Loggable\Entity\LogEntry;
Expand All @@ -19,6 +20,7 @@ class ResourceVersionStateProvider implements ProviderInterface

public function __construct(
private EntityManagerInterface $entityManager,
private IriConverterInterface $iriConverter,
) {
$this->versionRepository = $this->entityManager->getRepository(LogEntry::class);
}
Expand Down Expand Up @@ -49,15 +51,15 @@ private function getVersion(int $id): Version

$entity = $this->entityManager->find($log->getObjectClass(), $log->getObjectId());

return new Version($log, $entity);
return new Version($log, $entity, $this->iriConverter->getIriFromResource($entity));
}

/**
* @return Version[]
*/
private function getVersions(string $resourceName, int $resourceId): array
{
$resourceClass = ApiResourceNormalizer::toEntity($resourceName);
$resourceClass = \sprintf('App\\Entity\\%s', ucfirst($resourceName));

try {
$entity = $this->entityManager->find($resourceClass, $resourceId);
Expand All @@ -72,10 +74,39 @@ private function getVersions(string $resourceName, int $resourceId): array
$logs = $this->versionRepository->getLogEntries($entity);

$versions = [];
foreach ($logs as $log) {
$versions[] = new Version($log, $entity);
foreach ($logs as $key => $log) {
$resource = new EmbeddedResource();
$resource->id = $entity->getId();
$resource->iri = $this->iriConverter->getIriFromResource($entity);
$resource->resource = $this->reconstructEntity($entity, \array_slice($logs, 0, $key));

$version = new Version();
$version->id = $log->getId();
$version->version = $log->getVersion();
$version->action = $log->getAction();
$version->changes = $log->getData();
$version->resource = $resource;
$version->dateCreated = $log->getLoggedAt();

$versions[] = $version;
}

return $versions;
}

/**
* @param LogEntry[] $logs
*/
private function reconstructEntity(object $entity, array $logs): object
{
foreach ($logs as $log) {
$data = $log->getData();
foreach ($data as $property => $value) {
$setter = \sprintf('set%s', \ucfirst($property));
$entity->$setter($value);
}
}

return $entity;
}
}

0 comments on commit 56505f1

Please sign in to comment.