1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Factory; |
6
|
|
|
|
7
|
|
|
use function array_key_exists; |
8
|
|
|
use eZ\Publish\API\Repository\Values\Content\Field; |
9
|
|
|
use Netgen\InformationCollection\API\Constants; |
10
|
|
|
use Netgen\InformationCollection\API\ConfigurationConstants; |
11
|
|
|
use Netgen\InformationCollection\API\Exception\MissingValueException; |
12
|
|
|
use Netgen\InformationCollection\API\Value\DataTransfer\EmailContent; |
13
|
|
|
use Netgen\InformationCollection\API\Value\DataTransfer\TemplateContent; |
14
|
|
|
use Netgen\InformationCollection\API\Value\Event\InformationCollected; |
15
|
|
|
use function trim; |
16
|
|
|
|
17
|
|
|
class AutoResponderDataFactory extends EmailDataFactory |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Factory method. |
21
|
|
|
* |
22
|
|
|
* @param InformationCollected $value |
23
|
|
|
* |
24
|
|
|
* @return EmailContent |
25
|
|
|
*/ |
26
|
|
|
public function build(InformationCollected $value): EmailContent |
27
|
|
|
{ |
28
|
|
|
$location = $value->getLocation(); |
|
|
|
|
29
|
|
|
$contentType = $value->getContentType(); |
30
|
|
|
|
31
|
|
|
$template = $this->resolveTemplate($contentType->identifier); |
32
|
|
|
|
33
|
|
|
$templateWrapper = $this->twig->load($template); |
34
|
|
|
$data = new TemplateContent($value, $templateWrapper); |
35
|
|
|
|
36
|
|
|
$body = $this->resolveBody($data); |
37
|
|
|
|
38
|
|
|
return new EmailContent( |
39
|
|
|
$this->resolveRecipient($data), |
40
|
|
|
[$this->resolve($data, Constants::FIELD_SENDER, Constants::FIELD_TYPE_EMAIL)], |
41
|
|
|
$this->resolveSubject($data), |
42
|
|
|
$body |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns resolved parameter. |
48
|
|
|
* |
49
|
|
|
* @param TemplateContent $data |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
protected function resolveRecipient(TemplateContent $data) |
54
|
|
|
{ |
55
|
|
|
$fields = $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(); |
56
|
|
View Code Duplication |
if ($data->getTemplateWrapper()->hasBlock(Constants::FIELD_RECIPIENT)) { |
|
|
|
|
57
|
|
|
$rendered = $data->getTemplateWrapper()->renderBlock( |
58
|
|
|
Constants::FIELD_RECIPIENT, |
59
|
|
|
[ |
60
|
|
|
'event' => $data->getEvent(), |
61
|
|
|
'collected_fields' => $fields, |
62
|
|
|
'content' => $data->getContent(), |
63
|
|
|
] |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$rendered = trim($rendered); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
if (!empty($rendered)) { |
|
|
|
|
70
|
|
|
|
71
|
|
|
$emails = explode(',', $rendered); |
72
|
|
|
|
73
|
|
|
$emails = array_filter($emails, function($var) { |
74
|
|
|
return filter_var($var, FILTER_VALIDATE_EMAIL); |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
if (!empty($emails)) { |
78
|
|
|
return $emails; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$field = 'email'; |
83
|
|
View Code Duplication |
if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_FIELD_IDENTIFIER])) { |
|
|
|
|
84
|
|
|
$field = $this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_FIELD_IDENTIFIER]; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (array_key_exists($field, $fields)) { |
88
|
|
|
return [$fields[$field]->email]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
throw new MissingValueException($field); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns resolved parameter. |
96
|
|
|
* |
97
|
|
|
* @param TemplateContent $data |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function resolveSubject(TemplateContent $data) |
102
|
|
|
{ |
103
|
|
|
$fields = $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(); |
104
|
|
View Code Duplication |
if ($data->getTemplateWrapper()->hasBlock(Constants::FIELD_AUTO_RESPONDER_SUBJECT)) { |
|
|
|
|
105
|
|
|
$rendered = $data->getTemplateWrapper()->renderBlock( |
106
|
|
|
Constants::FIELD_AUTO_RESPONDER_SUBJECT, |
107
|
|
|
[ |
108
|
|
|
'event' => $data->getEvent(), |
109
|
|
|
'collected_fields' => $fields, |
110
|
|
|
'content' => $data->getContent(), |
111
|
|
|
] |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
return trim($rendered); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$content = $data->getContent(); |
118
|
|
|
if (array_key_exists(Constants::FIELD_AUTO_RESPONDER_SUBJECT, $content->fields) && |
119
|
|
|
!$this->fieldHelper->isFieldEmpty($content, Constants::FIELD_AUTO_RESPONDER_SUBJECT) |
120
|
|
|
) { |
121
|
|
|
$fieldValue = $this->translationHelper->getTranslatedField($content, Constants::FIELD_AUTO_RESPONDER_SUBJECT); |
122
|
|
|
|
123
|
|
|
if ($fieldValue instanceof Field) { |
124
|
|
|
return $fieldValue->value->text; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_SUBJECT])) { |
|
|
|
|
129
|
|
|
return $this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_SUBJECT]; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$message = Constants::FIELD_AUTO_RESPONDER_SUBJECT . '|' . ConfigurationConstants::EMAIL_SUBJECT; |
133
|
|
|
|
134
|
|
|
throw new MissingValueException($message); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.