Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
48 / 51
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SourceEdit
94.12% covered (success)
94.12%
48 / 51
50.00% covered (danger)
50.00%
1 / 2
12.03
0.00% covered (danger)
0.00%
0 / 1
 readResponse
92.50% covered (success)
92.50%
37 / 40
0.00% covered (danger)
0.00%
0 / 1
9.03
 testUpdateEntity
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 *
5 * @package Zmsadmin
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmsadmin;
11
12use BO\Zmsentities\Source as Entity;
13use BO\Mellon\Validator;
14
15class SourceEdit extends BaseController
16{
17    /**
18     * @SuppressWarnings(Param)
19     * @return String
20     */
21    public function readResponse(
22        \Psr\Http\Message\RequestInterface $request,
23        \Psr\Http\Message\ResponseInterface $response,
24        array $args
25    ) {
26        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 1])->getEntity();
27        $success = $request->getAttribute('validator')->getParameter('success')->isString()->getValue();
28        if (!$workstation->hasSuperUseraccount()) {
29            throw new Exception\NotAllowed();
30        }
31
32        if ('add' != $args['name']) {
33            $source = \App::$http
34                ->readGetResult('/source/' . $args['name'] . '/', ['resolveReferences' => 2])
35                ->getEntity();
36        }
37
38        $parents = \App::$http->readGetResult('/source/dldb/', ['resolveReferences' => 2])->getEntity();
39        $parentProviders = $parents->providers ?? [];
40        $parentRequests  = $parents->requests  ?? [];
41
42        try {
43            $apiRes  = \App::$http->readGetResult('/requestvariants/');
44            $body    = (string) $apiRes->getResponse()->getBody();
45            $payload = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
46            $requestVariants = $payload['data'] ?? [];
47        } catch (\JsonException $e) {
48            \BO\Log::error('requestvariants JSON decode failed', ['error' => $e->getMessage()]);
49            $requestVariants = [];
50        }
51
52        $input = $request->getParsedBody();
53        if (is_array($input) && array_key_exists('save', $input)) {
54            $result = $this->testUpdateEntity($input);
55            if ($result instanceof Entity) {
56                return \BO\Slim\Render::redirect('sourceEdit', ['name' => $result->getSource()], [
57                    'success' => 'source_saved'
58                ]);
59            }
60        }
61
62        return \BO\Slim\Render::withHtml(
63            $response,
64            'page/sourceedit.twig',
65            array(
66                'title' => 'Mandanten bearbeiten',
67                'menuActive' => 'source',
68                'workstation' => $workstation,
69                'source' => (isset($source)) ? $source : null,
70                'parentProviders' => $parentProviders,
71                'parentRequests' => $parentRequests,
72                'requestVariants' => $requestVariants,
73                'success' => $success,
74                'exception' => (isset($result)) ? $result : null
75            )
76        );
77    }
78
79    protected function testUpdateEntity($input)
80    {
81        $entity = (new Entity($input))->withCleanedUpFormData();
82        try {
83            $entity = \App::$http->readPostResult('/source/', $entity)->getEntity();
84        } catch (\BO\Zmsclient\Exception $exception) {
85            if ('BO\Zmsentities\Exception\SchemaValidation' == $exception->template) {
86                $exceptionData = [
87                    'template' => 'exception/bo/zmsentities/exception/schemavalidation.twig'
88                ];
89                $exceptionData['data'] = $exception->data;
90                return $exceptionData;
91            } else {
92                throw $exception;
93            }
94        }
95        return $entity;
96    }
97}