Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.59% covered (success)
92.59%
50 / 54
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Department
92.59% covered (success)
92.59%
50 / 54
25.00% covered (danger)
25.00%
1 / 4
14.08
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
2
 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
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
31        if ($request->getMethod() === 'POST') {
32            $input = $this->withCleanupLinks($input);
33            $input = $this->withCleanupDayoffs($input);
34            $input = $this->withEmailReminderDefaultValues($input);
35            $entity = (new Entity($input))->withCleanedUpFormData();
36            $entity->id = $entityId;
37            $entity->dayoff = $entity->getDayoffList()->withTimestampFromDateformat();
38            $entity = \App::$http->readPostResult(
39                '/department/' . $entity->id . '/',
40                $entity
41            )->getEntity();
42            return \BO\Slim\Render::redirect('department', ['id' => $entityId], [
43                'success' => 'department_saved'
44            ]);
45        }
46
47        return \BO\Slim\Render::withHtml(
48            $response,
49            'page/department.twig',
50            array(
51                'title' => 'Standort',
52                'workstation' => $workstation,
53                'organisation' => $organisation,
54                'department' => (new Schema($entity))->toSanitizedArray(),
55                'hasAccess' => $entity->hasAccess($workstation->getUseraccount()),
56                'menuActive' => 'owner',
57                'success' => $success,
58            )
59        );
60    }
61
62    protected function withCleanupLinks(array $input)
63    {
64        if (!isset($input['links'])) {
65            return $input;
66        }
67        $links = $input['links'];
68
69        $input['links'] = array_filter($links, function ($link) {
70            return !($link['name'] === '' && $link['url'] == '');
71        });
72
73        foreach ($input['links'] as $index => $link) {
74            $input['links'][$index]['target'] = (isset($link['target']) && $link['target']) ? 1 : 0;
75        }
76
77        return $input;
78    }
79
80    protected function withCleanupDayoffs(array $input)
81    {
82        if (!isset($input['dayoff'])) {
83            return $input;
84        }
85        $dayoffs = $input['dayoff'];
86        $input['dayoff'] = array_filter($dayoffs, function ($dayoff) {
87            return !($dayoff['name'] === '');
88        });
89
90        return $input;
91    }
92
93    private function withEmailReminderDefaultValues(array $input)
94    {
95        if ($input['sendEmailReminderMinutesBefore'] === '') {
96            $input['sendEmailReminderMinutesBefore'] = null;
97        }
98
99        if ($input['sendEmailReminderEnabled'] && empty($input['sendEmailReminderMinutesBefore'])) {
100            $input['sendEmailReminderMinutesBefore'] = 120;
101        }
102
103        return $input;
104    }
105}