Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Versioned Resources API #34

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"phpstan/phpdoc-parser": "^1.24",
"stof/doctrine-extensions-bundle": "^1.11",
"stripe/stripe-php": "^15.0",
"subiabre/doctrine-snowflakes": "^2.0",
"symfony/asset": "6.4.*",
"symfony/cache": "6.4.*",
"symfony/console": "6.4.*",
Expand Down
44 changes: 0 additions & 44 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions src/ApiResource/EmbeddedResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\ApiResource;

class EmbeddedResource
{
/**
* ID of the embedded resource.
*/
public int $id;

/**
* Path to the embedded resource.
*/
public string $iri;

/**
* Actual object data of the embedded resource.
*
* @var array<string, mixed>
*/
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;
}
}
Loading