Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.56% covered (success)
97.56%
40 / 41
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Organisation
97.56% covered (success)
97.56%
40 / 41
50.00% covered (danger)
50.00%
1 / 2
9
0.00% covered (danger)
0.00%
0 / 1
 readResponse
97.22% covered (success)
97.22%
35 / 36
0.00% covered (danger)
0.00%
0 / 1
8
 writeUpdatedEntity
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\Organisation as Entity;
11use BO\Mellon\Validator;
12
13/**
14  * Handle requests concerning services
15  *
16  */
17class Organisation extends BaseController
18{
19    /**
20     * @return \Psr\Http\Message\ResponseInterface
21     */
22
23    #[\Override]
24    public function readResponse(
25        \Psr\Http\Message\RequestInterface $request,
26        \Psr\Http\Message\ResponseInterface $response,
27        array $args
28    ): \Psr\Http\Message\ResponseInterface {
29        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 1])->getEntity();
30        $success = $request->getAttribute('validator')->getParameter('success')->isString()->getValue();
31        $entityId = Validator::value($args['id'])->isNumber()->getValue();
32        $entity = \App::$http->readGetResult(
33            '/organisation/' . $entityId . '/',
34            ['resolveReferences' => 1]
35        )->getEntity();
36
37        $input = $request->getParsedBody();
38        $result = null;
39        if (array_key_exists('save', (array) $input)) {
40            $result = $this->writeUpdatedEntity($input, $entityId);
41            if ($result instanceof Entity) {
42                return \BO\Slim\Render::redirect(
43                    'organisation',
44                    [
45                        'id' => $entityId
46                    ],
47                    [
48                        'success' => 'organisation_saved'
49                    ]
50                );
51            }
52        }
53
54        // If there was an error, use the submitted input data for form re-population
55        $organisationData = (isset($result) && is_array($result) && isset($result['data']))
56            ? new Entity(array_merge($entity->getArrayCopy(), $input ?? []))
57            : $entity;
58
59        return \BO\Slim\Render::withHtml(
60            $response,
61            'page/organisation.twig',
62            array(
63                'title' => 'Referat bearbeiten',
64                'workstation' => $workstation,
65                'organisation' => $organisationData,
66                'menuActive' => 'owner',
67                'success' => $success,
68                'exception' => (isset($result) && !($result instanceof Entity)) ? $result : null,
69            )
70        );
71    }
72
73    protected function writeUpdatedEntity($input, $entityId)
74    {
75        $entity = (new Entity($input))->withCleanedUpFormData();
76        $entity->id = $entityId;
77        return $this->handleEntityWrite(function () use ($entity) {
78            return \App::$http->readPostResult('/organisation/' . $entity->id . '/', $entity)->getEntity();
79        });
80    }
81}