Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
46.84% |
37 / 79 |
|
50.00% |
5 / 10 |
CRAP | |
0.00% |
0 / 1 |
SendMailReminder | |
46.84% |
37 / 79 |
|
50.00% |
5 / 10 |
94.73 | |
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 / 13 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb\Helper; |
4 | |
5 | use BO\Zmsdb\Log; |
6 | use BO\Zmsdb\Process as ProcessRepository; |
7 | use BO\Zmsdb\Mail as MailRepository; |
8 | use BO\Zmsdb\Department as DepartmentRepository; |
9 | use BO\Zmsdb\Config as ConfigRepository; |
10 | use BO\Zmsentities\Collection\ProcessList as Collection; |
11 | use BO\Zmsentities\Mail; |
12 | use BO\Zmsentities\Process; |
13 | |
14 | class SendMailReminder |
15 | { |
16 | protected $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, $sendReminderBeforeMinutes, $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: Send 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($message) |
51 | { |
52 | if ($this->verbose) { |
53 | error_log($message); |
54 | } |
55 | } |
56 | |
57 | public function getCount() |
58 | { |
59 | return $this->count; |
60 | } |
61 | |
62 | public function setLimit($limit) |
63 | { |
64 | $this->limit = $limit; |
65 | } |
66 | |
67 | public function setLoopCount($loopCount) |
68 | { |
69 | $this->loopCount = $loopCount; |
70 | } |
71 | |
72 | public function startProcessing($commit) |
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: Sent mail reminder: " . $this->count); |
80 | } |
81 | |
82 | protected function writeMailReminderList($commit) |
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($commit, \Closure $callback) |
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, $commit, $processCount) |
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\Zmsdb\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 | Log::writeProcessLog( |
136 | "Write Reminder (Mail::writeInQueue) $entity ", |
137 | 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, $config) |
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 | Process::STATUS_CONFIRMED, |
153 | Process::STATUS_PICKUP |
154 | ], |
155 | 2, |
156 | 50 |
157 | ); |
158 | //add list of found processes without the main process |
159 | $collection->addList($processList->withOutProcessId($process->getId())); |
160 | } |
161 | return $collection; |
162 | } |
163 | } |