Skip to content

Commit

Permalink
[Improvement]: Ignore undefined data object adapters for now (#583)
Browse files Browse the repository at this point in the history
* ignore undefined adapters for now as not field types from all bundles are implemented yet

* fix: sonar

* fix: sonar

* Apply php-cs-fixer changes

* fix: rather skip setter than use null value for it

* Apply php-cs-fixer changes

* fix: remove unused data service

---------

Co-authored-by: lukmzig <[email protected]>
  • Loading branch information
lukmzig and lukmzig authored Nov 28, 2024
1 parent d05a65e commit 9ce4fdc
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 36 deletions.
37 changes: 32 additions & 5 deletions src/DataObject/Data/Adapter/BlockAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidDataTypeException;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Block;
Expand All @@ -38,6 +39,8 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class BlockAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService
Expand Down Expand Up @@ -148,13 +151,24 @@ private function processBlockElement(
$fieldContextData = $this->createFieldContextData($element, $fieldDefinition, $contextData);

foreach ($fieldDefinitions as $elementName => $fd) {
$resultElement[$elementName] = $this->createBlockElement(
$adapter = $this->dataAdapterService->tryDataAdapter($fd->getFieldType());
if (!$adapter) {
continue;
}

$value = $this->createBlockElement(
$adapter,
$element,
$fd,
$elementName,
$blockElement,
$fieldContextData
);
if (!$value) {
continue;
}

$resultElement[$elementName] = $value;
}

return $resultElement;
Expand All @@ -164,24 +178,37 @@ private function processBlockElement(
* @throws Exception
*/
private function createBlockElement(
SetterDataInterface $adapter,
Concrete $element,
Data $fieldDefinition,
string $elementName,
?array $blockElement,
?FieldContextData $fieldContextData = null
): BlockElement {
): ?BlockElement {
$elementType = $fieldDefinition->getFieldtype();
$elementData = $blockElement[$elementName] ?? null;

$adapter = $this->dataAdapterService->getDataAdapter($elementType);
$blockData = $adapter->getDataForSetter(
$data = $adapter->getDataForSetter(
$element,
$fieldDefinition,
$elementName,
[$elementName => $elementData],
$fieldContextData
);
if (!$this->validateEncryptedField($fieldDefinition, $data)) {
return null;
}

return new BlockElement($elementName, $elementType, $blockData);
return new BlockElement(
$elementName,
$elementType,
$adapter->getDataForSetter(
$element,
$fieldDefinition,
$elementName,
[$elementName => $elementData],
$fieldContextData
)
);
}
}
12 changes: 11 additions & 1 deletion src/DataObject/Data/Adapter/ClassificationStoreAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Classificationstore as ClassificationstoreDefinition;
Expand All @@ -47,6 +48,8 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class ClassificationStoreAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DefinitionCacheResolverInterface $definitionCacheResolver,
private DataAdapterServiceInterface $dataAdapterService,
Expand Down Expand Up @@ -182,13 +185,20 @@ private function processGroupKeys(
continue;
}

$adapter = $this->dataAdapterService->getDataAdapter($fieldDefinition->getFieldType());
$adapter = $this->dataAdapterService->tryDataAdapter($fieldDefinition->getFieldType());
if ($adapter === null) {
continue;
}

$setterData = $adapter->getDataForSetter(
$element,
$fieldDefinition,
$fieldDefinition->getName(),
[$fieldDefinition->getName() => $value]
);
if (!$this->validateEncryptedField($fieldDefinition, $setterData)) {
continue;
}

$container->setLocalizedKeyValue($groupId, $keyId, $setterData, $language);
}
Expand Down
33 changes: 29 additions & 4 deletions src/DataObject/Data/Adapter/EncryptedFieldAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\EncryptedField as EncryptedFieldDefinition;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Data\EncryptedField;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
Expand All @@ -43,17 +44,41 @@ public function getDataForSetter(
array $data,
?FieldContextData $contextData = null
): ?EncryptedField {
if (!($fieldDefinition instanceof Data\EncryptedField)) {
if (!$fieldDefinition instanceof EncryptedFieldDefinition) {
return null;
}

$delegateFieldDefinition = $fieldDefinition->getDelegateDatatypeDefinition();
if (!$delegateFieldDefinition) {
return null;
}
$adapter = $this->dataAdapterService->getDataAdapter($delegateFieldDefinition->getFieldType());
$result = $adapter->getDataForSetter($element, $delegateFieldDefinition, $key, $data);

return new EncryptedField($fieldDefinition->getDelegate(), $result);
return $this->handleDelegatedField(
$element,
$delegateFieldDefinition,
$fieldDefinition,
$key,
$data,
$contextData
);
}

private function handleDelegatedField(
Concrete $element,
Data $delegateFieldDefinition,
EncryptedFieldDefinition $fieldDefinition,
string $key,
array $data,
?FieldContextData $contextData = null
): ?EncryptedField {
$adapter = $this->dataAdapterService->tryDataAdapter($fieldDefinition->getFieldType());
if ($adapter instanceof SetterDataInterface) {
return new EncryptedField(
$fieldDefinition->getDelegate(),
$adapter->getDataForSetter($element, $delegateFieldDefinition, $key, $data, $contextData)
);
}

return null;
}
}
14 changes: 11 additions & 3 deletions src/DataObject/Data/Adapter/FieldCollectionsAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Fieldcollections;
use Pimcore\Model\DataObject\Concrete;
Expand All @@ -38,6 +39,8 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class FieldCollectionsAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService,
Expand Down Expand Up @@ -146,18 +149,23 @@ private function processCollectionRaw(

foreach ($collectionDef?->getFieldDefinitions() as $elementName => $fd) {
$elementValue = $blockElement[$elementName] ?? null;
if (!$elementValue) {
$adapter = $this->dataAdapterService->tryDataAdapter($fd->getFieldType());
if (!$elementValue || !$adapter) {
continue;
}

$adapter = $this->dataAdapterService->getDataAdapter($fd->getFieldType());
$collectionData[$elementName] = $adapter->getDataForSetter(
$value = $adapter->getDataForSetter(
$element,
$fd,
$elementName,
[$elementName => $elementValue],
$fieldContextData
);
if (!$this->validateEncryptedField($fieldDefinition, $value)) {
continue;
}

$collectionData[$elementName] = $value;
}

return $collectionData;
Expand Down
28 changes: 18 additions & 10 deletions src/DataObject/Data/Adapter/LocalizedFieldsAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
Expand All @@ -43,6 +44,8 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class LocalizedFieldsAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService,
Expand Down Expand Up @@ -75,18 +78,23 @@ public function getDataForSetter(
continue;
}

$adapter = $this->dataAdapterService->getDataAdapter($childFieldDefinition->getFieldType());
$localizedField->setLocalizedValue(
$adapter = $this->dataAdapterService->tryDataAdapter($childFieldDefinition->getFieldType());
if (!$adapter) {
continue;
}

$value = $adapter->getDataForSetter(
$element,
$childFieldDefinition,
$name,
$adapter->getDataForSetter(
$element,
$childFieldDefinition,
$name,
[$name => $fieldData],
new FieldContextData(language: $language)
),
$language
[$name => $fieldData],
new FieldContextData(language: $language)
);
if (!$this->validateEncryptedField($childFieldDefinition, $value)) {
continue;
}

$localizedField->setLocalizedValue($name, $value, $language);
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/DataObject/Data/Adapter/ObjectBricksAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateFieldTypeTrait;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\ClassDefinition\Data\Objectbricks;
use Pimcore\Model\DataObject\Concrete;
Expand All @@ -39,6 +40,8 @@
#[AutoconfigureTag(DataAdapterLoaderInterface::ADAPTER_TAG)]
final readonly class ObjectBricksAdapter implements SetterDataInterface, DataNormalizerInterface
{
use ValidateFieldTypeTrait;

public function __construct(
private DataAdapterServiceInterface $dataAdapterService,
private DataServiceInterface $dataService,
Expand Down Expand Up @@ -177,19 +180,24 @@ private function getCollectionData(
): array {
$collectionData = [];
foreach ($collectionDef->getFieldDefinitions() as $fd) {
$adapter = $this->dataAdapterService->tryDataAdapter($fd->getFieldType());
$fieldName = $fd->getName();
if (!array_key_exists($fieldName, $rawData)) {
if (!array_key_exists($fieldName, $rawData) || !$adapter) {
continue;
}

$adapter = $this->dataAdapterService->getDataAdapter($fd->getFieldType());
$collectionData[$fd->getName()] = $adapter->getDataForSetter(
$value = $adapter->getDataForSetter(
$element,
$fd,
$fieldName,
[$fieldName => $rawData[$fieldName]],
new FieldContextData($brick)
);
if (!$this->validateEncryptedField($fd, $value)) {
continue;
}

$collectionData[$fieldName] = $value;
}

return $collectionData;
Expand Down
13 changes: 13 additions & 0 deletions src/DataObject/Service/DataAdapterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,23 @@ public function getFieldDefinitionAdapterClass(string $fieldDefinitionType): str
);
}

/**
* @throws InvalidArgumentException
*/
public function getDataAdapter(string $fieldDefinitionType): SetterDataInterface
{
return $this->dataAdapterLoader->loadAdapter(
$this->getFieldDefinitionAdapterClass($fieldDefinitionType)
);
}

// ToDo: Consider removing this method and using getDataAdapter when field types from bundles are implemented
public function tryDataAdapter(string $fieldDefinitionType): ?SetterDataInterface
{
try {
return $this->getDataAdapter($fieldDefinitionType);
} catch (InvalidArgumentException) {
return null;
}
}
}
5 changes: 5 additions & 0 deletions src/DataObject/Service/DataAdapterServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ public function getAdaptersMapping(): array;
*/
public function getFieldDefinitionAdapterClass(string $fieldDefinitionType): string;

/**
* @throws InvalidArgumentException
*/
public function getDataAdapter(string $fieldDefinitionType): SetterDataInterface;

public function tryDataAdapter(string $fieldDefinitionType): ?SetterDataInterface;
}
6 changes: 4 additions & 2 deletions src/DataObject/Service/DataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ public function getObjectData(Concrete $dataObject): array

foreach ($fieldDefinitions as $key => $fieldDefinition) {
try {
$data[$key] = $this->getNormalizedValue($dataObject->get($key), $fieldDefinition);
$value = $dataObject->get($key);
} catch (Exception) {
throw new NotFoundException(type: 'field', id: $key);
}

$data[$key] = $this->getNormalizedValue($value, $fieldDefinition);
}

return $data;
Expand Down Expand Up @@ -85,7 +87,7 @@ public function getNormalizedValue(
return null;
}

$adapter = $this->dataAdapterService->getDataAdapter($fieldDefinition->getFieldType());
$adapter = $this->dataAdapterService->tryDataAdapter($fieldDefinition->getFieldType());
if ($adapter instanceof DataNormalizerInterface) {
return $adapter->normalize($value, $fieldDefinition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Service\Loader;

use InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Data\SetterDataInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\DataAdapterLoaderInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use function get_class;
use function sprintf;
Expand Down
Loading

0 comments on commit 9ce4fdc

Please sign in to comment.