Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       48.44%  | 
       31 / 64  | 
               | 
       50.00%  | 
       5 / 10  | 
       CRAP |         | 
       0.00%  | 
       0 / 1  | 
      
| SendNotificationReminder |         | 
       48.44%  | 
       31 / 64  | 
               | 
       50.00%  | 
       5 / 10  | 
       74.84 |         | 
       0.00%  | 
       0 / 1  | 
      
| __construct |         | 
       58.33%  | 
       7 / 12  | 
               | 
       0.00%  | 
       0 / 1  | 
       5.16 | |||
| 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%  | 
       3 / 3  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| writeNotificationReminderList |         | 
       100.00%  | 
       10 / 10  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| writeByCallback |         | 
       70.00%  | 
       7 / 10  | 
               | 
       0.00%  | 
       0 / 1  | 
       4.43 | |||
| writeReminder |         | 
       0.00%  | 
       0 / 16  | 
               | 
       0.00%  | 
       0 / 1  | 
       6 | |||
| deleteReminderTimestamp |         | 
       0.00%  | 
       0 / 8  | 
               | 
       0.00%  | 
       0 / 1  | 
       12 | |||
| 1 | <?php | 
| 2 | |
| 3 | namespace BO\Zmsdb\Helper; | 
| 4 | |
| 5 | use BO\Zmsdb\Log; | 
| 6 | use BO\Zmsdb\Config as ConfigRepository; | 
| 7 | |
| 8 | class SendNotificationReminder | 
| 9 | { | 
| 10 | public \DateTimeInterface $dateTime; | 
| 11 | |
| 12 | protected $reminderInSeconds; | 
| 13 | |
| 14 | protected $verbose = false; | 
| 15 | |
| 16 | protected $limit = 5000; | 
| 17 | |
| 18 | protected $loopCount = 500; | 
| 19 | |
| 20 | protected $count = 0; | 
| 21 | |
| 22 | public function __construct(\DateTimeInterface $now, $verbose = false) | 
| 23 | { | 
| 24 | $config = (new ConfigRepository())->readEntity(); | 
| 25 | $configLimit = $config->getPreference('notifications', 'sqlMaxLimit'); | 
| 26 | $configBatchSize = $config->getPreference('notifications', 'sqlBatchSize'); | 
| 27 | $this->limit = ($configLimit) ? $configLimit : $this->limit; | 
| 28 | $this->loopCount = ($configBatchSize) ? $configBatchSize : $this->loopCount; | 
| 29 | $this->dateTime = $now; | 
| 30 | if ($verbose) { | 
| 31 | $this->verbose = true; | 
| 32 | $this->log( | 
| 33 | "\nINFO: Send notification reminder (Limits: " . | 
| 34 | $configLimit . "|" . $configBatchSize . ") dependent on lead time" | 
| 35 | ); | 
| 36 | } | 
| 37 | } | 
| 38 | |
| 39 | protected function log($message) | 
| 40 | { | 
| 41 | if ($this->verbose) { | 
| 42 | error_log(trim($message)); | 
| 43 | } | 
| 44 | } | 
| 45 | |
| 46 | public function getCount() | 
| 47 | { | 
| 48 | return $this->count; | 
| 49 | } | 
| 50 | |
| 51 | public function setLimit($limit) | 
| 52 | { | 
| 53 | $this->limit = $limit; | 
| 54 | } | 
| 55 | |
| 56 | public function setLoopCount($loopCount) | 
| 57 | { | 
| 58 | $this->loopCount = $loopCount; | 
| 59 | } | 
| 60 | |
| 61 | public function startProcessing($commit) | 
| 62 | { | 
| 63 | $this->writeNotificationReminderList($commit); | 
| 64 | $this->log("\nINFO: Last run " . $this->dateTime->format('Y-m-d H:i:s')); | 
| 65 | $this->log("SUMMARY: Sent notification reminder: " . $this->count); | 
| 66 | } | 
| 67 | |
| 68 | protected function writeNotificationReminderList($commit) | 
| 69 | { | 
| 70 | // The offset parameter was removed here, because with each loop the processes are searched, which have not | 
| 71 | // been processed yet. An offset leads to the fact that with the renewed search the first results are skipped. | 
| 72 | $count = $this->writeByCallback($commit, function ($limit) { | 
| 73 | $processList = (new \BO\Zmsdb\Process())->readNotificationReminderProcessList( | 
| 74 | $this->dateTime, | 
| 75 | $limit, | 
| 76 | null, | 
| 77 | 1 | 
| 78 | ); | 
| 79 | return $processList; | 
| 80 | }); | 
| 81 | $this->count += $count; | 
| 82 | } | 
| 83 | |
| 84 | protected function writeByCallback($commit, \Closure $callback) | 
| 85 | { | 
| 86 | $processCount = 0; | 
| 87 | while ($processCount < $this->limit) { | 
| 88 | $this->log("***Stack count***: " . $processCount); | 
| 89 | $processList = $callback($this->loopCount); | 
| 90 | if (0 == $processList->count()) { | 
| 91 | break; | 
| 92 | } | 
| 93 | foreach ($processList as $process) { | 
| 94 | $this->writeReminder($process, $commit, $processCount); | 
| 95 | $processCount++; | 
| 96 | } | 
| 97 | } | 
| 98 | return $processCount; | 
| 99 | } | 
| 100 | |
| 101 | protected function writeReminder(\BO\Zmsentities\Process $process, $commit, $processCount) | 
| 102 | { | 
| 103 | $config = (new \BO\Zmsdb\Config())->readEntity(); | 
| 104 | $department = (new \BO\Zmsdb\Department())->readByScopeId($process->getScopeId(), 2); | 
| 105 | $entity = (new \BO\Zmsentities\Notification())->toResolvedEntity($process, $config, $department, 'reminder'); | 
| 106 | |
| 107 | $this->log("INFO: $processCount Create notification: $entity->message"); | 
| 108 | if ($commit) { | 
| 109 | $notification = (new \BO\Zmsdb\Notification())->writeInQueue($entity, $this->dateTime); | 
| 110 | Log::writeProcessLog( | 
| 111 | "Write Reminder (Notification::writeInQueue) $entity ", | 
| 112 | Log::ACTION_SEND_REMINDER, | 
| 113 | $process | 
| 114 | ); | 
| 115 | $this->log( | 
| 116 | "INFO: $processCount Notification has been written in queue successfully with ID " . | 
| 117 | $notification->getId() | 
| 118 | ); | 
| 119 | $this->deleteReminderTimestamp($process, $notification, $processCount, $commit); | 
| 120 | } | 
| 121 | } | 
| 122 | |
| 123 | protected function deleteReminderTimestamp( | 
| 124 | \BO\Zmsentities\Process $process, | 
| 125 | $notification, | 
| 126 | $processCount, | 
| 127 | $commit | 
| 128 | ) { | 
| 129 | if ($notification) { | 
| 130 | $process->reminderTimestamp = 0; | 
| 131 | if ($commit) { | 
| 132 | $process = (new \BO\Zmsdb\Process())->updateEntity($process, $this->dateTime); | 
| 133 | } | 
| 134 | $this->log("INFO: $processCount Updated $process->id - reminder timestamp removed"); | 
| 135 | } else { | 
| 136 | $this->log( | 
| 137 | "WARNING: $processCount Notification for $process->id not possible - no telephone or not enabled" | 
| 138 | ); | 
| 139 | } | 
| 140 | } | 
| 141 | } |