Skip to content

Commit

Permalink
Add asset setter
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Mar 11, 2024
1 parent ad901ef commit 82e3379
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
16 changes: 8 additions & 8 deletions src/Dto/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@

class Asset extends Element
{
private ?string $iconName;
private ?string $iconName = null;

private ?bool $hasChildren;
private ?bool $hasChildren = null;

private ?string $type;
private ?string $type = null;

private ?string $filename = null;

private ?string $mimeType;
private ?string $mimeType = null;

#[ApiProperty(genId: false)]
private ?array $metaData;
private ?array $metaData = null;

private ?bool $workflowWithPermissions;
private ?bool $workflowWithPermissions = null;

private ?string $fullPath;
private ?string $fullPath = null;

public function getIconName(): ?string
{
Expand Down Expand Up @@ -94,7 +94,7 @@ public function getMetaData(): ?array

public function getHasMetaData(): bool
{
return count($this->metaData) > 0;
return $this->metaData && count($this->metaData) > 0;

Check failure on line 97 in src/Dto/Asset.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Comparison operation ">" between int<1, max> and 0 is always true.
}

public function setMetaData(?array $metaData): void
Expand Down
20 changes: 10 additions & 10 deletions src/Dto/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ class Element
{
private ?int $parentId = null;

private ?string $path;
private ?string $path = null;

private ?int $userOwner;
private ?int $userOwner = null;

private ?int $userModification;
private ?int $userModification = null;

private ?string $locked;
private ?string $locked = null;

private ?bool $isLocked;
private ?bool $isLocked = null;

private ?int $creationDate;
private ?int $creationDate = null;

private ?int $modificationDate;
private ?int $modificationDate = null;

private ?Permissions $permissions;
private ?Permissions $permissions = null;

public function __construct(
private readonly int $id,
Expand Down Expand Up @@ -133,12 +133,12 @@ public function setModificationDate(?int $modificationDate): void
}

#[ApiProperty(genId: false)]
public function getPermissions(): Permissions
public function getPermissions(): ?Permissions
{
return $this->permissions;
}

public function setPermissions(Permissions $permissions): void
public function setPermissions(?Permissions $permissions): void
{
$this->permissions = $permissions;
}
Expand Down
22 changes: 19 additions & 3 deletions src/Service/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,25 @@ public function handleAsset(int $id, Asset $data): \Pimcore\Model\Asset
*/
private function setAssetData(\Pimcore\Model\Asset $asset, Asset $data): \Pimcore\Model\Asset
{
$asset->setFilename($data->getFilename() ?: $asset->getFilename());
$asset->save();

$getters = array_filter(get_class_methods($data), static fn($method) => str_starts_with($method, 'get'));
foreach($getters as $getter) {
$property = lcfirst(substr($getter, 3));
$value = $data->$getter();
if($value === null) {
continue;
}
$this->propertySetter($asset, $property, $value);
}
return $asset;
}



private function propertySetter(\Pimcore\Model\Asset $asset, string $property, mixed $value): void
{
$setter = 'set' . ucfirst($property);
if (method_exists($asset, $setter)) {
$asset->$setter($value);
}
}
}
6 changes: 6 additions & 0 deletions src/State/Asset/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Pimcore\Bundle\StudioApiBundle\Dto\Asset;
use Pimcore\Bundle\StudioApiBundle\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioApiBundle\Service\ModelData\V1\Hydrator\AssetHydratorServiceInterface;
use Pimcore\Model\Element\DuplicateFullPathException;

final readonly class Processor implements ProcessorInterface
{
Expand All @@ -32,6 +33,9 @@ public function __construct(
) {
}

/**
* @throws DuplicateFullPathException
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Asset
{
if (
Expand All @@ -44,6 +48,8 @@ public function process(mixed $data, Operation $operation, array $uriVariables =

$asset = $this->assetService->handleAsset($data->getId(), $data);

$asset->save();

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

0 comments on commit 82e3379

Please sign in to comment.