Skip to content

Commit

Permalink
Merge pull request #40 from Nattfarinn/fix-wrong-relation
Browse files Browse the repository at this point in the history
fix: log wrong relation and return empty value instead of uncaught exception
  • Loading branch information
lserwatka committed Oct 1, 2015
2 parents 7655b6e + 8f4d9e8 commit 5ffba8b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ services:
- @ez_recommendation.rest.field.type_value
- {fieldIdentifiers: %ez_recommendation.field_identifiers%}
- @ez_recommendation.field.relation_mapper
- @?logger

ez_recommendation.rest.field.type_value:
class: EzSystems\RecommendationBundle\Rest\Field\TypeValue
Expand Down
46 changes: 36 additions & 10 deletions Rest/Field/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use eZ\Publish\API\Repository\Values\Content\Field;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use EzSystems\RecommendationBundle\Exception\InvalidRelationException;
use Psr\Log\LoggerInterface;

class Value
{
Expand All @@ -34,24 +35,31 @@ class Value
/** @var array */
public $fieldDefIdentifiers;

/** @var \Psr\Log\LoggerInterface */
protected $logger;

/**
* @param \eZ\Publish\API\Repository\ContentService $contentService
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
* @param \EzSystems\RecommendationBundle\Rest\Field\TypeValue $typeValue
* @param array $parameters
* @param \EzSystems\RecommendationBundle\Rest\Field\RelationMapper $relationMapper
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
ContentService $contentService,
ContentTypeService $contentTypeService,
TypeValue $typeValue,
array $parameters,
RelationMapper $relationMapper
RelationMapper $relationMapper,
LoggerInterface $logger
) {
$this->contentService = $contentService;
$this->contentTypeService = $contentTypeService;
$this->typeValue = $typeValue;
$this->parameters = $parameters;
$this->relationMapper = $relationMapper;
$this->logger = $logger;
}

/**
Expand All @@ -77,16 +85,34 @@ public function getFieldValue(Content $content, $field, $language)

$mapping = $this->relationMapper->getMapping($contentType->identifier, $field);

if ($mapping && $relatedContentId) {
$relatedContentType = $this->contentTypeService->loadContentType($relatedContent->contentInfo->contentTypeId);

if ($relatedContentType->identifier != $mapping['content']) {
throw new InvalidRelationException(sprintf("Invalid relation: expected '%s' object but recived '%s'", $mapping['content'], $relatedContentType->identifier));
try {
if ($mapping && $relatedContentId) {
$relatedContentType = $this->contentTypeService->loadContentType($relatedContent->contentInfo->contentTypeId);

if ($relatedContentType->identifier != $mapping['content']) {
throw new InvalidRelationException(
sprintf(
"Invalid relation: field '%s:%s' (object: %s, field: %s) has improper relation to object '%s' (object: %s) but '%s:%s' expected.",
$contentType->identifier,
$field,
$content->id,
$fieldObj->id,
$relatedContentType->identifier,
$relatedContentId,
$mapping['content'],
$mapping['field']
)
);
}
$relatedField = $content->getField($mapping['field'], $language);
$value = $relatedField ? $this->getParsedFieldValue($relatedField, $relatedContent, $language, $imageFieldIdentifier) : '';
} else {
$value = $fieldObj ? $this->getParsedFieldValue($fieldObj, $content, $language, $imageFieldIdentifier) : '';
}
$relatedField = $content->getField($mapping['field'], $language);
$value = $relatedField ? $this->getParsedFieldValue($relatedField, $relatedContent, $language, $imageFieldIdentifier) : '';
} else {
$value = $fieldObj ? $this->getParsedFieldValue($fieldObj, $content, $language, $imageFieldIdentifier) : '';
} catch (InvalidRelationException $exception) {
$this->logger->warning($exception->getMessage());

$value = '';
}

return array(
Expand Down

0 comments on commit 5ffba8b

Please sign in to comment.