Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.62% covered (warning)
78.62%
125 / 159
66.67% covered (warning)
66.67%
10 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
Mail
78.62% covered (warning)
78.62%
125 / 159
66.67% covered (warning)
66.67%
10 / 15
48.67
0.00% covered (danger)
0.00%
0 / 1
 readEntity
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 readEntities
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 readEntitiesIds
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 readList
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 readListIds
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 readResolvedReferences
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
4.00
 writeInQueueWithAdmin
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
 writeInQueue
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
7
 writeInQueueWithDailyProcessList
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 writeMimeparts
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
6
 deleteEntity
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 deleteEntities
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 readReminderLastRun
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 writeReminderLastRun
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 readMultiPartByQueueId
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace BO\Zmsbackend\Mail\Service;
4
5use BO\Zmsentities\Mail as Entity;
6use BO\Zmsentities\Mimepart;
7use BO\Zmsentities\Process as ProcessEntity;
8use BO\Zmsentities\Collection\MailList as Collection;
9
10/**
11 *
12 * @SuppressWarnings(CouplingBetweenObjects)
13 */
14class Mail extends \BO\Zmsbackend\Base
15{
16    /**
17     * Fetch status from db
18     *
19     * @return \BO\Zmsentities\Mail
20     */
21    public function readEntity($itemId, $resolveReferences = 1)
22    {
23        $query = new \BO\Zmsbackend\Mail\Repository\MailQueue(\BO\Zmsbackend\Query\Base::SELECT);
24        $query->addEntityMapping()
25            ->addResolvedReferences($resolveReferences)
26            ->addConditionItemId($itemId);
27        $mail = $this->fetchOne($query, new Entity());
28        if ($mail && $mail->hasId()) {
29            $mail = $this->readResolvedReferences($mail, $resolveReferences);
30        }
31        return $mail;
32    }
33
34    public function readEntities(array $itemIds, $resolveReferences = 1, $limit = 300, $order = 'ASC')
35    {
36        $mailList = new Collection();
37        $query = new \BO\Zmsbackend\Mail\Repository\MailQueue(\BO\Zmsbackend\Query\Base::SELECT);
38        $query->addEntityMapping()
39              ->addResolvedReferences($resolveReferences)
40              ->addWhereIn('id', $itemIds)
41              ->addOrderBy('createTimestamp', $order)
42              ->addLimit($limit);
43        $result = $this->fetchList($query, new Entity());
44
45        foreach ($result as $item) {
46            $entity = new Entity($item);
47            $entity = $this->readResolvedReferences($entity, $resolveReferences);
48            if ($entity instanceof Entity) {
49                $mailList->addEntity($entity);
50            }
51        }
52
53        return $mailList;
54    }
55
56
57    public function readEntitiesIds(array $itemIds, $resolveReferences = 1, $limit = 300, $order = 'ASC')
58    {
59
60        $query = new \BO\Zmsbackend\Mail\Repository\MailQueue(\BO\Zmsbackend\Query\Base::SELECT);
61        $query->selectFields(['id', 'createTimestamp'])
62              ->addEntityMapping()
63              ->addResolvedReferences($resolveReferences)
64              ->addWhereIn('id', $itemIds)
65              ->addOrderBy('createTimestamp', $order)
66              ->addLimit($limit);
67
68        return $this->fetchList($query, new Entity());
69    }
70
71    public function readList($resolveReferences = 1, $limit = 300, $order = 'ASC')
72    {
73        $mailList = new Collection();
74        $query = new \BO\Zmsbackend\Mail\Repository\MailQueue(\BO\Zmsbackend\Query\Base::SELECT);
75        $query->addEntityMapping()
76              ->addResolvedReferences($resolveReferences)
77              ->addOrderBy('createTimestamp', $order)
78              ->addLimit($limit);
79        $result = $this->fetchList($query, new Entity());
80
81        foreach ($result as $item) {
82            $entity = new Entity($item);
83            $entity = $this->readResolvedReferences($entity, $resolveReferences);
84            if ($entity instanceof Entity) {
85                $mailList->addEntity($entity);
86            }
87        }
88
89        return $mailList;
90    }
91
92
93    public function readListIds($resolveReferences = 1, $limit = 300, $order = 'ASC')
94    {
95        $query = new \BO\Zmsbackend\Mail\Repository\MailQueue(\BO\Zmsbackend\Query\Base::SELECT);
96        $query->selectFields(['id', 'createTimestamp'])
97              ->addEntityMapping()
98              ->addResolvedReferences($resolveReferences)
99              ->addOrderBy('createTimestamp', $order)
100              ->addLimit($limit);
101
102        return $this->fetchList($query, new Entity());
103    }
104
105
106
107    #[\Override]
108    public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, $resolveReferences)
109    {
110        if (!$entity instanceof Entity) {
111            throw new \InvalidArgumentException('Expected ' . Entity::class);
112        }
113        $multiPart = $this->readMultiPartByQueueId($entity->id);
114        $entity->addMultiPart($multiPart);
115        if (1 <= $resolveReferences) {
116            $processQuery = new \BO\Zmsbackend\Process\Service\Process();
117            $authData = $processQuery->readAuthKeyByProcessId($entity->process['id']);
118            $entity->process = $processQuery
119                ->readEntity(
120                    $entity->process['id'],
121                    is_array($authData) ? $authData['authKey'] : null,
122                    $resolveReferences - 1
123                );
124            $entity->department = (new \BO\Zmsbackend\Department\Service\Department())
125                ->readEntity($entity->department['id'], $resolveReferences - 1);
126        }
127        return $entity;
128    }
129
130    public function writeInQueueWithAdmin(Entity $mail)
131    {
132        $query = new \BO\Zmsbackend\Mail\Repository\MailQueue(\BO\Zmsbackend\Query\Base::INSERT);
133        $process = new \BO\Zmsentities\Process($mail->process);
134        $department = (new \BO\Zmsbackend\Department\Service\Department())->readByScopeId($process->getScopeId(), 0);
135        $query->addValues(
136            array(
137                'processID' => $mail->process['id'],
138                'departmentID' => $department->toProperty()->id->get(),
139                'createIP' => $mail->createIP,
140                'createTimestamp' => time(),
141                'subject' => $mail->subject,
142                'clientFamilyName' => $process->scope['contact']['name'],
143                'clientEmail' => $process->scope['contact']['email']
144            )
145        );
146        $this->writeItem($query);
147        $queueId = $this->getWriter()->lastInsertId();
148        $this->writeMimeparts($queueId, $mail->multipart);
149        return $this->readEntity($queueId);
150    }
151
152    public function writeInQueue(Entity $mail, \DateTimeInterface $dateTime, $count = true)
153    {
154        $query = new \BO\Zmsbackend\Mail\Repository\MailQueue(\BO\Zmsbackend\Query\Base::INSERT);
155        $process = new \BO\Zmsentities\Process($mail->process);
156        $client = $mail->getFirstClient();
157        if (! $client->hasEmail()) {
158            throw new \BO\Zmsbackend\Mail\Exception\ClientWithoutEmail();
159        }
160        $department = ($mail->department && $mail->department->hasId()) ?
161            $mail->department :
162            (new \BO\Zmsbackend\Department\Service\Department())->readByScopeId($process->getScopeId(), 0);
163        $query->addValues(
164            array(
165                'processID' => $mail->process['id'],
166                'departmentID' => $department->getId(),
167                'createIP' => $mail->createIP,
168                'createTimestamp' => ($dateTime) ? $dateTime->format('U') : time(),
169                'subject' => $mail->subject,
170                'clientFamilyName' => $client->familyName,
171                'clientEmail' => $client->email
172            )
173        );
174        $success = $this->writeItem($query);
175        $queueId = $this->getWriter()->lastInsertId();
176        if ($count && $success) {
177            $client->emailSendCount += 1;
178            (new \BO\Zmsbackend\Process\Service\Process())->updateEntity($process, $dateTime);
179        }
180        $this->writeMimeparts($queueId, $mail->multipart);
181        return $this->readEntity($queueId);
182    }
183
184    public function writeInQueueWithDailyProcessList(
185        \BO\Zmsentities\Scope $scope,
186        Entity $mail
187    ) {
188        $query = new \BO\Zmsbackend\Mail\Repository\MailQueue(\BO\Zmsbackend\Query\Base::INSERT);
189        $department = (new \BO\Zmsbackend\Department\Service\Department())->readByScopeId($scope->getId(), 0);
190        $query->addValues(
191            array(
192                'departmentID' => $department->toProperty()->id->get(),
193                'createTimestamp' => time(),
194                'createIP' => $mail->createIP,
195                'subject' => $mail->subject,
196                'clientFamilyName' => $mail->client->familyName,
197                'clientEmail' => $mail->client->email
198            )
199        );
200        $this->writeItem($query);
201        $queueId = $this->getWriter()->lastInsertId();
202        $this->writeMimeparts($queueId, $mail->multipart);
203        return $this->readEntity($queueId);
204    }
205
206    protected function writeMimeparts($queueId, $multipart)
207    {
208        $success = true;
209        foreach ($multipart as $part) {
210            $query = new \BO\Zmsbackend\Mail\Repository\Mimepart(\BO\Zmsbackend\Query\Base::INSERT);
211            $query->addValues(
212                array(
213                    'queueId' => $queueId,
214                    'mime' => $part['mime'],
215                    'content' => $part['content'],
216                    'base64' => $part['base64'] ? 1 : 0
217                )
218            );
219            if (! isset($part['content']) || ! isset($part['mime']) || ! $success) {
220                $this->deleteEntity($queueId);
221                throw new \BO\Zmsbackend\Mail\Exception\MailWritePartFailed(
222                    'Failed to write part (' . $part['mime'] . ') of mail with id ' . $queueId
223                );
224            }
225            $success = $this->writeItem($query);
226        }
227        return true;
228    }
229
230    public function deleteEntity($itemId)
231    {
232        $query = \BO\Zmsbackend\Mail\Repository\MailQueue::QUERY_DELETE;
233        $status = $this->perform($query, [$itemId]);
234        return $status;
235    }
236
237    public function deleteEntities(array $itemIds)
238    {
239        $query = \BO\Zmsbackend\Mail\Repository\MailQueue::QUERY_MULTI_DELETE;
240        $placeholders = implode(',', array_fill(0, count($itemIds), '?'));
241        $query = str_replace('?', $placeholders, $query);
242        return $this->perform($query, $itemIds);
243    }
244
245    public function readReminderLastRun($now)
246    {
247        $lastRun = (new \BO\Zmsbackend\Config\Service\Config())->readProperty('status__mailReminderLastRun', false);
248        $lastRunDateTime = ($lastRun) ? new \DateTimeImmutable($lastRun) : $now;
249        return $lastRunDateTime;
250    }
251
252    public function writeReminderLastRun($now)
253    {
254        (new \BO\Zmsbackend\Config\Service\Config())->replaceProperty('status__mailReminderLastRun', $now->format('Y-m-d H:i:s'));
255    }
256
257    protected function readMultiPartByQueueId($queueId)
258    {
259        $query = new \BO\Zmsbackend\Mail\Repository\Mimepart(\BO\Zmsbackend\Query\Base::SELECT);
260        $query->addEntityMapping()
261            ->addConditionQueueId($queueId);
262        return $this->fetchList($query, new Mimepart());
263    }
264}