Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Oct 2, 2023
1 parent 998168a commit 0ead7eb
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 18 deletions.
20 changes: 14 additions & 6 deletions databox/api/src/Api/InputTransformer/AttributeInputTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Api\Model\Input\Attribute\AttributeInput;
use App\Api\Model\Input\Template\TemplateAttributeInput;
use App\Api\Processor\AttributeInputProcessorInterface;
use App\Attribute\InvalidAttributeValueException;
use App\Entity\Core\Asset;
use App\Entity\Core\Attribute;
use App\Entity\Core\AttributeDefinition;
Expand Down Expand Up @@ -74,19 +75,26 @@ protected function assignAttributes(
foreach ($attribute->value as $value) {
$attr = clone $attribute;
$attr->value = $value;
/** @var Attribute|TemplateAttribute $returnedAttribute */
$returnedAttribute = $attributeInputProcessor->transform($attr, Attribute::class, $subContext);
$object->addAttribute($returnedAttribute);

try {
/** @var Attribute|TemplateAttribute $returnedAttribute */
$returnedAttribute = $attributeInputProcessor->transform($attr, Attribute::class, $subContext);
$object->addAttribute($returnedAttribute);
} catch (InvalidAttributeValueException) {
}
}

continue;
}
// else add single attr below
}

/** @var Attribute|TemplateAttribute $returnedAttribute */
$returnedAttribute = $attributeInputProcessor->transform($attribute, Attribute::class, $subContext);
$object->addAttribute($returnedAttribute);
try {
/** @var Attribute|TemplateAttribute $returnedAttribute */
$returnedAttribute = $attributeInputProcessor->transform($attribute, Attribute::class, $subContext);
$object->addAttribute($returnedAttribute);
} catch (InvalidAttributeValueException) {
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Api\Model\Input\Attribute\AttributeInput;
use App\Attribute\AttributeAssigner;
use App\Attribute\InvalidAttributeValueException;
use App\Entity\Core\Asset;
use App\Entity\Core\Attribute;
use App\Entity\Core\AttributeDefinition;
Expand Down Expand Up @@ -72,8 +73,9 @@ public function resolveInitialAttributes(Asset $asset): array
$attribute->setUpdatedAt($now);
$attribute->setAsset($asset);

$this->attributeAssigner->assignAttributeFromInput($attribute, $input);
if (null === $attribute->getValue()) {
try {
$this->attributeAssigner->assignAttributeFromInput($attribute, $input);
} catch (InvalidAttributeValueException) {
// this can happen for e.g. if a date is invalid and cannot be normalized
continue;
}
Expand Down
10 changes: 6 additions & 4 deletions databox/api/src/Attribute/AttributeAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(private AttributeTypeRegistry $attributeTypeRegistry
{
}

public function assignAttributeFromInput(AbstractBaseAttribute $attribute, AbstractBaseAttributeInput $data): AbstractBaseAttribute
public function assignAttributeFromInput(AbstractBaseAttribute $attribute, AbstractBaseAttributeInput $data): void
{
if ($data instanceof AbstractExtendedAttributeInput) {
assert($attribute instanceof Attribute);
Expand Down Expand Up @@ -53,9 +53,11 @@ public function assignAttributeFromInput(AbstractBaseAttribute $attribute, Abstr
$type = $this->attributeTypeRegistry->getStrictType($attribute->getDefinition()->getFieldType());
$value = $type->normalizeValue($data->value);

$attribute->setValue($value ?? '');
$attribute->setPosition($data->position ?? 0);
if (null === $value) {
throw new InvalidAttributeValueException(sprintf('Normalized "%s" value is NULL (from: "%s")', $type::getName(), get_debug_type($data->value)));
}

return $attribute;
$attribute->setValue($value);
$attribute->setPosition($data->position ?? 0);
}
}
9 changes: 6 additions & 3 deletions databox/api/src/Attribute/BatchAttributeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,12 @@ private function upsertAttribute(
$attribute->setDefinition($definition);
}

$this->attributeAssigner->assignAttributeFromInput($attribute, $action);

$this->em->persist($attribute);
try {
$this->attributeAssigner->assignAttributeFromInput($attribute, $action);
$this->em->persist($attribute);
} catch (InvalidAttributeValueException) {
// Ignore invalid values
}

$attribute = null;
}
Expand Down
9 changes: 9 additions & 0 deletions databox/api/src/Attribute/InvalidAttributeValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Attribute;

class InvalidAttributeValueException extends \InvalidArgumentException
{
}
10 changes: 7 additions & 3 deletions databox/api/src/Attribute/Type/DateAttributeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getGroupValueLabel($value): ?string
return parent::getGroupValueLabel($value);
}

public function denormalizeValue(?string $value): ?\DateTimeImmutable
public function denormalizeValue(?string $value): ?string
{
if (null === $value) {
return null;
Expand All @@ -31,10 +31,14 @@ public function denormalizeValue(?string $value): ?\DateTimeImmutable
try {
$date = \DateTimeImmutable::createFromFormat('Y-m-d', $value);
if (false === $date) {
return parent::denormalizeValue($value);
$date = parent::denormalizeValue($value);
}

return $date;
if ($date instanceof \DateTimeInterface) {
return $date->format('Y-m-d');
}

return null;
} catch (\Throwable) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,15 @@ public function enqueueAckAction(

return new Response();
}

#[Route(path: '/{integrationId}/assets/{assetId}/ack', name: 'enqueue_asset_ack', methods: ['POST'])]
public function enqueueAssetAckAction(
string $integrationId,
string $assetId,
LoggerInterface $logger
): Response {
$logger->debug(sprintf('Phraseanet enqueue acknowledgement received for asset ID "%s"', $assetId));

return new Response();
}
}

0 comments on commit 0ead7eb

Please sign in to comment.