Lines
47.43%
37 / 78
Functions and Methods
50.00%
5 / 10
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| QueueMailReminder | 47.43% | 37 / 78 | 50.00% | 5 / 10 | 92.29 | 0.00% | 0 / 1 | |||
| __construct | 60.00% | 9 / 15 | 0.00% | 0 / 1 | 5.02 | |||||
| log | 50.00% | 1 / 2 | 0.00% | 0 / 1 | 2.50 | |||||
| getCount | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| setLimit | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| setLoopCount | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| startProcessing | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| writeMailReminderList | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 1 | |||||
| writeByCallback | 70.00% | 7 / 10 | 0.00% | 0 / 1 | 4.43 | |||||
| writeReminder | 0.00% | 0 / 19 | 0.00% | 0 / 1 | 20 | |||||
| getProcessListOverview | 0.00% | 0 / 12 | 0.00% | 0 / 1 | 6 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Helper; | |
| 4 | ||
| 5 | use BO\Zmsbackend\Log\Service\Log; | |
| 6 | use BO\Zmsbackend\Process\Service\Process as ProcessRepository; | |
| 7 | use BO\Zmsbackend\Mail\Service\Mail as MailRepository; | |
| 8 | use BO\Zmsbackend\Department\Service\Department as DepartmentRepository; | |
| 9 | use BO\Zmsbackend\Config\Service\Config as ConfigRepository; | |
| 10 | use BO\Zmsentities\Collection\ProcessList as Collection; | |
| 11 | use BO\Zmsentities\Mail; | |
| 12 | use BO\Zmsentities\Process; | |
| 13 | ||
| 14 | class QueueMailReminder | |
| 15 | { | |
| 16 | public \DateTimeInterface $dateTime; | |
| 17 | ||
| 18 | protected $lastRun; | |
| 19 | ||
| 20 | protected $defaultReminderInMinutes; | |
| 21 | ||
| 22 | protected $verbose = false; | |
| 23 | ||
| 24 | protected $limit = 5000; | |
| 25 | ||
| 26 | protected $loopCount = 500; | |
| 27 | ||
| 28 | protected $count = 0; | |
| 29 | ||
| 30 | public function __construct(\DateTimeInterface $now, int $sendReminderBeforeMinutes, bool $verbose = false) | |
| 31 | { | |
| 32 | $config = (new ConfigRepository())->readEntity(); | |
| 33 | $configLimit = $config->getPreference('mailings', 'sqlMaxLimit'); | |
| 34 | $configBatchSize = $config->getPreference('mailings', 'sqlBatchSize'); | |
| 35 | $this->limit = ($configLimit) ? $configLimit : $this->limit; | |
| 36 | $this->loopCount = ($configBatchSize) ? $configBatchSize : $this->loopCount; | |
| 37 | $this->dateTime = $now; | |
| 38 | $this->defaultReminderInMinutes = $sendReminderBeforeMinutes; | |
| 39 | $this->lastRun = (new MailRepository())->readReminderLastRun($now); | |
| 40 | if ($verbose) { | |
| 41 | $this->verbose = true; | |
| 42 | $this->log( | |
| 43 | "\nINFO: Queue email reminder (Limits: " . | |
| 44 | $configLimit . "|" . $configBatchSize . ") dependent on last run: " . | |
| 45 | $this->lastRun->format('Y-m-d H:i:s') | |
| 46 | ); | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | protected function log(string $message): void | |
| 51 | { | |
| 52 | if ($this->verbose) { | |
| 53 | \App::$log->info($message); | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | public function getCount() | |
| 58 | { | |
| 59 | return $this->count; | |
| 60 | } | |
| 61 | ||
| 62 | public function setLimit(int $limit): void | |
| 63 | { | |
| 64 | $this->limit = $limit; | |
| 65 | } | |
| 66 | ||
| 67 | public function setLoopCount(int $loopCount): void | |
| 68 | { | |
| 69 | $this->loopCount = $loopCount; | |
| 70 | } | |
| 71 | ||
| 72 | public function startProcessing(bool $commit): void | |
| 73 | { | |
| 74 | if ($commit) { | |
| 75 | (new MailRepository())->writeReminderLastRun($this->dateTime); | |
| 76 | } | |
| 77 | $this->writeMailReminderList($commit); | |
| 78 | $this->log("\nINFO: Last run " . $this->dateTime->format('Y-m-d H:i:s')); | |
| 79 | $this->log("\nSUMMARY: Queued mail reminder: " . $this->count); | |
| 80 | } | |
| 81 | ||
| 82 | protected function writeMailReminderList(bool $commit): void | |
| 83 | { | |
| 84 | // The offset parameter was removed here, because with each loop the processes are searched, which have not | |
| 85 | // been processed yet. An offset leads to the fact that with the renewed search the first results are skipped. | |
| 86 | $count = $this->writeByCallback($commit, function ($limit) { | |
| 87 | $processList = (new ProcessRepository())->readEmailReminderProcessListByInterval( | |
| 88 | $this->dateTime, | |
| 89 | $this->lastRun, | |
| 90 | $this->defaultReminderInMinutes, | |
| 91 | $limit, | |
| 92 | null, | |
| 93 | 2 | |
| 94 | ); | |
| 95 | return $processList; | |
| 96 | }); | |
| 97 | $this->count += $count; | |
| 98 | } | |
| 99 | ||
| 100 | protected function writeByCallback(bool $commit, \Closure $callback): int | |
| 101 | { | |
| 102 | $processCount = 0; | |
| 103 | while ($processCount < $this->limit) { | |
| 104 | $this->log("***Stack count***: " . $processCount); | |
| 105 | $processList = $callback($this->loopCount); | |
| 106 | if (0 == $processList->count()) { | |
| 107 | break; | |
| 108 | } | |
| 109 | ||
| 110 | foreach ($processList as $process) { | |
| 111 | $this->writeReminder($process, $commit, $processCount); | |
| 112 | $processCount++; | |
| 113 | } | |
| 114 | } | |
| 115 | return $processCount; | |
| 116 | } | |
| 117 | ||
| 118 | protected function writeReminder(Process $process, bool $commit, int $processCount): void | |
| 119 | { | |
| 120 | $department = (new DepartmentRepository())->readByScopeId($process->getScopeId(), 0); | |
| 121 | if ($process->getFirstClient()->hasEmail() && $department->hasMail()) { | |
| 122 | $config = (new ConfigRepository())->readEntity(); | |
| 123 | $collection = $this->getProcessListOverview($process, $config); | |
| 124 | ||
| 125 | $entity = (new Mail()) | |
| 126 | ->setTemplateProvider(new \BO\Zmsbackend\Helper\MailTemplateProvider($process)) | |
| 127 | ->toResolvedEntity($collection, $config, 'reminder'); | |
| 128 | ||
| 129 | $this->log( | |
| 130 | "INFO: $processCount. Create mail with process " . $process->getId() . | |
| 131 | " - " . $entity->subject . " for " . $process->getFirstAppointment() | |
| 132 | ); | |
| 133 | if ($commit) { | |
| 134 | $entity = (new MailRepository())->writeInQueue($entity, $this->dateTime); | |
| 135 | \BO\Zmsbackend\Log\Service\Log::writeProcessLog( | |
| 136 | "Write Reminder (\BO\Zmsbackend\Mail\Service\Mail::writeInQueue) $entity ", | |
| 137 | \BO\Zmsbackend\Log\Service\Log::ACTION_SEND_REMINDER, | |
| 138 | $process | |
| 139 | ); | |
| 140 | $this->log("INFO: Mail has been written in queue successfully with ID " . $entity->getId()); | |
| 141 | } | |
| 142 | } | |
| 143 | } | |
| 144 | ||
| 145 | protected function getProcessListOverview(Process $process, \BO\Zmsentities\Config $config): Collection | |
| 146 | { | |
| 147 | $collection = (new Collection())->addEntity($process); | |
| 148 | if (in_array(getenv('ZMS_ENV'), explode(',', $config->getPreference('appointments', 'enableSummaryByMail')))) { | |
| 149 | $processList = (new ProcessRepository())->readListByMailAndStatusList( | |
| 150 | $process->getFirstClient()->email, | |
| 151 | [ | |
| 152 | \BO\Zmsentities\Process::STATUS_CONFIRMED | |
| 153 | ], | |
| 154 | 2, | |
| 155 | 50 | |
| 156 | ); | |
| 157 | //add list of found processes without the main process | |
| 158 | $collection->addList($processList->withOutProcessId($process->getId())); | |
| 159 | } | |
| 160 | return $collection; | |
| 161 | } | |
| 162 | } |