Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
97 / 97
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ScopeAvailabilityDay
100.00% covered (success)
100.00%
97 / 97
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%
28 / 28
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                'callcenter' => $slot->callcenter,
62                'occupiedCount' => 0,
63            ];
64        }
65
66        foreach ($processList as $process) {
67            $startTime = $process->getAppointments()->getFirst()->getStartTime()->format('H:i');
68            $endTime = $process->getAppointments()->getFirst()->getEndTimeWithCustomSlotTime($slotTimeInMinutes)->format('H:i');
69
70            $startDateTime = new \DateTime($startTime);
71            $endDateTime = new \DateTime($endTime);
72
73            foreach (array_keys($buckets) as $time) {
74                $slotDateTime = new \DateTime($time);
75                if ($slotDateTime >= $startDateTime && $slotDateTime < $endDateTime) {
76                    $buckets[$time]['occupiedCount']++;
77                }
78            }
79        }
80
81        uksort($buckets, function ($time1, $time2) {
82            return strtotime($time1) <=> strtotime($time2);
83        });
84
85        return $buckets;
86    }
87
88    protected static function getAvailabilityData($scopeId, $dateString)
89    {
90        $scope = static::getScope($scopeId);
91        $dateTime = new \BO\Zmsentities\Helper\DateTime($dateString);
92        $dateWithTime = $dateTime->setTime(\App::$now->format('H'), \App::$now->format('i'));
93        $availabilityList = static::readAvailabilityList($scopeId, $dateWithTime);
94        $processList = \App::$http
95            ->readGetResult('/scope/' . $scopeId . '/process/' . $dateWithTime->format('Y-m-d') . '/')
96                ->getCollection()
97                ->toQueueList($dateWithTime)
98                ->withoutStatus(['fake'])
99                ->toProcessList();
100        if (!$processList->count()) {
101            $processList = new \BO\Zmsentities\Collection\ProcessList();
102        }
103
104
105        $conflictList = static::readConflictList($scopeId, $dateWithTime);
106        $maxSlots = $availabilityList->getSummerizedSlotCount();
107        $busySlots = $availabilityList->getCalculatedSlotCount($processList);
108
109        return [
110            'slotBuckets' => static::getSlotBuckets($availabilityList, $processList),
111            'scope' => $scope,
112            'availabilityList' => $availabilityList->getArrayCopy(),
113            'conflicts' => ($conflictList) ? $conflictList
114                ->setConflictAmendment()
115                ->getArrayCopy() : [],
116            'processList' => $processList->getArrayCopy(),
117            'dateString' => $dateString,
118            'timestamp' => $dateWithTime->getTimestamp(),
119            'menuActive' => 'availability',
120            'maxWorkstationCount' => $availabilityList->getMaxWorkstationCount(),
121            'maxSlotsForAvailabilities' => $maxSlots,
122            'busySlotsForAvailabilities' => $busySlots,
123            'today' => \App::$now->getTimestamp()
124        ];
125    }
126
127    public static function readConflictList($scopeId, $dateTime)
128    {
129        $processConflictList = \App::$http
130            ->readGetResult('/scope/' . $scopeId . '/conflict/', [
131                'startDate' => $dateTime->format('Y-m-d'),
132                'endDate' => $dateTime->format('Y-m-d')
133            ])
134            ->getCollection();
135        return ($processConflictList) ? $processConflictList
136            ->sortByAppointmentDate()
137            ->withoutDublicatedConflicts()
138            ->toQueueList($dateTime)
139            ->withoutStatus(['fake', 'queued'])
140            ->toProcessList() : null;
141    }
142
143    public static function readAvailabilityList($scopeId, $dateTime)
144    {
145        try {
146            $availabilityList = \App::$http
147                ->readGetResult(
148                    '/scope/' . $scopeId . '/availability/',
149                    [
150                        'startDate' => $dateTime->format('Y-m-d'), //for skipping old availabilities
151                    ]
152                )
153                ->getCollection()->sortByCustomKey('startDate');
154        } catch (\BO\Zmsclient\Exception $exception) {
155            if ($exception->template != 'BO\Zmsapi\Exception\Availability\AvailabilityNotFound') {
156                throw $exception;
157            }
158            $availabilityList = new \BO\Zmsentities\Collection\AvailabilityList();
159        }
160        return $availabilityList->withDateTime($dateTime); //withDateTime to check if opened
161    }
162}