Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
UnconfirmedAppointmentDeleteByCron
n/a
0 / 0
n/a
0 / 0
23
n/a
0 / 0
 __construct
n/a
0 / 0
n/a
0 / 0
1
 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
 deleteUnconfirmedProcesses
n/a
0 / 0
n/a
0 / 0
3
 deleteByCallback
n/a
0 / 0
n/a
0 / 0
6
 removeProcess
n/a
0 / 0
n/a
0 / 0
4
 deleteProcess
n/a
0 / 0
n/a
0 / 0
4
1<?php
2
3namespace BO\Zmsbackend\Helper;
4
5/**
6 * @codeCoverageIgnore
7 */
8class UnconfirmedAppointmentDeleteByCron
9{
10    use VerboseCronLogTrait;
11
12    protected $verbose = false;
13
14    protected $limit = 1000;
15
16    protected $loopCount = 100;
17
18    protected $time;
19
20    protected $now;
21
22    protected $statusListForDeletion = ['preconfirmed'];
23
24    protected $scopeList;
25
26    protected $count = [];
27
28    public function __construct(\DateTimeInterface $now, $verbose = false)
29    {
30        $this->now = $now;
31        $this->verbose = $verbose;
32        $this->scopeList = (new \BO\Zmsbackend\Scope\Service\Scope())->readList();
33    }
34
35    protected function log(string $message, string $level = 'info'): void
36    {
37        $this->writeVerboseCronLog($message, $level);
38    }
39
40    /** @psalm-api */
41    public function getCount()
42    {
43        return $this->count;
44    }
45
46    /**
47     * @psalm-api
48     */
49    public function setLimit($limit): void
50    {
51        $this->limit = $limit;
52    }
53
54    /**
55     * @psalm-api
56     */
57    public function setLoopCount($loopCount): void
58    {
59        $this->loopCount = $loopCount;
60    }
61
62    public function startProcessing($commit): void
63    {
64        $this->deleteUnconfirmedProcesses($commit);
65        $this->log("\nSUMMARY: Deleted processes: " . var_export($this->count, true));
66    }
67
68    protected function deleteUnconfirmedProcesses($commit): void
69    {
70        foreach ($this->scopeList as $scope) {
71            $count = $this->deleteByCallback($commit, function ($limit, $offset) use ($scope) {
72                $query = new \BO\Zmsbackend\Process\Service\Process();
73                $activationDuration = $scope->toProperty()->preferences->appointment->activationDuration->get();
74                $time = new \DateTimeImmutable();
75                $deleteFromTime = $time->setTimestamp(
76                    $this->now->getTimestamp() - ($activationDuration * 60)
77                );
78
79                if ($this->verbose) {
80                    $this->log(
81                        "INFO: Deleting appointments older than "
82                        . $deleteFromTime->format('c') . 'limit: ' . $limit
83                        . ' offset: ' . $offset
84                    );
85                }
86
87                $processList = $query->readUnconfirmedProcessList(
88                    $deleteFromTime,
89                    $scope->id,
90                    $limit,
91                    $offset
92                );
93                return $processList;
94            });
95            $this->count['preconfirmed'] = $count;
96        }
97    }
98
99    protected function deleteByCallback($commit, \Closure $callback): int
100    {
101        $processCount = 0;
102        $startposition = 0;
103        while ($processCount < $this->limit) {
104            $processList = $callback($this->loopCount, $startposition);
105            if (0 == $processList->count()) {
106                break;
107            }
108
109            if ($this->verbose) {
110                $this->log("INFO: ProcessList count " . $processList->count());
111            }
112
113            foreach ($processList as $process) {
114                if (!$this->removeProcess($process, $commit, $processCount)) {
115                    $startposition++;
116                }
117                $processCount++;
118            }
119        }
120        return $processCount;
121    }
122
123    protected function removeProcess(\BO\Zmsentities\Process $process, $commit): int
124    {
125        $verbose = $this->verbose;
126        if (in_array($process->status, $this->statusListForDeletion)) {
127            if ($commit) {
128                $this->deleteProcess($process);
129                return 1;
130            }
131        } elseif ($verbose) {
132            $this->log("INFO: Keep process $process");
133        }
134        return 0;
135    }
136
137    protected function deleteProcess(\BO\Zmsentities\Process $process): void
138    {
139        $verbose = $this->verbose;
140        $query = new \BO\Zmsbackend\Process\Service\Process();
141        if ($query->writeDeletedEntity($process->id)) {
142            if ($verbose) {
143                $this->log("INFO: Process $process->id successfully removed");
144            }
145        } else {
146            if ($verbose) {
147                $this->log("WARN: Could not remove process '$process->id'!");
148            }
149        }
150    }
151}