Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.13% |
113 / 124 |
|
77.78% |
14 / 18 |
CRAP | |
0.00% |
0 / 1 |
|
91.13% |
113 / 124 |
|
77.78% |
14 / 18 |
44.29 | |
0.00% |
0 / 1 |
|
getDefaults | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
getProcessId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getProcessAuthKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addMultiPart | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
hasIcs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getHtmlPart | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getPlainPart | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getIcsPart | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getClient | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getFirstClient | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
toCustomMessageEntity | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
6 | |||
toResolvedEntity | |
96.77% |
30 / 31 |
|
0.00% |
0 / 1 |
8 | |||
toScopeAdminProcessList | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
withDepartment | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getRecipient | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
setTemplateProvider | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
__toString | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace BO\Zmsentities; |
4 | |
5 | use BO\Zmsentities\Process; |
6 | use BO\Zmsentities\Collection\ProcessList; |
7 | use BO\Zmsentities\Helper\Messaging; |
8 | use BO\Zmsentities\Helper\Property; |
9 | |
10 | /** |
11 | * @SuppressWarnings(CouplingBetweenObjects) |
12 | * |
13 | */ |
14 | class Mail extends Schema\Entity |
15 | { |
16 | const PRIMARY = 'id'; |
17 | |
18 | public static $schema = "mail.json"; |
19 | |
20 | protected $templateProvider = false; |
21 | |
22 | public function getDefaults() |
23 | { |
24 | return [ |
25 | 'multipart' => new Collection\MimepartList(), |
26 | 'process' => new Process(), |
27 | 'department' => new Department(), |
28 | 'client' => new Client(), |
29 | ]; |
30 | } |
31 | |
32 | public function getProcessId() |
33 | { |
34 | return $this->toProperty()->process->id->get(); |
35 | } |
36 | |
37 | public function getProcessAuthKey() |
38 | { |
39 | return $this->toProperty()->process->authKey->get(); |
40 | } |
41 | |
42 | public function addMultiPart($multiPart) |
43 | { |
44 | $this->multipart = $multiPart; |
45 | return $this; |
46 | } |
47 | |
48 | public function hasContent() |
49 | { |
50 | return ($this->getHtmlPart() && $this->getPlainPart()); |
51 | } |
52 | |
53 | public function hasIcs() |
54 | { |
55 | return (null != $this->getIcsPart()); |
56 | } |
57 | |
58 | public function getHtmlPart() |
59 | { |
60 | $multiPart = $this->toProperty()->multipart->get(); |
61 | foreach ($multiPart as $part) { |
62 | $mimepart = new Mimepart($part); |
63 | if ($mimepart->isHtml()) { |
64 | return $mimepart->getContent(); |
65 | } |
66 | } |
67 | return null; |
68 | } |
69 | |
70 | public function getPlainPart() |
71 | { |
72 | foreach ($this->multipart as $part) { |
73 | $mimepart = new Mimepart($part); |
74 | if ($mimepart->isText()) { |
75 | return $mimepart->getContent(); |
76 | } |
77 | } |
78 | return null; |
79 | } |
80 | |
81 | public function getIcsPart() |
82 | { |
83 | foreach ($this->multipart as $part) { |
84 | $mimepart = new Mimepart($part); |
85 | if ($mimepart->isIcs()) { |
86 | return $mimepart->getContent(); |
87 | } |
88 | } |
89 | return null; |
90 | } |
91 | |
92 | public function getClient(): ?Client |
93 | { |
94 | if (isset($this['client'])) { |
95 | return $this['client']; |
96 | } |
97 | |
98 | return $this->getFirstClient(); |
99 | } |
100 | |
101 | public function getFirstClient() |
102 | { |
103 | $client = null; |
104 | if ($this->toProperty()->process->isAvailable()) { |
105 | $process = new Process($this->process); |
106 | $client = $process->getFirstClient(); |
107 | } |
108 | return $client; |
109 | } |
110 | |
111 | public function toCustomMessageEntity(Process $process, $collection) |
112 | { |
113 | $entity = clone $this; |
114 | $message = ''; |
115 | if ( |
116 | Property::__keyExists( |
117 | 'message', |
118 | $collection |
119 | ) && |
120 | '' != $collection['message']->getValue() |
121 | ) { |
122 | $message = $collection['message']->getValue(); |
123 | } |
124 | if ( |
125 | Property::__keyExists( |
126 | 'subject', |
127 | $collection |
128 | ) && |
129 | '' != $collection['subject']->getValue() |
130 | ) { |
131 | $entity->subject = $collection['subject']->getValue(); |
132 | } |
133 | $entity->process = $process; |
134 | $entity->createIP = $process->createIP; |
135 | |
136 | if (! isset($entity['client'])) { |
137 | $entity['client'] = $entity->getFirstClient(); |
138 | } |
139 | |
140 | $entity->multipart[] = new Mimepart(array( |
141 | 'mime' => 'text/html', |
142 | 'content' => $message, |
143 | 'base64' => false |
144 | )); |
145 | $entity->multipart[] = new Mimepart(array( |
146 | 'mime' => 'text/plain', |
147 | 'content' => Messaging::getPlainText($message), |
148 | 'base64' => false |
149 | )); |
150 | return $entity; |
151 | } |
152 | |
153 | /** |
154 | * @param Process|ProcessList Process|ProcessList $processList |
155 | * @param Config $config |
156 | * @param $status |
157 | * @param $initiator |
158 | * @return Mail |
159 | * @throws Exception\TemplateNotFound |
160 | */ |
161 | public function toResolvedEntity($processList, Config $config, $status, $initiator = null) |
162 | { |
163 | $collection = (new ProcessList())->testProcessListLength( |
164 | $processList, |
165 | Messaging::isEmptyProcessListAllowed($status) |
166 | ); |
167 | $mainProcess = $collection->getFirst(); |
168 | $entity = clone $this; |
169 | $content = Messaging::getMailContent($collection, $config, $initiator, $status, $this->templateProvider); |
170 | |
171 | $entity->process = ($mainProcess) ? ($mainProcess) : new Process(); |
172 | $entity->subject = ($mainProcess) ? |
173 | Messaging::getMailSubject($mainProcess, $config, $initiator, $status, $this->templateProvider) : |
174 | Messaging::getMailSubject((new Process()), $config, $initiator, $status, $this->templateProvider); |
175 | $entity->createIP = ($mainProcess) ? $mainProcess->createIP : ''; |
176 | $entity['client'] = (! isset($entity['client'])) ? $entity->getFirstClient() : $entity['client']; |
177 | |
178 | $entity->multipart[] = new Mimepart(array( |
179 | 'mime' => 'text/html', |
180 | 'content' => $content, |
181 | 'base64' => false |
182 | )); |
183 | $entity->multipart[] = new Mimepart(array( |
184 | 'mime' => 'text/plain', |
185 | 'content' => Messaging::getPlainText($content), |
186 | 'base64' => false |
187 | )); |
188 | |
189 | if ( |
190 | $mainProcess && $mainProcess->getAppointments()->getFirst()->hasTime() && |
191 | Messaging::isIcsRequired($config, $mainProcess, $status) |
192 | ) { |
193 | $entity->multipart[] = new Mimepart(array( |
194 | 'mime' => 'text/calendar', |
195 | 'content' => Messaging::getMailIcs($mainProcess, $config, $status, $initiator, false, $this->templateProvider)->getContent(), |
196 | 'base64' => false |
197 | )); |
198 | } |
199 | return $entity; |
200 | } |
201 | |
202 | public function toScopeAdminProcessList( |
203 | Collection\ProcessList $processList, |
204 | Scope $scope, |
205 | \DateTimeInterface $dateTime |
206 | ) { |
207 | $entity = clone $this; |
208 | $content = Messaging::getScopeAdminProcessListContent($processList, $scope, $dateTime); |
209 | $entity->subject = 'Termine am ' . $dateTime->format('Y-m-d'); |
210 | $entity->createIP = 0; |
211 | $entity->client = new Client([ |
212 | 'email' => $scope->getContactEmail(), |
213 | 'familyName' => $scope->getName() |
214 | ]); |
215 | $entity->multipart[] = new Mimepart(array( |
216 | 'mime' => 'text/html', |
217 | 'content' => $content, |
218 | 'base64' => false |
219 | )); |
220 | /* |
221 | $entity->multipart[] = new Mimepart(array( |
222 | 'mime' => 'text/plain', |
223 | 'content' => Messaging::getPlainText($content), |
224 | 'base64' => false |
225 | )); |
226 | */ |
227 | return $entity; |
228 | } |
229 | |
230 | public function withDepartment($department) |
231 | { |
232 | $this->department = $department; |
233 | return $this; |
234 | } |
235 | |
236 | public function getRecipient() |
237 | { |
238 | if (! isset($this['client'])) { |
239 | $this['client'] = $this->getFirstClient(); |
240 | } |
241 | if (! isset($this['client']['email']) || "" == $this['client']['email']) { |
242 | throw new Exception\MailMissedAddress(); |
243 | } |
244 | return $this['client']['email']; |
245 | } |
246 | |
247 | public function setTemplateProvider($templateProvider) |
248 | { |
249 | $this->templateProvider = $templateProvider; |
250 | return $this; |
251 | } |
252 | |
253 | public function __toString() |
254 | { |
255 | $string = "mail#"; |
256 | $string .= ($this->hasId()) ? $this->getId() : 0; |
257 | $string .= " recipient:" . $this->getRecipient(); |
258 | $string .= " process:" . $this->getProcessId(); |
259 | return $string; |
260 | } |
261 | } |