Skip to content

Commit

Permalink
WIP: showcase patch method
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Mar 13, 2024
1 parent ba4dbab commit fd87cc2
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/api_platform/resources/asset/image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resources:
ApiPlatform\Metadata\Get:
normalizationContext:
groups: ['image:read', 'asset:read', 'asset:item:get', 'dependency:read', 'property:read', 'element:read', 'element:item:get' ]
ApiPlatform\Metadata\Put:
ApiPlatform\Metadata\Patch:
properties:
id:
identifier: true
Expand Down
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
Pimcore\Bundle\StudioApiBundle\State\VersionProvider: ~

# Processors

Pimcore\Bundle\StudioApiBundle\State\Asset\Processor: ~
Pimcore\Bundle\StudioApiBundle\State\ResetPasswordProcessor: ~
Pimcore\Bundle\StudioApiBundle\State\Token\Create\Processor: ~
Pimcore\Bundle\StudioApiBundle\State\Token\Refresh\Processor: ~
Expand Down
9 changes: 7 additions & 2 deletions src/Dto/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
private readonly string $iconName,
private readonly bool $hasChildren,
private readonly string $type,
private readonly string $filename,
private string $filename,
private readonly ?string $mimeType,
private readonly array $metaData,
private readonly bool $workflowWithPermissions,
Expand Down Expand Up @@ -70,11 +70,16 @@ public function hasWorkflowWithPermissions(): bool
return $this->workflowWithPermissions;
}

public function getFilename(): ?string
public function getFilename(): string
{
return $this->filename;
}

public function setFilename(string $filename): void
{
$this->filename = $filename;
}

public function getType(): string
{
return $this->type;
Expand Down
7 changes: 6 additions & 1 deletion src/Dto/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Element
{
public function __construct(
private readonly int $id,
private readonly int $parentId,
private int $parentId,
private readonly string $path,
private readonly int $userOwner,
private readonly int $userModification,
Expand All @@ -45,6 +45,11 @@ public function getParentId(): int
return $this->parentId;
}

public function setParentId(int $parentId): void
{
$this->parentId = $parentId;
}

public function getPath(): string
{
return $this->path;
Expand Down
46 changes: 42 additions & 4 deletions src/Service/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,63 @@

namespace Pimcore\Bundle\StudioApiBundle\Service;

use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\SynchronousProcessingServiceInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\Asset\AssetResolverInterface;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Model\Asset as CoreAsset;
use Pimcore\Model\Element\DuplicateFullPathException;
use Pimcore\Model\Exception\NotFoundException;
use Pimcore\Model\Asset as CoreAsset;

final readonly class AssetService implements AssetServiceInterface
{
public function __construct(private AssetResolverInterface $assetResolver)
public function __construct(
private AssetResolverInterface $assetResolver,
private SynchronousProcessingServiceInterface $synchronousProcessingService
)
{
}

public function processAsset(int $id, Asset $data): CoreAsset
/**
* @throws DuplicateFullPathException
*/
public function processAsset(Asset $data): CoreAsset
{
$asset = $this->assetResolver->getById($id);
$asset = $this->assetResolver->getById($data->getId());

if(!$asset) {
throw new NotFoundException('Asset not found');
}

$differences = $this->compare($asset, $data);
foreach($differences as $field => $difference) {
$setter = 'set' . $field;
$asset->$setter($difference['new']);
}
$this->synchronousProcessingService->enable();
$asset->save();
return $asset;
}


private function compare(CoreAsset $current, Asset $data): array
{
$fieldsToCheck = $this->fieldsToCheck(get_class_methods($data));
$differences = [];
foreach($fieldsToCheck as $field) {
if($current->$field() !== $data->$field()) {
$fieldName = str_replace('get', '', $field);
$differences[$fieldName] = [
'current' => $current->$field(),
'new' => $data->$field()
];
}
}
return $differences;
}

private function fieldsToCheck(array $methods): array
{
$setters = array_filter($methods, static fn ($method) => str_starts_with($method, 'set'));
return array_map(static fn ($setter) => str_replace('set', 'get', $setter), $setters);
}
}
2 changes: 1 addition & 1 deletion src/Service/AssetServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@

interface AssetServiceInterface
{
public function processAsset(int $id, Asset $data): CoreAsset;
public function processAsset(Asset $data): CoreAsset;
}
2 changes: 1 addition & 1 deletion src/Service/CoreData/V1/Hydrator/AssetHydratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Pimcore\Model\Asset as CoreAsset;
use Symfony\Contracts\Service\ServiceProviderInterface;

final readonly class AssetHydratorService implements AssetHydratorInterface
final readonly class AssetHydratorService implements AssetHydratorServiceInterface
{
public function __construct(
private ServiceProviderInterface $assetHydratorLocator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Model\Asset as CoreAsset;

interface AssetHydratorInterface
interface AssetHydratorServiceInterface
{
/**
* @param CoreAsset $item
Expand Down
17 changes: 14 additions & 3 deletions src/State/Asset/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,34 @@

use ApiPlatform\Exception\OperationNotFoundException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\State\ProcessorInterface;
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Service\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\CoreData\V1\Hydrator\AssetHydratorServiceInterface;

final readonly class Processor implements ProcessorInterface
{

public function __construct(
private AssetServiceInterface $assetService,
private AssetHydratorServiceInterface $assetHydratorService
)
{
}

public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Asset
{
if (
!$operation instanceof Put ||
!$operation instanceof Patch ||
!$data instanceof Asset
) {
// wrong operation
throw new OperationNotFoundException();
}
$asset = $this->assetService->processAsset($data);


return $this->assetHydratorService->hydrate($asset);
}
}

0 comments on commit fd87cc2

Please sign in to comment.