Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.72% covered (success)
96.72%
59 / 61
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScopeAvailabilityMonth
96.72% covered (success)
96.72%
59 / 61
50.00% covered (danger)
50.00%
1 / 2
7
0.00% covered (danger)
0.00%
0 / 1
 readResponse
95.74% covered (success)
95.74%
45 / 47
0.00% covered (danger)
0.00%
0 / 1
4
 getAvailabilityList
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * Return availability list by scope in month view.
5 *
6 * @copyright 2018 BerlinOnline Stadtportal GmbH & Co. KG
7 */
8
9namespace BO\Zmsadmin;
10
11use BO\Mellon\Validator;
12use BO\Zmsentities\Calendar;
13use BO\Zmsentities\Closure;
14use BO\Zmsentities\Collection\AvailabilityList;
15use BO\Zmsentities\Collection\ProcessList;
16
17class ScopeAvailabilityMonth extends BaseController
18{
19    /**
20     * Return response.
21     *
22     * @SuppressWarnings(Param)
23     *
24     * @param \Psr\Http\Message\RequestInterface  $request  The request instance
25     * @param \Psr\Http\Message\ResponseInterface $response The response instance
26     * @param array                               $args     The path arguments
27     *
28     * @return string
29     */
30    #[\Override]
31    public function readResponse(
32        \Psr\Http\Message\RequestInterface $request,
33        \Psr\Http\Message\ResponseInterface $response,
34        array $args
35    ) {
36        $workstation = \App::$http->readGetResult('/workstation/', [
37            'resolveReferences' => 1,
38            'gql' => Helper\GraphDefaults::getWorkstation()
39        ])->getEntity();
40        if (!$workstation->getUseraccount()->hasPermissions(['availability'])) {
41            throw new \BO\Zmsentities\Exception\UserAccountMissingRights();
42        }
43        $dateTime = (isset($args['date'])) ? new \BO\Zmsentities\Helper\DateTime($args['date']) : \App::$now;
44        $startDate = $dateTime->modify('first day of this month');
45        $endDate = $dateTime->modify('last day of this month');
46
47        $scopeId = Validator::value($args['id'])->isNumber()->getValue();
48        $scope = \App::$http->readGetResult('/scope/' . $scopeId . '/', [
49            'resolveReferences' => 1
50        ])->getEntity();
51        $calendar = new Helper\Calendar($dateTime->format('Y-m-d'));
52        $scopeList = (new \BO\Zmsentities\Collection\ScopeList())->addEntity($scope);
53        $month = $calendar->readMonthListByScopeList($scopeList, 'intern', 0)->getFirst();
54
55        $availabilityList = $this->getAvailabilityList($scope, $startDate, $endDate);
56        $processConflictList = \App::$http
57            ->readGetResult('/scope/' . $scope->getId() . '/conflict/', [
58                'startDate' => \App::$now->format('Y-m-d'),
59                'endDate' => $endDate->format('Y-m-d'),
60            ])
61            ->getCollection();
62        $processConflictList = $processConflictList ?
63            $processConflictList->toConflictListByDay() :
64            new \BO\Zmsentities\Collection\ProcessList();
65
66        return \BO\Slim\Render::withHtml(
67            $response,
68            'page/availabilityMonth.twig',
69            array(
70                'availabilityList' => $availabilityList,
71                'conflicts' => $processConflictList,
72                'calendar' => $calendar,
73                'dayoffList' => $scope->getDayoffList(),
74                'closureList' => $scope->getClosureList(),
75                'dateTime' => $dateTime,
76                'timestamp' => $dateTime->getTimeStamp(),
77                'month' => $month,
78                'scope' => $scope,
79                'menuActive' => 'owner',
80                'title' => 'Behörden und Standorte - Öffnungszeiten',
81                'workstation' => $workstation,
82                'baseMonthString' => $startDate->format('m'),
83                'baseYearString' => $endDate->format('Y'),
84                'baseMonth_timestamp' => $startDate->getTimeStamp(),
85            )
86        );
87    }
88
89    protected function getAvailabilityList($scope, $startDate, $endDate)
90    {
91        try {
92            $availabilityList = \App::$http
93                ->readGetResult(
94                    '/scope/' . $scope->getId() . '/availability/',
95                    [
96                        'startDate' => $startDate->format('Y-m-d'),
97                        'endDate' => $endDate->format('Y-m-d')
98                    ]
99                )
100                ->getCollection();
101        } catch (\BO\Zmsclient\Exception $exception) {
102            if ($exception->template != 'BO\Zmsapi\Exception\Availability\AvailabilityNotFound') {
103                throw $exception;
104            }
105            $availabilityList = new \BO\Zmsentities\Collection\AvailabilityList();
106        }
107        return $availabilityList;
108    }
109}