Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.34% covered (warning)
78.34%
123 / 157
66.67% covered (warning)
66.67%
10 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
Mail
78.34% covered (warning)
78.34%
123 / 157
66.67% covered (warning)
66.67%
10 / 15
47.44
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
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
 writeInQueueWithAdmin
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
 writeInQueue
96.15% covered (success)
96.15%
25 / 26
0.00% covered (danger)
0.00%
0 / 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\Zmsdb;
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 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 Query\MailQueue(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 Query\MailQueue(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 Query\MailQueue(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 Query\MailQueue(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 Query\MailQueue(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        $multiPart = $this->readMultiPartByQueueId($entity->id);
111        $entity->addMultiPart($multiPart);
112        if (1 <= $resolveReferences) {
113            $processQuery = new \BO\Zmsdb\Process();
114            $authData = $processQuery->readAuthKeyByProcessId($entity->process['id']);
115            $entity->process = $processQuery
116                ->readEntity(
117                    $entity->process['id'],
118                    is_array($authData) ? $authData['authKey'] : null,
119                    $resolveReferences - 1
120                );
121            $entity->department = (new \BO\Zmsdb\Department())
122                ->readEntity($entity->department['id'], $resolveReferences - 1);
123        }
124        return $entity;
125    }
126
127    public function writeInQueueWithAdmin(Entity $mail)
128    {
129        $query = new Query\MailQueue(Query\Base::INSERT);
130        $process = new \BO\Zmsentities\Process($mail->process);
131        $department = (new Department())->readByScopeId($process->getScopeId(), 0);
132        $query->addValues(
133            array(
134                'processID' => $mail->process['id'],
135                'departmentID' => $department->toProperty()->id->get(),
136                'createIP' => $mail->createIP,
137                'createTimestamp' => time(),
138                'subject' => $mail->subject,
139                'clientFamilyName' => $process->scope['contact']['name'],
140                'clientEmail' => $process->scope['contact']['email']
141            )
142        );
143        $this->writeItem($query);
144        $queueId = $this->getWriter()->lastInsertId();
145        $this->writeMimeparts($queueId, $mail->multipart);
146        return $this->readEntity($queueId);
147    }
148
149    public function writeInQueue(Entity $mail, \DateTimeInterface $dateTime, $count = true)
150    {
151        $query = new Query\MailQueue(Query\Base::INSERT);
152        $process = new \BO\Zmsentities\Process($mail->process);
153        $client = $mail->getFirstClient();
154        if (! $client->hasEmail()) {
155            throw new Exception\Mail\ClientWithoutEmail();
156        }
157        $department = ($mail->department && $mail->department->hasId()) ?
158            $mail->department :
159            (new Department())->readByScopeId($process->getScopeId(), 0);
160        $query->addValues(
161            array(
162                'processID' => $mail->process['id'],
163                'departmentID' => $department->getId(),
164                'createIP' => $mail->createIP,
165                'createTimestamp' => ($dateTime) ? $dateTime->format('U') : time(),
166                'subject' => $mail->subject,
167                'clientFamilyName' => $client->familyName,
168                'clientEmail' => $client->email
169            )
170        );
171        $success = $this->writeItem($query);
172        $queueId = $this->getWriter()->lastInsertId();
173        if ($count && $success) {
174            $client->emailSendCount += 1;
175            (new Process())->updateEntity($process, $dateTime);
176        }
177        $this->writeMimeparts($queueId, $mail->multipart);
178        return $this->readEntity($queueId);
179    }
180
181    public function writeInQueueWithDailyProcessList(
182        \BO\Zmsentities\Scope $scope,
183        Entity $mail
184    ) {
185        $query = new Query\MailQueue(Query\Base::INSERT);
186        $department = (new Department())->readByScopeId($scope->getId(), 0);
187        $query->addValues(
188            array(
189                'departmentID' => $department->toProperty()->id->get(),
190                'createTimestamp' => time(),
191                'createIP' => $mail->createIP,
192                'subject' => $mail->subject,
193                'clientFamilyName' => $mail->client->familyName,
194                'clientEmail' => $mail->client->email
195            )
196        );
197        $this->writeItem($query);
198        $queueId = $this->getWriter()->lastInsertId();
199        $this->writeMimeparts($queueId, $mail->multipart);
200        return $this->readEntity($queueId);
201    }
202
203    protected function writeMimeparts($queueId, $multipart)
204    {
205        $success = true;
206        foreach ($multipart as $part) {
207            $query = new Query\Mimepart(Query\Base::INSERT);
208            $query->addValues(
209                array(
210                    'queueId' => $queueId,
211                    'mime' => $part['mime'],
212                    'content' => $part['content'],
213                    'base64' => $part['base64'] ? 1 : 0
214                )
215            );
216            if (! isset($part['content']) || ! isset($part['mime']) || ! $success) {
217                $this->deleteEntity($queueId);
218                throw new Exception\MailWritePartFailed(
219                    'Failed to write part (' . $part['mime'] . ') of mail with id ' . $queueId
220                );
221            }
222            $success = $this->writeItem($query);
223        }
224        return true;
225    }
226
227    public function deleteEntity($itemId)
228    {
229        $query = Query\MailQueue::QUERY_DELETE;
230        $status = $this->perform($query, [$itemId]);
231        return $status;
232    }
233
234    public function deleteEntities(array $itemIds)
235    {
236        $query = Query\MailQueue::QUERY_MULTI_DELETE;
237        $placeholders = implode(',', array_fill(0, count($itemIds), '?'));
238        $query = str_replace('?', $placeholders, $query);
239        return $this->perform($query, $itemIds);
240    }
241
242    public function readReminderLastRun($now)
243    {
244        $lastRun = (new \BO\Zmsdb\Config())->readProperty('status__mailReminderLastRun', false);
245        $lastRunDateTime = ($lastRun) ? new \DateTimeImmutable($lastRun) : $now;
246        return $lastRunDateTime;
247    }
248
249    public function writeReminderLastRun($now)
250    {
251        (new \BO\Zmsdb\Config())->replaceProperty('status__mailReminderLastRun', $now->format('Y-m-d H:i:s'));
252    }
253
254    protected function readMultiPartByQueueId($queueId)
255    {
256        $query = new Query\Mimepart(Query\Base::SELECT);
257        $query->addEntityMapping()
258            ->addConditionQueueId($queueId);
259        return $this->fetchList($query, new Mimepart());
260    }
261}