Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.06% covered (success)
92.06%
58 / 63
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Department
92.06% covered (success)
92.06%
58 / 63
20.00% covered (danger)
20.00%
1 / 5
21.22
0.00% covered (danger)
0.00%
0 / 1
 readResponse
96.97% covered (success)
96.97%
32 / 33
0.00% covered (danger)
0.00%
0 / 1
8
 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        $success = $request->getAttribute('validator')->getParameter('success')->isString()->getValue();
26        $entityId = Validator::value($args['id'])->isNumber()->getValue();
27        $entity = \App::$http->readGetResult('/department/' . $entityId . '/', ['resolveReferences' => 1])->getEntity();
28        $organisation = \App::$http->readGetResult('/department/' . $entityId . '/organisation/')->getEntity();
29        $input = $request->getParsedBody();
30        $result = null;
31
32        if ($request->getMethod() === 'POST') {
33            $input = $this->withCleanupLinks($input);
34            $input = $this->withCleanupDayoffs($input);
35            $input = $this->withEmailReminderDefaultValues($input);
36            $result = $this->writeUpdatedEntity($input, $entityId);
37            if ($result instanceof Entity) {
38                return \BO\Slim\Render::redirect('department', ['id' => $entityId], [
39                    'success' => 'department_saved'
40                ]);
41            }
42        }
43
44        // If there was an error, use the submitted input data for form re-population
45        $departmentData = (isset($result) && is_array($result) && isset($result['data']))
46            ? array_merge((new Schema($entity))->toSanitizedArray(), $input ?? [])
47            : (new Schema($entity))->toSanitizedArray();
48
49        return \BO\Slim\Render::withHtml(
50            $response,
51            'page/department.twig',
52            array(
53                'title' => 'Standort',
54                'workstation' => $workstation,
55                'organisation' => $organisation,
56                'department' => $departmentData,
57                'hasAccess' => $entity->hasAccess($workstation->getUseraccount()),
58                'menuActive' => 'owner',
59                'success' => $success,
60                'exception' => (isset($result) && !($result instanceof Entity)) ? $result : null,
61            )
62        );
63    }
64
65    protected function withCleanupLinks(array $input)
66    {
67        if (!isset($input['links'])) {
68            return $input;
69        }
70        $links = $input['links'];
71
72        $input['links'] = array_filter($links, function ($link) {
73            return !($link['name'] === '' && $link['url'] == '');
74        });
75
76        foreach ($input['links'] as $index => $link) {
77            $input['links'][$index]['target'] = (isset($link['target']) && $link['target']) ? 1 : 0;
78        }
79
80        return $input;
81    }
82
83    protected function withCleanupDayoffs(array $input)
84    {
85        if (!isset($input['dayoff'])) {
86            return $input;
87        }
88        $dayoffs = $input['dayoff'];
89        $input['dayoff'] = array_filter($dayoffs, function ($dayoff) {
90            return !($dayoff['name'] === '');
91        });
92
93        return $input;
94    }
95
96    private function withEmailReminderDefaultValues(array $input)
97    {
98        if ($input['sendEmailReminderMinutesBefore'] === '') {
99            $input['sendEmailReminderMinutesBefore'] = null;
100        }
101
102        if ($input['sendEmailReminderEnabled'] && empty($input['sendEmailReminderMinutesBefore'])) {
103            $input['sendEmailReminderMinutesBefore'] = 120;
104        }
105
106        return $input;
107    }
108
109    protected function writeUpdatedEntity($input, $entityId)
110    {
111        $entity = (new Entity($input))->withCleanedUpFormData();
112        $entity->id = $entityId;
113        $entity->dayoff = $entity->getDayoffList()->withTimestampFromDateformat();
114        return $this->handleEntityWrite(function () use ($entity) {
115            return \App::$http->readPostResult(
116                '/department/' . $entity->id . '/',
117                $entity
118            )->getEntity();
119        });
120    }
121}