Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
AvailabilityDeleteByCron
n/a
0 / 0
n/a
0 / 0
13
n/a
0 / 0
 __construct
n/a
0 / 0
n/a
0 / 0
2
 startProcessing
n/a
0 / 0
n/a
0 / 0
5
 deleteAvailability
n/a
0 / 0
n/a
0 / 0
6
1<?php
2
3namespace BO\Zmsbackend\Helper;
4
5/**
6 * @codeCoverageIgnore
7 */
8class AvailabilityDeleteByCron
9{
10    protected $verbose = false;
11    protected $query;
12
13    public function __construct($verbose = false)
14    {
15        $this->query = new \BO\Zmsbackend\Availability\Service\Availability();
16        if ($verbose) {
17            $this->verbose = true;
18        }
19    }
20
21    public function startProcessing(\DateTimeImmutable $datetime, $commit = false): void
22    {
23        $availabilityList = $this->query->readAvailabilityListBefore($datetime);
24        if ($this->verbose) {
25            \App::$log->info('Reading availability list');
26        }
27        foreach ($availabilityList as $availability) {
28            if ($commit) {
29                $this->deleteAvailability($availability->getId());
30            } elseif ($this->verbose) {
31                \App::$log->info('Would remove availability', ['availability' => (string) $availability]);
32            }
33        }
34    }
35
36    protected function deleteAvailability(string $availabilityId): void
37    {
38        $entity = null;
39        try {
40            $entity = $this->query->readEntity($availabilityId, 1);
41        } catch (\Throwable $exception) {
42            // Still attempt delete; history write needs a resolved entity.
43            \App::$log->warning('availability_history pre-delete read failed', [
44                'availabilityId' => $availabilityId,
45                'exception' => $exception->getMessage(),
46            ]);
47        }
48
49        if ($this->query->deleteEntity($availabilityId)) {
50            if ($entity && $entity->hasId()) {
51                (new \BO\Zmsbackend\Availability\Service\AvailabilityHistory())
52                    ->writeDeleted($entity, 'cron');
53            } else {
54                \App::$log->warning('availability_history skipped: entity unavailable before cron delete', [
55                    'availabilityId' => $availabilityId,
56                ]);
57            }
58            if ($this->verbose) {
59                \App::$log->info('Availability successfully removed', ['availabilityId' => $availabilityId]);
60            }
61        } else {
62            \App::$log->warning('Could not remove availability', ['availabilityId' => $availabilityId]);
63        }
64    }
65}