Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.67% covered (success)
97.67%
42 / 43
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
OrganisationAddDepartment
97.67% covered (success)
97.67%
42 / 43
66.67% covered (warning)
66.67%
2 / 3
6
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
3
 withCleanupLinks
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 withCleanupDayoffs
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @package Zmsadmin
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsadmin;
9
10use BO\Zmsentities\Exception\UserAccountMissingRights;
11use BO\Zmsentities\Department as Entity;
12use BO\Mellon\Validator;
13
14class OrganisationAddDepartment extends BaseController
15{
16    /**
17     * @return \Psr\Http\Message\ResponseInterface
18     */
19    #[\Override]
20    public function readResponse(
21        \Psr\Http\Message\RequestInterface $request,
22        \Psr\Http\Message\ResponseInterface $response,
23        array $args
24    ): \Psr\Http\Message\ResponseInterface {
25        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 1])->getEntity();
26        if (!$workstation->getUseraccount()->hasPermissions(['department'])) {
27            throw new UserAccountMissingRights();
28        }
29        $input = $request->getParsedBody();
30        $organisationId = Validator::value($args['id'])->isNumber()->getValue();
31        $organisation = \App::$http->readGetResult('/organisation/' . $organisationId . '/')->getEntity();
32        if ($request->getMethod() === 'POST') {
33            $input = $this->withCleanupLinks($input);
34            $input = $this->withCleanupDayoffs($input);
35            $entity = (new Entity($input))->withCleanedUpFormData();
36            $entity->dayoff = $entity->getDayoffList()->withTimestampFromDateformat();
37            $department = \App::$http->readPostResult('/organisation/' . $organisationId . '/department/', $entity)
38                ->getEntity();
39            return \BO\Slim\Render::redirect(
40                'department',
41                array(
42                    'id' => $department->id
43                ),
44                array(
45                    'success' => 'department_created'
46                )
47            );
48        }
49
50        return \BO\Slim\Render::withHtml(
51            $response,
52            'page/department.twig',
53            array(
54                'title' => 'Behörde einrichten',
55                'action' => 'add',
56                'menuActive' => 'owner',
57                'workstation' => $workstation,
58                'organisation' => $organisation
59            )
60        );
61    }
62
63    protected function withCleanupLinks(array $input)
64    {
65        $links = $input['links'];
66
67        $input['links'] = array_filter($links, function ($link) {
68            return !($link['name'] === '' && $link['url'] == '');
69        });
70
71        return $input;
72    }
73
74    protected function withCleanupDayoffs(array $input)
75    {
76        $dayoffs = $input['dayoff'];
77        $input['dayoff'] = array_filter($dayoffs, function ($dayoff) {
78            return !($dayoff['name'] === '');
79        });
80
81        return $input;
82    }
83}