Lines 91.22% 52 / 57
Functions and Methods 44.44% 4 / 9
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
OverviewCalendar 91.22% 52 / 57 44.44% 4 / 9 17.20 0.00% 0 / 1
 formatDateTime 100.00% 3 / 3 100.00% 1 / 1 2
 insert 100.00% 7 / 7 100.00% 1 / 1 1
 updateByProcess 100.00% 6 / 6 100.00% 1 / 1 1
 readMaxUpdated 88.88% 8 / 9 0.00% 0 / 1 3.01
 readMaxUpdatedGlobal 83.33% 5 / 6 0.00% 0 / 1 3.04
 readRange 87.50% 7 / 8 0.00% 0 / 1 2.01
 readRangeUpdated 88.88% 8 / 9 0.00% 0 / 1 2.01
 readChangedProcessIdsSince 83.33% 5 / 6 0.00% 0 / 1 2.02
 deleteOlderThan 100.00% 3 / 3 100.00% 1 / 1 1
1<?php
2
3namespace BO\Zmsbackend\OverviewCalendar\Service;
4
5use BO\Zmsbackend\Calendar\Repository\OverviewCalendar as Calender;
6use DateInterval;
7use DateTimeImmutable;
8use DateTimeInterface;
9
10class OverviewCalendar extends \BO\Zmsbackend\Base
11{
12    private function formatDateTime(\DateTimeInterface|string $dt): string
13    {
14        return ($dt instanceof \DateTimeInterface)
15            ? $dt->format('Y-m-d H:i:s')
16            : $dt;
17    }
18
19    public function insert(
20        int $scopeId,
21        int $processId,
22        string $status,
23        \DateTimeInterface $startsAt,
24        \DateTimeInterface $endsAt
25    ): void {
26        $this->perform(Calender::INSERT_ONE, [
27            'scope_id' => $scopeId,
28            'process_id' => $processId,
29            'status' => $status,
30            'starts_at' => $this->formatDateTime($startsAt),
31            'ends_at' => $this->formatDateTime($endsAt),
32        ]);
33    }
34
35    public function updateByProcess(
36        int $processId,
37        int $scopeId,
38        \DateTimeInterface $startsAt,
39        \DateTimeInterface $endsAt
40    ): bool {
41        return (bool)$this->perform(\BO\Zmsbackend\Calendar\Repository\OverviewCalendar::UPDATE_BY_PROCESS, [
42            'process_id' => $processId,
43            'scope_id' => $scopeId,
44            'starts_at' => $this->formatDateTime($startsAt),
45            'ends_at' => $this->formatDateTime($endsAt),
46        ]);
47    }
48
49    public function readMaxUpdated(array $scopeIds, string $from, string $until): ?string
50    {
51        if (empty($scopeIds)) {
52            return null;
53        }
54        $in = implode(',', array_map('intval', $scopeIds));
55        $sql = sprintf(Calender::SELECT_MAX_UPDATED, $in);
56
57        $val = $this->fetchValue($sql, [
58            'from' => $from,
59            'until' => $until,
60        ]);
61
62        return $val ?: null;
63    }
64
65    public function readMaxUpdatedGlobal(array $scopeIds): ?string
66    {
67        if (empty($scopeIds)) {
68            return null;
69        }
70        $in = implode(',', array_map('intval', $scopeIds));
71        $sql = sprintf(Calender::SELECT_MAX_UPDATED_GLOBAL, $in);
72        $val = $this->fetchValue($sql, []);
73        return $val ?: null;
74    }
75
76    public function readRange(array $scopeIds, string $from, string $until): array
77    {
78        if (empty($scopeIds)) {
79            return [];
80        }
81        $in = implode(',', array_map('intval', $scopeIds));
82        $sql = sprintf(Calender::SELECT_RANGE, $in);
83
84        return $this->fetchAll($sql, [
85            'from' => $from,
86            'until' => $until,
87        ]);
88    }
89
90    public function readRangeUpdated(array $scopeIds, string $from, string $until, string $updatedAfter): array
91    {
92        if (empty($scopeIds)) {
93            return [];
94        }
95        $in = implode(',', array_map('intval', $scopeIds));
96        $sql = sprintf(Calender::SELECT_RANGE_UPDATED, $in);
97
98        return $this->fetchAll($sql, [
99            'from' => $from,
100            'until' => $until,
101            'updatedAfter' => $updatedAfter,
102        ]);
103    }
104
105    public function readChangedProcessIdsSince(array $scopeIds, string $updatedAfter): array
106    {
107        if (empty($scopeIds)) {
108            return [];
109        }
110        $in  = implode(',', array_map('intval', $scopeIds));
111        $sql = sprintf(Calender::SELECT_CHANGED_PIDS_SINCE, $in);
112        $rows = $this->fetchAll($sql, ['updatedAfter' => $updatedAfter]);
113        return array_map(fn($r) => (int)$r['process_id'], $rows);
114    }
115
116    public function deleteOlderThan(\DateTimeInterface|string $threshold): bool
117    {
118        return (bool)$this->perform(Calender::DELETE_ALL_BEFORE_END, [
119            'threshold' => $this->formatDateTime($threshold),
120        ]);
121    }
122}