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