|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Contact\Service\Execute; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Contact\Constant\Event; |
|
8
|
|
|
use AbterPhp\Contact\Domain\Entities\Form; |
|
|
|
|
|
|
9
|
|
|
use AbterPhp\Contact\Domain\Entities\Message as Entity; |
|
10
|
|
|
use AbterPhp\Contact\Orm\FormRepo; |
|
11
|
|
|
use AbterPhp\Contact\Validation\Factory\Message as ValidatorFactory; |
|
12
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
|
13
|
|
|
use AbterPhp\Framework\Email\Sender; |
|
14
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
|
15
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
|
16
|
|
|
use Opulence\Orm\OrmException; |
|
17
|
|
|
|
|
18
|
|
|
class Message |
|
19
|
|
|
{ |
|
20
|
|
|
const PHONE_NUMBER = 'contact:phoneNumber'; |
|
21
|
|
|
const NO_PHONE = 'contact:noPhoneNumber'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var ValidatorFactory */ |
|
24
|
|
|
protected $validatorFactory; |
|
25
|
|
|
|
|
26
|
|
|
/** @var FormRepo */ |
|
27
|
|
|
protected $formRepo; |
|
28
|
|
|
|
|
29
|
|
|
/** @var Sender */ |
|
30
|
|
|
protected $sender; |
|
31
|
|
|
|
|
32
|
|
|
/** @var IEventDispatcher */ |
|
33
|
|
|
protected $eventDispatcher; |
|
34
|
|
|
|
|
35
|
|
|
/** @var Form[] */ |
|
36
|
|
|
protected $forms = []; |
|
37
|
|
|
|
|
38
|
|
|
/** @var ITranslator */ |
|
39
|
|
|
protected $translator; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Message constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param FormRepo $formRepo |
|
45
|
|
|
* @param ValidatorFactory $validatorFactory |
|
46
|
|
|
* @param IEventDispatcher $eventDispatcher |
|
47
|
|
|
* @param Sender $sender |
|
48
|
|
|
* @param ITranslator $translator |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
FormRepo $formRepo, |
|
52
|
|
|
ValidatorFactory $validatorFactory, |
|
53
|
|
|
IEventDispatcher $eventDispatcher, |
|
54
|
|
|
Sender $sender, |
|
55
|
|
|
ITranslator $translator |
|
56
|
|
|
) { |
|
57
|
|
|
$this->formRepo = $formRepo; |
|
58
|
|
|
$this->validatorFactory = $validatorFactory; |
|
59
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
60
|
|
|
$this->sender = $sender; |
|
61
|
|
|
$this->translator = $translator; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $formIdentifier |
|
66
|
|
|
* |
|
67
|
|
|
* @return Form|null |
|
68
|
|
|
* @throws \Opulence\Orm\OrmException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getForm(string $formIdentifier): ?Form |
|
71
|
|
|
{ |
|
72
|
|
|
if (array_key_exists($formIdentifier, $this->forms)) { |
|
73
|
|
|
return $this->forms[$formIdentifier]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
|
|
$form = $this->formRepo->getByIdentifier($formIdentifier); |
|
78
|
|
|
} catch (OrmException $e) { |
|
79
|
|
|
try { |
|
80
|
|
|
$form = $this->formRepo->getById($formIdentifier); |
|
81
|
|
|
} catch (OrmException $e) { |
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->forms[$form->getIdentifier()] = $form; |
|
87
|
|
|
|
|
88
|
|
|
return $form; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param Entity $message |
|
93
|
|
|
* |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
|
|
public function send(Entity $message): int |
|
97
|
|
|
{ |
|
98
|
|
|
$this->eventDispatcher->dispatch(Event::MESSAGE_READY, $message); |
|
99
|
|
|
|
|
100
|
|
|
$form = $message->getForm(); |
|
101
|
|
|
$recipients = [$form->getToEmail() => $form->getToName()]; |
|
102
|
|
|
|
|
103
|
|
|
$replyToAddresses = [$message->getFromEmail() => $message->getFromName()]; |
|
104
|
|
|
$fromAddresses = $replyToAddresses; |
|
105
|
|
|
|
|
106
|
|
|
$result = $this->sender->send( |
|
107
|
|
|
$message->getSubject(), |
|
108
|
|
|
$message->getBody(), |
|
109
|
|
|
$recipients, |
|
110
|
|
|
$fromAddresses, |
|
111
|
|
|
$replyToAddresses |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$this->eventDispatcher->dispatch(Event::MESSAGE_SENT, $message); |
|
115
|
|
|
|
|
116
|
|
|
return $result; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getFailedRecipients(): array |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->sender->getFailedRecipients(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $formIdentifier |
|
129
|
|
|
* @param array $postData |
|
130
|
|
|
* |
|
131
|
|
|
* @return array errors |
|
132
|
|
|
* @throws OrmException |
|
133
|
|
|
*/ |
|
134
|
|
|
public function validateForm(string $formIdentifier, array $postData): array |
|
135
|
|
|
{ |
|
136
|
|
|
/** @var Form $form */ |
|
137
|
|
|
$form = $this->getForm($formIdentifier); |
|
138
|
|
|
if (null === $form) { |
|
139
|
|
|
throw new \InvalidArgumentException(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$validator = $this->validatorFactory |
|
143
|
|
|
->setMaxBodyLength($form->getMaxBodyLength()) |
|
144
|
|
|
->createValidator(); |
|
145
|
|
|
|
|
146
|
|
|
if ($validator->isValid($postData)) { |
|
147
|
|
|
return []; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $validator->getErrors()->getAll(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param string $entityId |
|
155
|
|
|
* |
|
156
|
|
|
* @return Entity|IStringerEntity |
|
157
|
|
|
*/ |
|
158
|
|
|
public function createEntity(string $entityId): IStringerEntity |
|
159
|
|
|
{ |
|
160
|
|
|
$form = new Form('', '', '', '', '', '', '', 0); |
|
161
|
|
|
|
|
162
|
|
|
$message = new Entity($entityId, $form, '', '', '', ''); |
|
163
|
|
|
|
|
164
|
|
|
return $message; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $formIdentifier |
|
171
|
|
|
* @param IStringerEntity $entity |
|
172
|
|
|
* @param array $postData |
|
173
|
|
|
* @param array $fileData |
|
174
|
|
|
* |
|
175
|
|
|
* @return IStringerEntity|Entity |
|
176
|
|
|
*/ |
|
177
|
|
|
public function fillEntity( |
|
178
|
|
|
string $formIdentifier, |
|
179
|
|
|
IStringerEntity $entity, |
|
180
|
|
|
array $postData, |
|
181
|
|
|
array $fileData |
|
|
|
|
|
|
182
|
|
|
): IStringerEntity { |
|
183
|
|
|
assert($entity instanceof Entity, new \InvalidArgumentException('Invalid entity')); |
|
184
|
|
|
|
|
185
|
|
|
$form = $this->getForm($formIdentifier); |
|
186
|
|
|
if (null === $form) { |
|
187
|
|
|
return $entity; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$subject = $postData['subject']; |
|
191
|
|
|
|
|
192
|
|
|
$body = $postData['body'] . PHP_EOL . PHP_EOL . $this->translator->translate(static::PHONE_NUMBER) . ': '; |
|
193
|
|
|
$body .= $postData['from_phone'] ? $postData['from_phone'] : $this->translator->translate(static::NO_PHONE); |
|
194
|
|
|
|
|
195
|
|
|
$name = $postData['from_name']; |
|
196
|
|
|
$email = $postData['from_email']; |
|
197
|
|
|
|
|
198
|
|
|
$entity |
|
199
|
|
|
->setForm($form) |
|
200
|
|
|
->setSubject($subject) |
|
201
|
|
|
->setBody($body) |
|
202
|
|
|
->setFromName($name) |
|
203
|
|
|
->setFromEmail($email); |
|
204
|
|
|
|
|
205
|
|
|
return $entity; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: