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