Skip to content

Commit

Permalink
Fallback logic in EmailDataFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioBlazek committed Mar 9, 2018
1 parent 9a26188 commit b3c4eea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
55 changes: 52 additions & 3 deletions bundle/Factory/EmailDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function build(InformationCollected $value)
$body = $this->resolveBody($data);

return new EmailData(
$this->resolve($data, Constants::FIELD_RECIPIENT, Constants::FIELD_TYPE_EMAIL),
$this->resolve($data, Constants::FIELD_SENDER, Constants::FIELD_TYPE_EMAIL),
$this->resolveEmail($data, Constants::FIELD_RECIPIENT),
$this->resolveEmail($data, Constants::FIELD_SENDER),
$this->resolve($data, Constants::FIELD_SUBJECT),
$body,
$this->resolveAttachments($contentType->identifier, $value->getInformationCollectionStruct()->getCollectedFields())
Expand All @@ -107,6 +107,7 @@ public function build(InformationCollected $value)
*/
protected function resolve(TemplateData $data, $field, $property = Constants::FIELD_TYPE_TEXT)
{
$rendered = '';
if ($data->getTemplateWrapper()->hasBlock($field)) {
$rendered = $data->getTemplateWrapper()->renderBlock(
$field,
Expand All @@ -117,7 +118,11 @@ protected function resolve(TemplateData $data, $field, $property = Constants::FI
)
);

return trim($rendered);
$rendered = trim($rendered);
}

if (!empty($rendered)) {
return $rendered;
}

$content = $data->getContent();
Expand All @@ -136,6 +141,50 @@ protected function resolve(TemplateData $data, $field, $property = Constants::FI
throw new MissingValueException($field);
}

/**
* Returns resolved email parameter.
*
* @param TemplateData $data
* @param string $field
*
* @return string
*/
protected function resolveEmail(TemplateData $data, $field)
{
$rendered = '';
if ($data->getTemplateWrapper()->hasBlock($field)) {
$rendered = $data->getTemplateWrapper()->renderBlock(
$field,
array(
'event' => $data->getEvent(),
'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(),
'content' => $data->getContent(),
)
);

$rendered = trim($rendered);
}

if (!empty($rendered) && filter_var($rendered, FILTER_VALIDATE_EMAIL)) {
return $rendered;
}

$content = $data->getContent();
if (array_key_exists($field, $content->fields) &&
!$this->fieldHelper->isFieldEmpty($content, $field)
) {
$fieldValue = $this->translationHelper->getTranslatedField($content, $field);

return $fieldValue->value->email;
}

if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field])) {
return $this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field];
}

throw new MissingValueException($field);
}

/**
* Returns resolved template name.
*
Expand Down
4 changes: 4 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.4.1] - 2018-03-09
### Fixed
- Continue with fallback logic if subject/recipient/sender template blocks do not return value

## [1.4.0] - 2018-02-28
### Added
- Custom field hadlers for Date, Time and DateAndTime field values
Expand Down

0 comments on commit b3c4eea

Please sign in to comment.