Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.71% covered (success)
92.71%
89 / 96
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExchangeCapacityscope
92.71% covered (success)
92.71%
89 / 96
40.00% covered (danger)
40.00%
2 / 5
21.17
0.00% covered (danger)
0.00%
0 / 1
 readEntity
90.20% covered (success)
90.20%
46 / 51
0.00% covered (danger)
0.00%
0 / 1
9.08
 readScopeSlotTimes
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
6
 normalizeScopeIds
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 readSubjectList
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
2
 readPeriodList
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace BO\Zmsbackend\Exchange\Service;
4
5use BO\Zmsbackend\Exchange\Repository\ExchangeCapacityscope as ExchangeCapacityscopeQuery;
6use BO\Zmsentities\Exchange;
7
8class ExchangeCapacityscope extends \BO\Zmsbackend\Base
9{
10    public function readEntity(
11        $subjectid,
12        ?\DateTimeInterface $datestart = null,
13        ?\DateTimeInterface $dateend = null,
14        $period = 'day'
15    ): Exchange {
16        if (trim((string) $subjectid) === '') {
17            throw new \InvalidArgumentException('Subject ID cannot be empty');
18        }
19
20        $subjectIdList = explode(',', $subjectid);
21        $firstScopeId = $subjectIdList[0];
22        $scope = (new \BO\Zmsbackend\Scope\Service\Scope())->readEntity($firstScopeId);
23        $entity = new Exchange();
24        $entity['title'] = "Terminkapazität " . $scope->contact->name . " " . $scope->shortName;
25
26        $unfiltered = $datestart === null || $dateend === null;
27        if ($unfiltered) {
28            $datestart = new \DateTimeImmutable('1970-01-01');
29            $dateend = new \DateTimeImmutable('2099-12-31');
30            $period = 'day';
31        } else {
32            $entity->setPeriod($datestart, $dateend, $period);
33        }
34
35        $entity->addDictionaryEntry('subjectid', 'string', 'Standort-ID', 'scope.id');
36        $dateDescription = $period === 'hour' ? 'Zeitpunkt' : 'Datum';
37        $entity->addDictionaryEntry('date', 'string', $dateDescription);
38        $entity->addDictionaryEntry('bookedcount', 'number', 'Gebuchte Kapazität insgesamt (Zeitschlitze)');
39        $entity->addDictionaryEntry('plannedcount', 'number', 'Geplante Kapazität insgesamt (Zeitschlitze)');
40        $entity->addDictionaryEntry('bookedminutes', 'number', 'Gebuchte Kapazität insgesamt (Minuten)');
41        $entity->addDictionaryEntry('plannedminutes', 'number', 'Geplante Kapazität insgesamt (Minuten)');
42        $entity->addDictionaryEntry('bookedcount_public', 'number', 'Gebuchte Kapazität Internet (Zeitschlitze)');
43        $entity->addDictionaryEntry('plannedcount_public', 'number', 'Geplante Kapazität Internet (Zeitschlitze)');
44        $entity->addDictionaryEntry('bookedminutes_public', 'number', 'Gebuchte Kapazität Internet (Minuten)');
45        $entity->addDictionaryEntry('plannedminutes_public', 'number', 'Geplante Kapazität Internet (Minuten)');
46
47        $entity['visualization']['xlabel'] = ["date"];
48        $entity['visualization']['ylabel'] = ["bookedcount", "plannedcount"];
49        $entity['visualization']['ylabelMinutes'] = ["bookedminutes", "plannedminutes"];
50        $entity['visualization']['ylabelPublic'] = ["bookedcount_public", "plannedcount_public"];
51        $entity['visualization']['ylabelMinutesPublic'] = ["bookedminutes_public", "plannedminutes_public"];
52        $entity['visualization']['allowCapacityChannel'] = true;
53
54        $scopeIds = $this->normalizeScopeIds($subjectIdList);
55        if ($scopeIds === []) {
56            throw new \InvalidArgumentException('Subject ID cannot be empty');
57        }
58
59        $dateStart = $unfiltered ? null : $datestart;
60        $dateEnd = $unfiltered ? null : $dateend;
61        $parameters = [];
62        $query = ExchangeCapacityscopeQuery::buildCapacityMetricsQuery(
63            $scopeIds,
64            $dateStart,
65            $dateEnd,
66            $period,
67            $parameters
68        );
69        foreach ($this->fetchAll($query, $parameters) as $entry) {
70            $entity->addDataSet(array_values($entry));
71        }
72
73        $entity['visualization']['scopeSlotTimes'] = $this->readScopeSlotTimes(
74            $scopeIds,
75            $dateStart,
76            $dateEnd
77        );
78
79        return $entity;
80    }
81
82    /**
83     * @param array<int, int> $scopeIds
84     * @return array<int, array{id: string, name: string, slotTimeInMinutes: int}>
85     */
86    private function readScopeSlotTimes(
87        array $scopeIds,
88        ?\DateTimeInterface $dateStart,
89        ?\DateTimeInterface $dateEnd
90    ): array {
91        $parameters = [];
92        $query = ExchangeCapacityscopeQuery::buildScopeSlotTimeQuery(
93            $scopeIds,
94            $dateStart,
95            $dateEnd,
96            $parameters
97        );
98        $slotTimes = [];
99        foreach ($this->fetchAll($query, $parameters) as $entry) {
100            $scopeId = (string) ($entry['subjectid'] ?? '');
101            $minutes = (int) ($entry['slotminutes'] ?? 0);
102            if ($scopeId === '' || $minutes <= 0 || isset($slotTimes[$scopeId])) {
103                continue;
104            }
105            $name = trim((string) ($entry['scopename'] ?? ''));
106            $slotTimes[$scopeId] = [
107                'id' => $scopeId,
108                'name' => $name !== '' ? $name : 'Standort ' . $scopeId,
109                'slotTimeInMinutes' => $minutes,
110            ];
111        }
112
113        return array_values($slotTimes);
114    }
115
116    /**
117     * @param array<int, string> $subjectIdList
118     * @return array<int, int>
119     */
120    private function normalizeScopeIds(array $subjectIdList): array
121    {
122        $scopeIds = [];
123        foreach ($subjectIdList as $scopeId) {
124            $scopeId = trim($scopeId);
125            if (preg_match('/^\d+$/', $scopeId) !== 1) {
126                continue;
127            }
128            $scopeIds[] = (int) $scopeId;
129        }
130
131        return array_values(array_unique($scopeIds));
132    }
133
134    public function readSubjectList(): Exchange
135    {
136        $raw = $this->getReader()->fetchAll(
137            ExchangeCapacityscopeQuery::QUERY_CAPACITY_REPORT_SCOPE_SUBJECT_LIST,
138            []
139        );
140        $entity = new Exchange();
141        $entity['title'] = "Terminkapazität ";
142        $entity->addDictionaryEntry('subject', 'string', 'Standort ID', 'scope.id');
143        $entity->addDictionaryEntry('periodstart', 'string', 'Datum von');
144        $entity->addDictionaryEntry('periodend', 'string', 'Datum bis');
145        $entity->addDictionaryEntry('description', 'string', 'Beschreibung des Standortes');
146        foreach ($raw as $entry) {
147            $entity->addDataSet(array_values($entry));
148        }
149
150        return $entity;
151    }
152
153    /**
154     * Aggregate period placeholder required by ExchangeSubject interface.
155     *
156     * @SuppressWarnings(Unused)
157     */
158    public function readPeriodList($subjectid, $period = 'day'): Exchange
159    {
160        $entity = new Exchange();
161        $entity['title'] = "Terminkapazität ";
162        $entity->addDictionaryEntry('id', 'string', 'Gesamter Zeitraum', 'useraccount.permissions.superuser');
163        $entity->addDataSet(["_"]);
164
165        return $entity;
166    }
167}