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

EZP-31118: Introduced strict types for ContentService #2866

Merged
merged 3 commits into from
Mar 16, 2020
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 @@ -49,9 +49,7 @@ protected function setUp(): void
public function testGetVariationOfImageAsset()
{
$assetField = new Field([
'value' => new ImageAsset\Value([
'destinationContentId' => 486,
]),
'value' => new ImageAsset\Value(486),
]);
$imageField = new Field([
'value' => new Image\Value([
Expand Down
94 changes: 47 additions & 47 deletions eZ/Publish/API/Repository/ContentService.php

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions eZ/Publish/Core/Event/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function updateContentMetadata(
return $content;
}

public function deleteContent(ContentInfo $contentInfo): array
public function deleteContent(ContentInfo $contentInfo): iterable
{
$eventData = [$contentInfo];

Expand All @@ -140,8 +140,8 @@ public function deleteContent(ContentInfo $contentInfo): array

public function createContentDraft(
ContentInfo $contentInfo,
VersionInfo $versionInfo = null,
User $creator = null,
?VersionInfo $versionInfo = null,
?User $creator = null,
?Language $language = null
): Content {
$eventData = [
Expand Down Expand Up @@ -242,7 +242,7 @@ public function deleteVersion(VersionInfo $versionInfo): void
public function copyContent(
ContentInfo $contentInfo,
LocationCreateStruct $destinationLocationCreateStruct,
VersionInfo $versionInfo = null
?VersionInfo $versionInfo = null
): Content {
$eventData = [
$contentInfo,
Expand Down Expand Up @@ -320,7 +320,7 @@ public function deleteRelation(

public function deleteTranslation(
ContentInfo $contentInfo,
$languageCode
string $languageCode
): void {
$eventData = [
$contentInfo,
Expand Down
26 changes: 20 additions & 6 deletions eZ/Publish/Core/FieldType/ImageAsset/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
return $errors;
}

$content = $this->contentService->loadContent($fieldValue->destinationContentId);
$content = $this->contentService->loadContent(
(int)$fieldValue->destinationContentId
);

if (!$this->assetMapper->isAsset($content)) {
$currentContentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);
$currentContentType = $this->contentTypeService->loadContentType(
(int)$content->contentInfo->contentTypeId
);

$errors[] = new ValidationError(
'Content %type% is not a valid asset target',
Expand Down Expand Up @@ -207,11 +211,16 @@ protected function getSortInfo(BaseValue $value): bool
*/
public function fromHash($hash): Value
{
if ($hash) {
return new Value($hash['destinationContentId'], $hash['alternativeText']);
if (!$hash) {
return new Value();
}

return new Value();
$destinationContentId = $hash['destinationContentId'];
if ($destinationContentId !== null) {
$destinationContentId = (int)$destinationContentId;
}

return new Value($destinationContentId, $hash['alternativeText']);
}

/**
Expand All @@ -223,8 +232,13 @@ public function fromHash($hash): Value
*/
public function toHash(SPIValue $value): array
{
$destinationContentId = null;
if ($value->destinationContentId !== null) {
$destinationContentId = (int)$value->destinationContentId;
}

return [
'destinationContentId' => $value->destinationContentId,
'destinationContentId' => $destinationContentId,
'alternativeText' => $value->alternativeText,
];
}
Expand Down
16 changes: 14 additions & 2 deletions eZ/Publish/Core/FieldType/Relation/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ protected function getSortInfo(BaseValue $value)
*/
public function fromHash($hash)
{
return new Value($hash['destinationContentId']);
$destinationContentId = $hash['destinationContentId'];
if ($destinationContentId !== null) {
$destinationContentId = (int)$destinationContentId;
}

return new Value($destinationContentId);
}

/**
Expand All @@ -253,7 +258,14 @@ public function fromHash($hash)
*/
public function toHash(SPIValue $value)
{
return ['destinationContentId' => $value->destinationContentId];
$destinationContentId = null;
if ($value->destinationContentId !== null) {
$destinationContentId = (int)$value->destinationContentId;
}

return [
'destinationContentId' => $destinationContentId,
];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@ public function __construct(ContentService $contentService)
*
* @return array
*/
public function getViewParameters(Field $field)
public function getViewParameters(Field $field): array
{
try {
$contentInfo = $this->contentService->loadContentInfo($field->value->destinationContentId);
$contentInfo = null;
if ($field->value->destinationContentId !== null) {
$contentInfo = $this->contentService->loadContentInfo(
$field->value->destinationContentId
);
}

return [
'available' => !$contentInfo->isTrashed(),
'available' => $contentInfo !== null && !$contentInfo->isTrashed(),
];
} catch (NotFoundException | UnauthorizedException $exception) {
return [
'available' => false,
];
} catch (NotFoundException $exception) {
return ['available' => false];
} catch (UnauthorizedException $exception) {
return ['available' => false];
}
}
}
Loading