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