Lines
22.22%
2 / 9
Methods
0.00%
0 / 2
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | n/a | 0 / 0 | n/a | 0 / 0 | 2 | ||
| log | n/a | 0 / 0 | n/a | 0 / 0 | 1 | ||
| getCount | n/a | 0 / 0 | n/a | 0 / 0 | 1 | ||
| setLimit | n/a | 0 / 0 | n/a | 0 / 0 | 1 | ||
| setLoopCount | n/a | 0 / 0 | n/a | 0 / 0 | 1 | ||
| startProcessing | n/a | 0 / 0 | n/a | 0 / 0 | 1 | ||
| deallocateProcessList | n/a | 0 / 0 | n/a | 0 / 0 | 1 | ||
| deleteByCallback | n/a | 0 / 0 | n/a | 0 / 0 | 5 | ||
| handleProcess | n/a | 0 / 0 | n/a | 0 / 0 | 4 | ||
| writeDeallocatedProcess | n/a | 0 / 0 | n/a | 0 / 0 | 4 | ||
| [BO\Zmsbackend\Helper\VerboseCronLogTrait] resolveCronLogLevel | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 6 | ||
| [BO\Zmsbackend\Helper\VerboseCronLogTrait] writeVerboseCronLog | 50.00% | 2 / 4 | 0.00% | 0 / 1 | 2.50 | ||
| 8 | class AppointmentDeallocateByCron | |
| 9 | { | |
| 10 | use VerboseCronLogTrait; | |
| 11 | ||
| 12 | protected $verbose = false; | |
| 13 | ||
| 14 | protected $limit = 10000; | |
| 15 | ||
| 16 | protected $loopCount = 500; | |
| 17 | ||
| 18 | protected $time; | |
| 19 | ||
| 20 | protected $statuslist = [ | |
| 21 | "deleted" | |
| 22 | ]; | |
| 23 | ||
| 24 | protected $count = []; | |
| 25 | ||
| 26 | public function __construct(\DateTimeInterface $dateTime, bool $verbose = false) | |
| 27 | { | |
| 28 | $this->time = $dateTime; | |
| 29 | if ($verbose) { | |
| 30 | $this->log("INFO: Deallocate cancelled appointments by scopes deallocate preference"); | |
| 31 | $this->verbose = true; | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | protected function log(string $message, string $level = 'info'): void | |
| 36 | { | |
| 37 | $this->writeVerboseCronLog($message, $level); | |
| 38 | } | |
| 39 | ||
| 40 | public function getCount() | |
| 41 | { | |
| 42 | return $this->count; | |
| 43 | } | |
| 44 | ||
| 45 | public function setLimit(int $limit): void | |
| 46 | { | |
| 47 | $this->limit = $limit; | |
| 48 | } | |
| 49 | ||
| 50 | public function setLoopCount(int $loopCount): void | |
| 51 | { | |
| 52 | $this->loopCount = $loopCount; | |
| 53 | } | |
| 54 | ||
| 55 | public function startProcessing(bool $commit): void | |
| 56 | { | |
| 57 | $this->count['deallocated'] = 0; | |
| 58 | $this->deallocateProcessList($commit); | |
| 59 | $this->log("\nSUMMARY: Deallocated processes: " . var_export($this->count, true)); | |
| 60 | } | |
| 61 | ||
| 62 | protected function deallocateProcessList(bool $commit): void | |
| 63 | { | |
| 64 | $this->log("\nDeallocate cancelled processes"); | |
| 65 | $count = $this->deleteByCallback($commit, function ($limit, $offset) { | |
| 66 | $query = new \BO\Zmsbackend\Process\Service\Process(); | |
| 67 | $processList = $query->readDeallocateProcessList($this->time, $limit, $offset); | |
| 68 | return $processList; | |
| 69 | }); | |
| 70 | $this->count["deallocated"] += $count; | |
| 71 | } | |
| 72 | ||
| 73 | protected function deleteByCallback(bool $commit, \Closure $callback): int | |
| 74 | { | |
| 75 | $processCount = 0; | |
| 76 | $startposition = 0; | |
| 77 | while ($processCount < $this->limit) { | |
| 78 | $processList = $callback($this->loopCount, $startposition); | |
| 79 | if (0 == $processList->count()) { | |
| 80 | break; | |
| 81 | } | |
| 82 | foreach ($processList as $process) { | |
| 83 | if (!$this->handleProcess($process, $commit, $processCount)) { | |
| 84 | $startposition++; | |
| 85 | } | |
| 86 | $processCount++; | |
| 87 | } | |
| 88 | } | |
| 89 | return $processCount; | |
| 90 | } | |
| 91 | ||
| 92 | protected function handleProcess(\BO\Zmsentities\Process $process, bool $commit, int $processCount): int | |
| 93 | { | |
| 94 | $verbose = $this->verbose; | |
| 95 | if (in_array($process->status, $this->statuslist)) { | |
| 96 | $this->log("INFO: $processCount. Deallocate $process"); | |
| 97 | if ($commit) { | |
| 98 | $this->writeDeallocatedProcess($process); | |
| 99 | return 1; | |
| 100 | } | |
| 101 | } elseif ($verbose) { | |
| 102 | $this->log("INFO: Keep process $process->id"); | |
| 103 | } | |
| 104 | return 0; | |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * It is important to know that the slots in writeBlockedEntity are unblocked again, | |
| 109 | * but the ID remains blocked until the next day | |
| 110 | */ | |
| 111 | protected function writeDeallocatedProcess(\BO\Zmsentities\Process $process): void | |
| 112 | { | |
| 113 | $verbose = $this->verbose; | |
| 114 | $query = new \BO\Zmsbackend\Process\Service\Process(); | |
| 115 | if ($query->writeBlockedEntity($process, true)) { | |
| 116 | if ($verbose) { | |
| 117 | $this->log("INFO: Process $process->id successfully deallocated"); | |
| 118 | } | |
| 119 | } else { | |
| 120 | if ($verbose) { | |
| 121 | $this->log("WARN: Could not deallocate process '$process->id'!"); | |
| 122 | } | |
| 123 | } | |
| 124 | } | |
| 125 | } |
From BO\Zmsbackend\Helper\VerboseCronLogTrait
| 5 | trait VerboseCronLogTrait | |
| 6 | { | |
| 7 | /** | |
| 8 | * @return array{0: string, 1: string} | |
| 9 | */ | |
| 10 | protected static function resolveCronLogLevel(string $message, string $level): array | |
| 11 | { | |
| 12 | if (preg_match('/^(DEBUG|INFO|NOTICE|WARN(?:ING)?|ERROR|CRITICAL|ALERT|EMERGENCY):\s*(.*)$/s', $message, $matches)) { | |
| 13 | $level = $matches[1]; | |
| 14 | $message = $matches[2]; | |
| 15 | } | |
| 16 | $level = \BO\Slim\Bootstrap::normalizeLogLevelName($level); | |
| 17 | ||
| 18 | return [$level, $message]; | |
| 19 | } | |
| 20 | ||
| 21 | protected function writeVerboseCronLog(string $message, string $level = 'info'): void | |
| 22 | { | |
| 23 | if (!$this->verbose) { | |
| 24 | return; | |
| 25 | } | |
| 26 | [$level, $message] = self::resolveCronLogLevel($message, $level); | |
| 27 | \App::$log->{$level}($message); | |
| 28 | } | |
| 29 | } |