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

IBX-5936: Added ContentTypeHandler::loadContentTypesByFieldDefinitionIdentifier method #249

Merged
merged 4 commits into from
Aug 8, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ tables:
ezcontentclass_attribute:
indexes:
ezcontentclass_attr_ccid: { fields: [contentclass_id] }
ezcontentclass_attr_dts: { fields: [data_type_string] }
id:
id: { type: integer, nullable: false, options: { autoincrement: true } }
version: { type: integer, nullable: false, options: { default: '0' } }
Expand Down
5 changes: 5 additions & 0 deletions src/contracts/Persistence/Content/Type/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public function loadContentTypes($groupId, $status = Type::STATUS_DEFINED);
*/
public function loadContentTypeList(array $contentTypeIds): array;

/**
* @return \Ibexa\Contracts\Core\Persistence\Content\Type[]
*/
public function loadContentTypesByFieldDefinitionIdentifier(string $identifier): array;

/**
* Loads a content type by id and status.
*
Expand Down
15 changes: 15 additions & 0 deletions src/lib/Persistence/Cache/ContentTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ContentTypeHandler extends AbstractInMemoryPersistenceHandler implements C
private const BY_IDENTIFIER_SUFFIX = 'by_identifier_suffix';
private const CONTENT_TYPE_LIST_BY_GROUP_IDENTIFIER = 'content_type_list_by_group';
private const BY_REMOTE_SUFFIX = 'by_remote_suffix';
private const CONTENT_TYPE_LIST_BY_FIELD_DEFINITION_IDENTIFIER = 'content_type_list_by_field_definition_identifier';
private const TYPE_MAP_IDENTIFIER = 'type_map';
private const CONTENT_FIELDS_TYPE_IDENTIFIER = 'content_fields_type';
private const TYPE_WITHOUT_VALUE_IDENTIFIER = 'type_without_value';
Expand Down Expand Up @@ -303,6 +304,20 @@ function () use ($remoteId) {
);
}

public function loadContentTypesByFieldDefinitionIdentifier(string $identifier): array
{
return $this->getListCacheValue(
$this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_LIST_BY_FIELD_DEFINITION_IDENTIFIER, [$identifier], true),
function () use ($identifier): array {
return $this->persistenceHandler->contentTypeHandler()->loadContentTypesByFieldDefinitionIdentifier($identifier);
},
$this->getTypeTags,
$this->getTypeKeys,
null,
[$identifier]
);
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 5 additions & 0 deletions src/lib/Persistence/Legacy/Content/Type/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ abstract public function updateType(int $typeId, int $status, Type $type): void;
*/
abstract public function loadTypesListData(array $typeIds): array;

/**
* @return array<mixed>
*/
abstract public function loadTypesDataByFieldDefinitionIdentifier(string $identifier): array;
konradoboza marked this conversation as resolved.
Show resolved Hide resolved

abstract public function loadTypeData(int $typeId, int $status): array;

abstract public function loadTypeDataByIdentifier(string $identifier, int $status): array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,20 @@ public function loadTypesListData(array $typeIds): array
return $query->execute()->fetchAll();
}

public function loadTypesDataByFieldDefinitionIdentifier(string $identifier): array
{
$query = $this->getLoadTypeQueryBuilder();
$query
->andWhere(
$query->expr()->eq(
'a.data_type_string',
konradoboza marked this conversation as resolved.
Show resolved Hide resolved
$query->createNamedParameter($identifier)
)
);

return $query->execute()->fetchAllAssociative();
}

public function loadTypeData(int $typeId, int $status): array
{
$query = $this->getLoadTypeQueryBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ public function loadTypeDataByRemoteId(string $remoteId, int $status): array
}
}

public function loadTypesDataByFieldDefinitionIdentifier(string $identifier): array
{
try {
return $this->innerGateway->loadTypesDataByFieldDefinitionIdentifier($identifier);
} catch (PDOException $e) {
throw DatabaseException::wrap($e);
}
}

public function countInstancesOfType(int $typeId): int
{
try {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/Persistence/Legacy/Content/Type/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ public function loadContentTypeList(array $contentTypeIds): array
);
}

/**
* @return \Ibexa\Contracts\Core\Persistence\Content\Type[]
*/
public function loadContentTypesByFieldDefinitionIdentifier(string $identifier): array
{
return $this->mapper->extractTypesFromRows(
$this->contentTypeGateway->loadTypesDataByFieldDefinitionIdentifier($identifier)
);
}

/**
* Loads a content type by id and status.
*
Expand Down
16 changes: 16 additions & 0 deletions src/lib/Persistence/Legacy/Content/Type/MemoryCachingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ public function loadContentTypeList(array $contentTypeIds): array
return $contentTypes;
}

/**
* {@inheritdoc}
*/
public function loadContentTypesByFieldDefinitionIdentifier(string $identifier): array
{
$cacheKey = 'ibx-ctlbfdi-' . $identifier;
alongosz marked this conversation as resolved.
Show resolved Hide resolved

$types = $this->cache->get($cacheKey);
if ($types === null) {
$types = $this->innerHandler->loadContentTypesByFieldDefinitionIdentifier($identifier);
$this->storeTypeCache($types, $cacheKey);
}

return $types;
}

/**
* {@inheritdoc}
*/
Expand Down
1 change: 1 addition & 0 deletions src/lib/Resources/settings/storage_engines/cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ parameters:
content_type_group: 'ctg-%s'
content_type_group_with_id_suffix: 'ctg-%s-bi'
content_type_group_with_by_remote_suffix: 'ctg-%s-br'
content_type_list_by_field_definition_identifier: 'ctlbfdi-%s'
content_type_group_list: 'ctgl-%s'
content_type_list_by_group: 'ctlbg-%s'
image_variation: 'ig'
Expand Down
1 change: 1 addition & 0 deletions tests/lib/Persistence/Cache/ContentTypeHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public function providerForCachedLoadMethodsHit(): array
],
['loadAllGroups', [], 'ibx-ctgl', null, null, [['content_type_group_list', [], true]], ['ibx-ctgl'], [3 => $group]],
['loadContentTypes', [3, 0], 'ibx-ctlbg-3', null, null, [['content_type_list_by_group', [3], true]], ['ibx-ctlbg-3'], [$type]],
['loadContentTypesByFieldDefinitionIdentifier', [3, 0], 'ibx-ctlbfdi-3', null, null, [['content_type_list_by_field_definition_identifier', [3], true]], ['ibx-ctlbfdi-3'], [$type]],
['loadContentTypeList', [[5]], 'ibx-ct-5', null, null, [['content_type', [], true]], ['ibx-ct'], [5 => $type], true],
['load', [5, 0], 'ibx-ct-5', null, null, [['content_type', [], true]], ['ibx-ct'], $type],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,30 @@ public function testLoadContentTypeList(): void
);
}

public function testLoadContentTypesByFieldDefinitionIdentifier(): void
{
$gatewayMock = $this->getGatewayMock();
$gatewayMock->expects($this->once())
->method('loadTypesDataByFieldDefinitionIdentifier')
->with('ezstring')
->willReturn([]);

$mapperMock = $this->getMapperMock();
$mapperMock->expects(self::once())
->method('extractTypesFromRows')
->with([])
->willReturn([23 => new Type()]);

$handler = $this->getHandler();
$types = $handler->loadContentTypesByFieldDefinitionIdentifier('ezstring');

self::assertEquals(
[23 => new Type()],
$types,
'Types not loaded correctly'
);
}

public function testLoad()
{
$gatewayMock = $this->getGatewayMock();
Expand Down