Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.77% covered (success)
90.77%
59 / 65
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Department
90.77% covered (success)
90.77%
59 / 65
20.00% covered (danger)
20.00%
1 / 5
22.38
0.00% covered (danger)
0.00%
0 / 1
 readResponse
94.29% covered (success)
94.29%
33 / 35
0.00% covered (danger)
0.00%
0 / 1
9.02
 withCleanupLinks
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
6.05
 withCleanupDayoffs
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
 withEmailReminderDefaultValues
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
5.02
 writeUpdatedEntity
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @package 115Mandant
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsadmin;
9
10use BO\Zmsentities\Department as Entity;
11use BO\Zmsentities\Schema\Schema;
12use BO\Zmsentities\Exception\UserAccountMissingRights;
13use BO\Mellon\Validator;
14
15class Department extends BaseController
16{
17    /**
18     * @return \Psr\Http\Message\ResponseInterface
19     */
20    #[\Override]
21    public function readResponse(
22        \Psr\Http\Message\RequestInterface $request,
23        \Psr\Http\Message\ResponseInterface $response,
24        array $args
25    ): \Psr\Http\Message\ResponseInterface {
26        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity();
27        if (!$workstation->getUseraccount()->hasPermissions(['department'])) {
28            throw new UserAccountMissingRights();
29        }
30        $success = $request->getAttribute('validator')->getParameter('success')->isString()->getValue();
31        $entityId = Validator::value($args['id'])->isNumber()->getValue();
32        $entity = \App::$http->readGetResult('/department/' . $entityId . '/', ['resolveReferences' => 1])->getEntity();
33        $organisation = \App::$http->readGetResult('/department/' . $entityId . '/organisation/')->getEntity();
34        $input = $request->getParsedBody();
35        $result = null;
36
37        if ($request->getMethod() === 'POST') {
38            $input = $this->withCleanupLinks($input);
39            $input = $this->withCleanupDayoffs($input);
40            $input = $this->withEmailReminderDefaultValues($input);
41            $result = $this->writeUpdatedEntity($input, $entityId);
42            if ($result instanceof Entity) {
43                return \BO\Slim\Render::redirect('department', ['id' => $entityId], [
44                    'success' => 'department_saved'
45                ]);
46            }
47        }
48
49        // If there was an error, use the submitted input data for form re-population
50        $departmentData = (isset($result) && is_array($result) && isset($result['data']))
51            ? array_merge((new Schema($entity))->toSanitizedArray(), $input ?? [])
52            : (new Schema($entity))->toSanitizedArray();
53
54        return \BO\Slim\Render::withHtml(
55            $response,
56            'page/department.twig',
57            array(
58                'title' => 'Behörde bearbeiten',
59                'workstation' => $workstation,
60                'organisation' => $organisation,
61                'department' => $departmentData,
62                'hasAccess' => $entity->hasAccess($workstation->getUseraccount()),
63                'menuActive' => 'owner',
64                'success' => $success,
65                'exception' => (isset($result) && !($result instanceof Entity)) ? $result : null,
66            )
67        );
68    }
69
70    protected function withCleanupLinks(array $input)
71    {
72        if (!isset($input['links'])) {
73            return $input;
74        }
75        $links = $input['links'];
76
77        $input['links'] = array_filter($links, function ($link) {
78            return !($link['name'] === '' && $link['url'] == '');
79        });
80
81        foreach ($input['links'] as $index => $link) {
82            $input['links'][$index]['target'] = (isset($link['target']) && $link['target']) ? 1 : 0;
83        }
84
85        return $input;
86    }
87
88    protected function withCleanupDayoffs(array $input)
89    {
90        if (!isset($input['dayoff'])) {
91            return $input;
92        }
93        $dayoffs = $input['dayoff'];
94        $input['dayoff'] = array_filter($dayoffs, function ($dayoff) {
95            return !($dayoff['name'] === '');
96        });
97
98        return $input;
99    }
100
101    private function withEmailReminderDefaultValues(array $input)
102    {
103        if ($input['sendEmailReminderMinutesBefore'] === '') {
104            $input['sendEmailReminderMinutesBefore'] = null;
105        }
106
107        if ($input['sendEmailReminderEnabled'] && empty($input['sendEmailReminderMinutesBefore'])) {
108            $input['sendEmailReminderMinutesBefore'] = 120;
109        }
110
111        return $input;
112    }
113
114    protected function writeUpdatedEntity($input, $entityId)
115    {
116        $entity = (new Entity($input))->withCleanedUpFormData();
117        $entity->id = $entityId;
118        $entity->dayoff = $entity->getDayoffList()->withTimestampFromDateformat();
119        return $this->handleEntityWrite(function () use ($entity) {
120            return \App::$http->readPostResult(
121                '/department/' . $entity->id . '/',
122                $entity
123            )->getEntity();
124        });
125    }
126}