Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.80% covered (success)
91.80%
56 / 61
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SourceEdit
91.80% covered (success)
91.80%
56 / 61
33.33% covered (danger)
33.33%
1 / 3
18.18
0.00% covered (danger)
0.00%
0 / 1
 readResponse
92.31% covered (success)
92.31%
36 / 39
0.00% covered (danger)
0.00%
0 / 1
9.04
 writeUpdatedEntity
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 loadParentsFromSources
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
8.09
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        $currentSource = $args['name'] ?? null;
39        [$parentProviders, $parentRequests] = $this->loadParentsFromSources($currentSource);
40
41        try {
42            $apiRes  = \App::$http->readGetResult('/requestvariants/');
43            $body    = (string) $apiRes->getResponse()->getBody();
44            $payload = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
45            $requestVariants = $payload['data'] ?? [];
46        } catch (\JsonException $e) {
47            \BO\Log::error('requestvariants JSON decode failed', ['error' => $e->getMessage()]);
48            $requestVariants = [];
49        }
50
51        $input = $request->getParsedBody();
52        if (is_array($input) && array_key_exists('save', $input)) {
53            $result = $this->writeUpdatedEntity($input);
54            if ($result instanceof Entity) {
55                return \BO\Slim\Render::redirect('sourceEdit', ['name' => $result->getSource()], [
56                    'success' => 'source_saved'
57                ]);
58            }
59        }
60
61        return \BO\Slim\Render::withHtml(
62            $response,
63            'page/sourceedit.twig',
64            array(
65                'title' => 'Mandanten bearbeiten',
66                'menuActive' => 'source',
67                'workstation' => $workstation,
68                'source' => (isset($source)) ? $source : null,
69                'parentProviders' => $parentProviders,
70                'parentRequests' => $parentRequests,
71                'requestVariants' => $requestVariants,
72                'success' => $success,
73                'exception' => (isset($result)) ? $result : null
74            )
75        );
76    }
77
78    protected function writeUpdatedEntity($input)
79    {
80        $entity = (new Entity($input))->withCleanedUpFormData();
81        return $this->handleEntityWrite(function () use ($entity) {
82            return \App::$http->readPostResult('/source/', $entity)->getEntity();
83        });
84    }
85
86    private function loadParentsFromSources(?string $currentSource): array
87    {
88        $sourceList = \App::$http->readGetResult('/source/', ['resolveReferences' => 2])->getCollection();
89
90        $allowed = ['dldb'];
91        if ($currentSource && $currentSource !== 'add') {
92            $allowed[] = $currentSource;
93        }
94        $allowed = array_unique($allowed);
95
96        $parentProviders = [];
97        $parentRequests  = [];
98
99        foreach ($sourceList as $fullSource) {
100            $srcName = $fullSource->source ?? null;
101            if (!$srcName || !in_array($srcName, $allowed, true)) {
102                continue;
103            }
104
105            foreach (($fullSource->providers ?? []) as $provider) {
106                $parentProviders[] = $provider;
107            }
108            foreach (($fullSource->requests ?? []) as $req) {
109                $parentRequests[] = $req;
110            }
111        }
112
113        usort($parentProviders, fn($a, $b) => strcasecmp($a->name ?? '', $b->name ?? ''));
114        usort($parentRequests, fn($a, $b) => strcasecmp($a->name ?? '', $b->name ?? ''));
115
116        return [$parentProviders, $parentRequests];
117    }
118}