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