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 | 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( |
| 196 | $mainProcess, |
| 197 | $config, |
| 198 | $status, |
| 199 | $initiator, |
| 200 | false, |
| 201 | $this->templateProvider |
| 202 | )->getContent(), |
| 203 | 'base64' => false |
| 204 | )); |
| 205 | } |
| 206 | return $entity; |
| 207 | } |
| 208 | |
| 209 | public function toScopeAdminProcessList( |
| 210 | Collection\ProcessList $processList, |
| 211 | Scope $scope, |
| 212 | \DateTimeInterface $dateTime |
| 213 | ) { |
| 214 | $entity = clone $this; |
| 215 | $content = Messaging::getScopeAdminProcessListContent($processList, $scope, $dateTime); |
| 216 | $entity->subject = 'Termine am ' . $dateTime->format('Y-m-d'); |
| 217 | $entity->createIP = 0; |
| 218 | $entity->client = new Client([ |
| 219 | 'email' => $scope->getContactEmail(), |
| 220 | 'familyName' => $scope->getName() |
| 221 | ]); |
| 222 | $entity->multipart[] = new Mimepart(array( |
| 223 | 'mime' => 'text/html', |
| 224 | 'content' => $content, |
| 225 | 'base64' => false |
| 226 | )); |
| 227 | /* |
| 228 | $entity->multipart[] = new Mimepart(array( |
| 229 | 'mime' => 'text/plain', |
| 230 | 'content' => Messaging::getPlainText($content), |
| 231 | 'base64' => false |
| 232 | )); |
| 233 | */ |
| 234 | return $entity; |
| 235 | } |
| 236 | |
| 237 | public function withDepartment($department) |
| 238 | { |
| 239 | $this->department = $department; |
| 240 | return $this; |
| 241 | } |
| 242 | |
| 243 | public function getRecipient() |
| 244 | { |
| 245 | if (! isset($this['client'])) { |
| 246 | $this['client'] = $this->getFirstClient(); |
| 247 | } |
| 248 | if (! isset($this['client']['email']) || "" == $this['client']['email']) { |
| 249 | throw new Exception\MailMissedAddress(); |
| 250 | } |
| 251 | return $this['client']['email']; |
| 252 | } |
| 253 | |
| 254 | public function setTemplateProvider($templateProvider) |
| 255 | { |
| 256 | $this->templateProvider = $templateProvider; |
| 257 | return $this; |
| 258 | } |
| 259 | |
| 260 | public function __toString() |
| 261 | { |
| 262 | $string = "mail#"; |
| 263 | $string .= ($this->hasId()) ? $this->getId() : 0; |
| 264 | $string .= " recipient:" . $this->getRecipient(); |
| 265 | $string .= " process:" . $this->getProcessId(); |
| 266 | return $string; |
| 267 | } |
| 268 | } |