Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.23% covered (warning)
85.23%
75 / 88
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReportWaitingService
85.23% covered (warning)
85.23%
75 / 88
14.29% covered (danger)
14.29%
1 / 7
28.18
0.00% covered (danger)
0.00%
0 / 1
 getExchangeWaitingData
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getExchangeWaitingForDateRange
76.47% covered (warning)
76.47%
13 / 17
0.00% covered (danger)
0.00%
0 / 1
5.33
 getExchangeWaitingForPeriod
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
2.01
 getWaitingPeriod
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
2.26
 fetchAndCombineDataFromYears
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
0.00%
0 / 1
6
 createFilteredExchangeWaiting
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
3.01
 prepareDownloadArgs
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
5.39
1<?php
2
3/**
4 * @package Zmsstatistic
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsstatistic\Service;
9
10use BO\Zmsentities\Day;
11use BO\Zmsstatistic\Helper\ReportHelper;
12use DateTime;
13use Exception;
14
15class ReportWaitingService
16{
17    protected $hashset = [
18        'waitingcount',
19        'waitingtime',
20        'waitingcalculated',
21        'waitingcount_termin',
22        'waitingtime_termin',
23        'waitingcalculated_termin',
24        'waytime',
25        'waytime_termin',
26    ];
27
28    protected $groupfields = [
29        'date',
30        'hour'
31    ];
32
33    /**
34     * Get exchange waiting data based on date range or period
35     */
36    public function getExchangeWaitingData(string $scopeId, ?array $dateRange, array $args): mixed
37    {
38        if ($dateRange) {
39            return $this->getExchangeWaitingForDateRange($scopeId, $dateRange);
40        }
41
42        return isset($args['period'])
43            ? $this->getExchangeWaitingForPeriod($scopeId, $args['period'])
44            : null;
45    }
46
47    /**
48     * Get exchange waiting data for a specific date range
49     */
50    public function getExchangeWaitingForDateRange(string $scopeId, array $dateRange): mixed
51    {
52        if (!isset($dateRange['from']) || !isset($dateRange['to'])) {
53            return null;
54        }
55
56        $fromDate = $dateRange['from'];
57        $toDate = $dateRange['to'];
58
59        try {
60            $reportHelper = new ReportHelper();
61            $years = $reportHelper->getYearsForDateRange($fromDate, $toDate);
62            $combinedData = $this->fetchAndCombineDataFromYears($scopeId, $years, $fromDate, $toDate);
63
64            if (empty($combinedData['data'])) {
65                return null;
66            }
67
68            return $this->createFilteredExchangeWaiting(
69                $combinedData['entity'],
70                $combinedData['data'],
71                $fromDate,
72                $toDate
73            );
74        } catch (Exception $exception) {
75            return null;
76        }
77    }
78
79    /**
80     * Get exchange waiting data for a specific period (legacy functionality)
81     */
82    public function getExchangeWaitingForPeriod(string $scopeId, string $period): mixed
83    {
84        try {
85            $exchangeWaiting = \App::$http
86                ->readGetResult('/warehouse/waitingscope/' . $scopeId . '/' . $period . '/')
87                ->getEntity()
88                ->toGrouped($this->groupfields, $this->hashset)
89                ->withMaxByHour($this->hashset)
90                ->withMaxAndAverageFromWaitingTime();
91
92            // Apply extra ReportHelper processing
93            $exchangeWaiting = ReportHelper::withMaxAndAverage($exchangeWaiting, 'waitingtime');
94            $exchangeWaiting = ReportHelper::withMaxAndAverage($exchangeWaiting, 'waitingtime_termin');
95            $exchangeWaiting = ReportHelper::withMaxAndAverage($exchangeWaiting, 'waytime');
96            $exchangeWaiting = ReportHelper::withMaxAndAverage($exchangeWaiting, 'waytime_termin');
97
98            return $exchangeWaiting;
99        } catch (Exception $exception) {
100            return null;
101        }
102    }
103
104    /**
105     * Get waiting period data for the current scope
106     */
107    public function getWaitingPeriod(string $scopeId): mixed
108    {
109        try {
110            return \App::$http
111                ->readGetResult('/warehouse/waitingscope/' . $scopeId . '/')
112                ->getEntity();
113        } catch (Exception $exception) {
114            return null;
115        }
116    }
117
118    /**
119     * Fetch and combine data from multiple years
120     */
121    private function fetchAndCombineDataFromYears(string $scopeId, array $years, string $fromDate, string $toDate): array
122    {
123        $combinedData = [];
124        $baseEntity = null;
125
126        foreach ($years as $year) {
127            try {
128                $exchangeWaiting = \App::$http
129                    ->readGetResult(
130                        '/warehouse/waitingscope/' . $scopeId . '/' . $year . '/',
131                        [
132                            'groupby' => 'day',
133                            'fromDate' => $fromDate,
134                            'toDate' => $toDate
135                        ]
136                    )
137                    ->getEntity();
138
139                if (isset($exchangeWaiting->data) && is_array($exchangeWaiting->data)) {
140                    $combinedData = array_merge($combinedData, $exchangeWaiting->data);
141                }
142
143                if ($baseEntity === null) {
144                    unset($exchangeWaiting->data);
145                    $baseEntity = $exchangeWaiting;
146                }
147            } catch (Exception $exception) {
148                // continue with other years
149            }
150        }
151
152        return [
153            'entity' => $baseEntity,
154            'data' => $combinedData
155        ];
156    }
157
158    /**
159     * Create filtered exchange waiting with updated properties
160     */
161    private function createFilteredExchangeWaiting(
162        $exchangeWaitingBasic,
163        array $filteredData,
164        string $fromDate,
165        string $toDate
166    ): mixed {
167        $exchangeWaiting = $exchangeWaitingBasic;
168        $exchangeWaiting->data = $filteredData;
169
170        if (!isset($exchangeWaiting->period)) {
171            $exchangeWaiting->period = 'day';
172        }
173
174        $exchangeWaiting->firstDay = (new Day())->setDateTime(new DateTime($fromDate));
175        $exchangeWaiting->lastDay = (new Day())->setDateTime(new DateTime($toDate));
176
177        if (!empty($filteredData)) {
178            $exchangeWaiting = $exchangeWaiting
179                ->toGrouped($this->groupfields, $this->hashset);
180
181            $exchangeWaiting = $exchangeWaiting->withMaxByHour($this->hashset)
182                ->withMaxAndAverageFromWaitingTime();
183
184            $exchangeWaiting = ReportHelper::withMaxAndAverage($exchangeWaiting, 'waitingtime');
185            $exchangeWaiting = ReportHelper::withMaxAndAverage($exchangeWaiting, 'waitingtime_termin');
186            $exchangeWaiting = ReportHelper::withMaxAndAverage($exchangeWaiting, 'waytime');
187            $exchangeWaiting = ReportHelper::withMaxAndAverage($exchangeWaiting, 'waytime_termin');
188
189            return $exchangeWaiting;
190        }
191
192        return $exchangeWaiting->toHashed();
193    }
194
195    /**
196     * Prepare download arguments for waiting report
197     */
198    public function prepareDownloadArgs(
199        array $args,
200        mixed $exchangeWaiting,
201        ?array $dateRange,
202        array $selectedScopes = []
203    ): array {
204        $args['category'] = 'waitingscope';
205
206        if ($dateRange) {
207            $args['period'] = $dateRange['from'] . '_' . $dateRange['to'];
208        }
209
210        if (!empty($selectedScopes)) {
211            $args['selectedScopes'] = $selectedScopes;
212        }
213
214        if ($exchangeWaiting && count($exchangeWaiting->data)) {
215            $args['reports'][] = $exchangeWaiting;
216        }
217
218        return $args;
219    }
220}