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 \Psr\Http\Message\ResponseInterface
20     */
21    #[\Override]
22    public function readResponse(
23        \Psr\Http\Message\RequestInterface $request,
24        \Psr\Http\Message\ResponseInterface $response,
25        array $args
26    ): \Psr\Http\Message\ResponseInterface {
27        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 1])->getEntity();
28        $success = $request->getAttribute('validator')->getParameter('success')->isString()->getValue();
29        if (!$workstation->getUseraccount()->hasPermissions(['source'])) {
30            throw new \BO\Zmsentities\Exception\UserAccountMissingRights();
31        }
32
33        if ('add' != $args['name']) {
34            $source = \App::$http
35                ->readGetResult('/source/' . $args['name'] . '/', ['resolveReferences' => 2])
36                ->getEntity();
37        }
38
39        $currentSource = $args['name'] ?? null;
40        [$parentProviders, $parentRequests] = $this->loadParentsFromSources($currentSource);
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->writeUpdatedEntity($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 writeUpdatedEntity($input)
80    {
81        $entity = (new Entity($input))->withCleanedUpFormData();
82        return $this->handleEntityWrite(function () use ($entity) {
83            return \App::$http->readPostResult('/source/', $entity)->getEntity();
84        });
85    }
86
87    private function loadParentsFromSources(?string $currentSource): array
88    {
89        $sourceList = \App::$http->readGetResult('/source/', ['resolveReferences' => 2])->getCollection();
90
91        $allowed = ['dldb'];
92        if ($currentSource && $currentSource !== 'add') {
93            $allowed[] = $currentSource;
94        }
95        $allowed = array_unique($allowed);
96
97        $parentProviders = [];
98        $parentRequests  = [];
99
100        foreach ($sourceList as $fullSource) {
101            $srcName = $fullSource->source ?? null;
102            if (!$srcName || !in_array($srcName, $allowed, true)) {
103                continue;
104            }
105
106            foreach (($fullSource->providers ?? []) as $provider) {
107                $parentProviders[] = $provider;
108            }
109            foreach (($fullSource->requests ?? []) as $req) {
110                $parentRequests[] = $req;
111            }
112        }
113
114        usort($parentProviders, fn($a, $b) => strcasecmp($a->name ?? '', $b->name ?? ''));
115        usort($parentRequests, fn($a, $b) => strcasecmp($a->name ?? '', $b->name ?? ''));
116
117        return [$parentProviders, $parentRequests];
118    }
119}