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