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