Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.98% covered (success)
98.98%
97 / 98
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScopeAvailabilityDay
98.98% covered (success)
98.98%
97 / 98
83.33% covered (warning)
83.33%
5 / 6
18
0.00% covered (danger)
0.00%
0 / 1
 readResponse
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
2.00
 getScope
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSlotBuckets
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
7
 getAvailabilityData
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
3
 readConflictList
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 readAvailabilityList
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * @package Zmsadmin
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsadmin;
9
10use BO\Zmsentities\Collection\AvailabilityList;
11
12class ScopeAvailabilityDay extends BaseController
13{
14    /**
15     * @SuppressWarnings(Param)
16     * @return String
17     */
18    public function readResponse(
19        \Psr\Http\Message\RequestInterface $request,
20        \Psr\Http\Message\ResponseInterface $response,
21        array $args
22    ) {
23        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 1])->getEntity();
24        if (!$workstation->getUseraccount()->hasPermissions(['availability'])) {
25            throw new \BO\Zmsentities\Exception\UserAccountMissingRights();
26        }
27        $data = static::getAvailabilityData(intval($args['id']), $args['date']);
28        $data['title'] = 'Behörden und Standorte - Öffnungszeiten';
29        $data['menuActive'] = 'owner';
30        $data['workstation'] = $workstation;
31        return \BO\Slim\Render::withHtml(
32            $response,
33            'page/availabilityday.twig',
34            $data
35        );
36    }
37
38    protected static function getScope($scopeId)
39    {
40        return \App::$http->readGetResult('/scope/' . $scopeId . '/', [
41            'resolveReferences' => 3
42        ])->getEntity();
43    }
44
45    protected static function getSlotBuckets($availabilityList, $processList)
46    {
47        $availability = $availabilityList->getFirst();
48
49        if (!$availability) {
50            return [];
51        }
52
53        $buckets = [];
54
55        $slotTimeInMinutes = $availability->getSlotTimeInMinutes();
56
57        foreach ($availabilityList->getSlotListByType('appointment') as $slot) {
58            $time = $slot->time;
59            $buckets[$time] = [
60                'time' => $time,
61                'timeString' => $slot->getTimeString(),
62                'public' => $slot->public,
63                'intern' => $slot->intern,
64                'occupiedCount' => 0,
65            ];
66        }
67
68        foreach ($processList as $process) {
69            $startTime = $process->getAppointments()->getFirst()->getStartTime()->format('H:i');
70            $endTime = $process->getAppointments()->getFirst()->getEndTimeWithCustomSlotTime($slotTimeInMinutes)->format('H:i');
71
72            $startDateTime = new \DateTime($startTime);
73            $endDateTime = new \DateTime($endTime);
74
75            foreach (array_keys($buckets) as $time) {
76                $slotDateTime = new \DateTime($time);
77                if ($slotDateTime >= $startDateTime && $slotDateTime < $endDateTime) {
78                    $buckets[$time]['occupiedCount']++;
79                }
80            }
81        }
82
83        uksort($buckets, function ($time1, $time2) {
84            return strtotime($time1) <=> strtotime($time2);
85        });
86
87        return $buckets;
88    }
89
90    protected static function getAvailabilityData($scopeId, $dateString)
91    {
92        $scope = static::getScope($scopeId);
93        $dateTime = new \BO\Zmsentities\Helper\DateTime($dateString);
94        $dateWithTime = $dateTime->setTime(\App::$now->format('H'), \App::$now->format('i'));
95        $availabilityList = static::readAvailabilityList($scopeId, $dateWithTime);
96        $processList = \App::$http
97            ->readGetResult('/scope/' . $scopeId . '/process/' . $dateWithTime->format('Y-m-d') . '/')
98                ->getCollection()
99                ->toQueueList($dateWithTime)
100                ->withoutStatus(['fake'])
101                ->toProcessList();
102        if (!$processList->count()) {
103            $processList = new \BO\Zmsentities\Collection\ProcessList();
104        }
105
106
107        $conflictList = static::readConflictList($scopeId, $dateWithTime);
108        $maxSlots = $availabilityList->getSummerizedSlotCount();
109        $busySlots = $availabilityList->getCalculatedSlotCount($processList);
110
111        return [
112            'slotBuckets' => static::getSlotBuckets($availabilityList, $processList),
113            'scope' => $scope,
114            'availabilityList' => $availabilityList->getArrayCopy(),
115            'conflicts' => ($conflictList) ? $conflictList
116                ->setConflictAmendment()
117                ->getArrayCopy() : [],
118            'processList' => $processList->getArrayCopy(),
119            'dateString' => $dateString,
120            'timestamp' => $dateWithTime->getTimestamp(),
121            'menuActive' => 'availability',
122            'maxWorkstationCount' => $availabilityList->getMaxWorkstationCount(),
123            'maxSlotsForAvailabilities' => $maxSlots,
124            'busySlotsForAvailabilities' => $busySlots,
125            'today' => \App::$now->getTimestamp()
126        ];
127    }
128
129    public static function readConflictList($scopeId, $dateTime)
130    {
131        $processConflictList = \App::$http
132            ->readGetResult('/scope/' . $scopeId . '/conflict/', [
133                'startDate' => $dateTime->format('Y-m-d'),
134                'endDate' => $dateTime->format('Y-m-d')
135            ])
136            ->getCollection();
137        return ($processConflictList) ? $processConflictList
138            ->sortByAppointmentDate()
139            ->withoutDublicatedConflicts()
140            ->toQueueList($dateTime)
141            ->withoutStatus(['fake', 'queued'])
142            ->toProcessList() : null;
143    }
144
145    public static function readAvailabilityList($scopeId, $dateTime)
146    {
147        try {
148            $availabilityList = \App::$http
149                ->readGetResult(
150                    '/scope/' . $scopeId . '/availability/',
151                    [
152                        'startDate' => $dateTime->format('Y-m-d'), //for skipping old availabilities
153                    ]
154                )
155                ->getCollection()->sortByCustomKey('startDate');
156        } catch (\BO\Zmsclient\Exception $exception) {
157            if ($exception->template != 'BO\Zmsapi\Exception\Availability\AvailabilityNotFound') {
158                throw $exception;
159            }
160            $availabilityList = new \BO\Zmsentities\Collection\AvailabilityList();
161        }
162        return $availabilityList->withDateTime($dateTime); //withDateTime to check if opened
163    }
164}