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