1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Factory; |
6
|
|
|
|
7
|
|
|
use function array_key_exists; |
8
|
|
|
use Netgen\InformationCollection\API\Value\Event\InformationCollected; |
9
|
|
|
use Netgen\InformationCollection\API\Exception\MissingValueException; |
10
|
|
|
use Netgen\InformationCollection\API\Value\DataTransfer\EmailContent; |
11
|
|
|
use Netgen\InformationCollection\API\Value\DataTransfer\TemplateContent; |
12
|
|
|
use Netgen\InformationCollection\API\Constants; |
13
|
|
|
use function trim; |
14
|
|
|
|
15
|
|
|
class AutoResponderDataFactory extends EmailDataFactory |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Factory method. |
19
|
|
|
* |
20
|
|
|
* @param InformationCollected $value |
21
|
|
|
* |
22
|
|
|
* @return EmailData |
23
|
|
|
*/ |
24
|
|
|
public function build(InformationCollected $value) |
25
|
|
|
{ |
26
|
|
|
$location = $value->getLocation(); |
27
|
|
|
$contentType = $value->getContentType(); |
28
|
|
|
$content = $this->contentService->loadContent($location->contentId); |
29
|
|
|
|
30
|
|
|
$template = $this->resolveTemplate($contentType->identifier); |
31
|
|
|
|
32
|
|
|
$templateWrapper = $this->twig->load($template); |
33
|
|
|
$data = new TemplateData($value, $content, $templateWrapper); |
34
|
|
|
|
35
|
|
|
$body = $this->resolveBody($data); |
36
|
|
|
|
37
|
|
|
return new EmailData( |
38
|
|
|
$this->resolveRecipient($data), |
39
|
|
|
$this->resolve($data, Constants::FIELD_SENDER, Constants::FIELD_TYPE_EMAIL), |
40
|
|
|
$this->resolveSubject($data), |
41
|
|
|
$body |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns resolved parameter. |
47
|
|
|
* |
48
|
|
|
* @param TemplateData $data |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
protected function resolveRecipient(TemplateData $data) |
53
|
|
|
{ |
54
|
|
|
$fields = $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(); |
55
|
|
View Code Duplication |
if ($data->getTemplateWrapper()->hasBlock(Constants::FIELD_RECIPIENT)) { |
|
|
|
|
56
|
|
|
$rendered = $data->getTemplateWrapper()->renderBlock( |
57
|
|
|
Constants::FIELD_RECIPIENT, |
58
|
|
|
[ |
59
|
|
|
'event' => $data->getEvent(), |
60
|
|
|
'collected_fields' => $fields, |
61
|
|
|
'content' => $data->getContent(), |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return trim($rendered); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$field = 'email'; |
69
|
|
View Code Duplication |
if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_FIELD_IDENTIFIER])) { |
|
|
|
|
70
|
|
|
$field = $this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_FIELD_IDENTIFIER]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (array_key_exists($field, $fields)) { |
74
|
|
|
return $fields[$field]->email; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new MissingValueException($field); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns resolved parameter. |
82
|
|
|
* |
83
|
|
|
* @param TemplateData $data |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function resolveSubject(TemplateData $data) |
88
|
|
|
{ |
89
|
|
|
$fields = $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(); |
90
|
|
View Code Duplication |
if ($data->getTemplateWrapper()->hasBlock(Constants::FIELD_AUTO_RESPONDER_SUBJECT)) { |
|
|
|
|
91
|
|
|
$rendered = $data->getTemplateWrapper()->renderBlock( |
92
|
|
|
Constants::FIELD_AUTO_RESPONDER_SUBJECT, |
93
|
|
|
[ |
94
|
|
|
'event' => $data->getEvent(), |
95
|
|
|
'collected_fields' => $fields, |
96
|
|
|
'content' => $data->getContent(), |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return trim($rendered); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$content = $data->getContent(); |
104
|
|
|
if (array_key_exists(Constants::FIELD_AUTO_RESPONDER_SUBJECT, $content->fields) && |
105
|
|
|
!$this->fieldHelper->isFieldEmpty($content, Constants::FIELD_AUTO_RESPONDER_SUBJECT) |
106
|
|
|
) { |
107
|
|
|
$fieldValue = $this->translationHelper->getTranslatedField($content, Constants::FIELD_AUTO_RESPONDER_SUBJECT); |
108
|
|
|
|
109
|
|
|
return $fieldValue->value->text; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_SUBJECT])) { |
|
|
|
|
113
|
|
|
return $this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_SUBJECT]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$message = Constants::FIELD_AUTO_RESPONDER_SUBJECT . '|' . ConfigurationConstants::EMAIL_SUBJECT; |
117
|
|
|
throw new MissingValueException($message); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.