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