Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.19% covered (success)
96.19%
101 / 105
80.00% covered (warning)
80.00%
8 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
AvailabilityHistory
96.19% covered (success)
96.19%
101 / 105
80.00% covered (warning)
80.00%
8 / 10
18
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getEntityMapping
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
1
 addConditionScopeId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionChangedAtRange
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 addConditionAvailabilityId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addConditionAction
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 reverseEntityMapping
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
2
 postProcess
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
3
 asDateString
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
 asTimeString
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
3.33
1<?php
2
3declare(strict_types=1);
4
5namespace BO\Zmsbackend\Availability\Repository;
6
7use BO\Zmsentities\AvailabilityHistory as Entity;
8
9class AvailabilityHistory extends \BO\Zmsbackend\Query\Base implements \BO\Zmsbackend\Query\MappingInterface
10{
11    public const string TABLE = 'availability_history';
12
13    public const string ALIAS = 'availabilityHistory';
14
15    public const string QUERY_DELETE_OLDER_THAN = '
16        DELETE FROM availability_history
17        WHERE changed_at < :cutoff
18    ';
19
20    /** @var list<string> */
21    public const array ALLOWED_ACTIONS = Entity::ACTIONS;
22
23    /** @var int */
24    protected $resolveLevel = 0;
25
26    public function __construct(
27        mixed $queryType,
28        string $prefix = '',
29        string|false $name = false,
30        mixed $resolveLevel = null
31    ) {
32        parent::__construct($queryType, $prefix, $name, $resolveLevel);
33
34        if ($queryType === self::SELECT) {
35            $this->query->orderBy(self::ALIAS . '.changed_at', 'DESC');
36            $this->query->orderBy(self::ALIAS . '.id', 'DESC');
37        }
38    }
39
40    #[\Override]
41    public function getEntityMapping(): array
42    {
43        return [
44            'id' => self::ALIAS . '.id',
45            'scopeId' => self::ALIAS . '.scope_id',
46            'availabilityId' => self::ALIAS . '.availability_id',
47            'action' => self::ALIAS . '.action',
48            'startDate' => self::ALIAS . '.start_date',
49            'endDate' => self::ALIAS . '.end_date',
50            'everyXWeeks' => self::ALIAS . '.every_x_weeks',
51            'everyOtherWeek' => self::ALIAS . '.every_other_week',
52            'weekday' => self::ALIAS . '.weekday',
53            'startTime' => self::ALIAS . '.start_time',
54            'appointmentStartTime' => self::ALIAS . '.appointment_start_time',
55            'endTime' => self::ALIAS . '.end_time',
56            'appointmentEndTime' => self::ALIAS . '.appointment_end_time',
57            'timeSlot' => self::ALIAS . '.time_slot',
58            'workstationCount' => self::ALIAS . '.workstation_count',
59            'appointmentWorkstationCount' => self::ALIAS . '.appointment_workstation_count',
60            'comment' => self::ALIAS . '.comment',
61            'internetReduction' => self::ALIAS . '.internet_reduction',
62            'multipleSlotsAllowed' => self::ALIAS . '.multiple_slots_allowed',
63            'openFromDays' => self::ALIAS . '.open_from_days',
64            'openUntilDays' => self::ALIAS . '.open_until_days',
65            'version' => self::ALIAS . '.version',
66            'changedAt' => self::ALIAS . '.changed_at',
67            'changedBy' => self::ALIAS . '.changed_by',
68        ];
69    }
70
71    public function addConditionScopeId(int $scopeId): self
72    {
73        $this->query->where(self::ALIAS . '.scope_id', '=', $scopeId);
74        return $this;
75    }
76
77    public function addConditionChangedAtRange(\DateTimeInterface $from, \DateTimeInterface $to): self
78    {
79        $this->query->where(self::ALIAS . '.changed_at', '>=', $from->format('Y-m-d H:i:s'));
80        $this->query->where(self::ALIAS . '.changed_at', '<=', $to->format('Y-m-d H:i:s'));
81        return $this;
82    }
83
84    public function addConditionAvailabilityId(int $availabilityId): self
85    {
86        $this->query->where(self::ALIAS . '.availability_id', '=', $availabilityId);
87        return $this;
88    }
89
90    public function addConditionAction(string $action): self
91    {
92        $this->query->where(self::ALIAS . '.action', '=', $action);
93        return $this;
94    }
95
96    public function reverseEntityMapping(array $row): array
97    {
98        return [
99            'scope_id' => (int) $row['scope_id'],
100            'availability_id' => $row['availability_id'] !== null ? (int) $row['availability_id'] : null,
101            'action' => $row['action'],
102            'start_date' => (string) $row['start_date'],
103            'end_date' => (string) $row['end_date'],
104            'every_x_weeks' => (int) ($row['every_x_weeks'] ?? 0),
105            'every_other_week' => (int) ($row['every_other_week'] ?? 0),
106            'weekday' => (int) $row['weekday'],
107            'start_time' => (string) ($row['start_time'] ?? '00:00:00'),
108            'appointment_start_time' => (string) ($row['appointment_start_time'] ?? '00:00:00'),
109            'end_time' => (string) ($row['end_time'] ?? '00:00:00'),
110            'appointment_end_time' => (string) ($row['appointment_end_time'] ?? '00:00:00'),
111            'time_slot' => (string) ($row['time_slot'] ?? '00:00:00'),
112            'workstation_count' => (int) ($row['workstation_count'] ?? 0),
113            'appointment_workstation_count' => (int) ($row['appointment_workstation_count'] ?? 0),
114            'comment' => $row['comment'] ?? null,
115            'internet_reduction' => (int) ($row['internet_reduction'] ?? 0),
116            'multiple_slots_allowed' => (int) ($row['multiple_slots_allowed'] ?? 0),
117            'open_from_days' => (int) ($row['open_from_days'] ?? 0),
118            'open_until_days' => (int) ($row['open_until_days'] ?? 0),
119            'version' => (int) ($row['version'] ?? 1),
120            'changed_by' => $row['changed_by'],
121        ];
122    }
123
124    /**
125     * @param array $data
126     */
127    #[\Override]
128    public function postProcess($data): array
129    {
130        $intFields = [
131            'id',
132            'scopeId',
133            'everyXWeeks',
134            'everyOtherWeek',
135            'workstationCount',
136            'appointmentWorkstationCount',
137            'internetReduction',
138            'multipleSlotsAllowed',
139            'openFromDays',
140            'openUntilDays',
141            'version',
142        ];
143        foreach ($intFields as $field) {
144            $data[$this->getPrefixed($field)] = (int) $data[$this->getPrefixed($field)];
145        }
146
147        $availabilityId = $data[$this->getPrefixed('availabilityId')];
148        $data[$this->getPrefixed('availabilityId')] = $availabilityId !== null ? (int) $availabilityId : null;
149        $data[$this->getPrefixed('weekday')] = Entity::decodeWeekdayMask((int) $data[$this->getPrefixed('weekday')]);
150        $data[$this->getPrefixed('startDate')] = $this->asDateString($data[$this->getPrefixed('startDate')]);
151        $data[$this->getPrefixed('endDate')] = $this->asDateString($data[$this->getPrefixed('endDate')]);
152        $data[$this->getPrefixed('startTime')] = $this->asTimeString($data[$this->getPrefixed('startTime')]);
153        $data[$this->getPrefixed('appointmentStartTime')] = $this->asTimeString(
154            $data[$this->getPrefixed('appointmentStartTime')]
155        );
156        $data[$this->getPrefixed('endTime')] = $this->asTimeString($data[$this->getPrefixed('endTime')]);
157        $data[$this->getPrefixed('appointmentEndTime')] = $this->asTimeString(
158            $data[$this->getPrefixed('appointmentEndTime')]
159        );
160        $data[$this->getPrefixed('timeSlot')] = $this->asTimeString($data[$this->getPrefixed('timeSlot')]);
161
162        return $data;
163    }
164
165    private function asDateString(mixed $value): string
166    {
167        if ($value instanceof \DateTimeInterface) {
168            return $value->format('Y-m-d');
169        }
170
171        $value = (string) $value;
172        if (preg_match('/^\d{4}-\d{2}-\d{2}/', $value) === 1) {
173            return substr($value, 0, 10);
174        }
175
176        return $value;
177    }
178
179    private function asTimeString(mixed $value): string
180    {
181        if ($value instanceof \DateTimeInterface) {
182            return $value->format('H:i:s');
183        }
184
185        $value = (string) $value;
186        if (preg_match('/^\d{1,2}:\d{2}(:\d{2})?/', $value, $matches) === 1) {
187            return $matches[0];
188        }
189
190        return $value;
191    }
192}