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