Lines 97.22% 35 / 36
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
OrganisationAddDepartment 97.22% 35 / 36 50.00% 1 / 2 5 0.00% 0 / 1
 readResponse 96.77% 30 / 31 0.00% 0 / 1 3
 withCleanupLinks 100.00% 5 / 5 100.00% 1 / 1 2
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            $entity = (new Entity($input))->withCleanedUpFormData();
35            $department = \App::$http->readPostResult('/organisation/' . $organisationId . '/department/', $entity)
36                ->getEntity();
37            return \BO\Slim\Render::redirect(
38                'department',
39                array(
40                    'id' => $department->id
41                ),
42                array(
43                    'success' => 'department_created'
44                )
45            );
46        }
47
48        return \BO\Slim\Render::withHtml(
49            $response,
50            'page/department.twig',
51            array(
52                'title' => 'Behörde einrichten',
53                'action' => 'add',
54                'menuActive' => 'owner',
55                'workstation' => $workstation,
56                'organisation' => $organisation
57            )
58        );
59    }
60
61    /**
62     * @return (array|mixed)[]
63     *
64     */
65    protected function withCleanupLinks(array $input): array
66    {
67        $links = $input['links'];
68
69        $input['links'] = array_filter($links, function ($link) {
70            return !($link['name'] === '' && $link['url'] == '');
71        });
72
73        return $input;
74    }
75}