Lines 91.81% 101 / 110
Methods 66.66% 6 / 9
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 readResponse 97.95% 48 / 49 0.00% 0 / 1 6
 readSourceList 100.00% 2 / 2 100.00% 1 / 1 1
 readCurrentSource 100.00% 2 / 2 100.00% 1 / 1 1
 writeUpdatedEntity 76.92% 10 / 13 0.00% 0 / 1 5.31
 writeUploadedImage 100.00% 3 / 3 100.00% 1 / 1 3
 [BO\Zmsadmin\BaseController] __invoke 100.00% 3 / 3 100.00% 1 / 1 1
 [BO\Zmsadmin\BaseController] getSchemaConstraintList 100.00% 8 / 8 100.00% 1 / 1 4
 [BO\Zmsadmin\BaseController] transformValidationErrors 61.53% 8 / 13 0.00% 0 / 1 15.69
 [BO\Zmsadmin\BaseController] handleEntityWrite 100.00% 17 / 17 100.00% 1 / 1 5
16class Scope extends BaseController
17{
18    /**
19     *
20     * @return \Psr\Http\Message\ResponseInterface
21     */
22    #[\Override]
23    public function readResponse(
24        \Psr\Http\Message\RequestInterface $request,
25        \Psr\Http\Message\ResponseInterface $response,
26        array $args
27    ): \Psr\Http\Message\ResponseInterface {
28        $success = $request->getAttribute('validator')->getParameter('success')->isString()->getValue();
29
30        $workstation = \App::$http->readGetResult('/workstation/', [
31            'resolveReferences' => 1,
32            'gql' => Helper\GraphDefaults::getWorkstation()
33        ])->getEntity();
34        if (!$workstation->getUseraccount()->hasAnyPermission(['scope','restrictedscope'])) {
35            throw new \BO\Zmsentities\Exception\UserAccountMissingRights();
36        }
37        $entityId = Validator::value($args['id'])->isNumber()->getValue();
38        $entity = \App::$http
39            ->readGetResult('/scope/' . $entityId . '/', [
40                'resolveReferences' => 1,
41                'accessRights' => 'restrictedscope',
42                'gql' => Helper\GraphDefaults::getScope()
43            ])
44            ->getEntity();
45
46        $sourceList = $this->readSourceList();
47        $providerList = Helper\ProviderHandler::readProviderList($entity->getSource());
48        $currentSource = $this->readCurrentSource($entity->getSource());
49
50        $organisation = \App::$http->readGetResult('/scope/' . $entityId . '/organisation/')->getEntity();
51        $department = \App::$http->readGetResult('/scope/' . $entityId . '/department/')->getEntity();
52        $callDisplayImage = \App::$http->readGetResult('/scope/' . $entityId . '/imagedata/calldisplay/')->getEntity();
53        $input = $request->getParsedBody();
54        if ($request->getMethod() === 'POST') {
55            $result = $this->writeUpdatedEntity($input, $entityId, $entity, $workstation);
56            if ($result instanceof Entity) {
57                if ($workstation->getUseraccount()->hasPermissions(['scope'])) {
58                    $this->writeUploadedImage($request, $entityId, $input);
59                }
60                return Render::redirect('scope', ['id' => $entityId], [
61                    'success' => 'scope_saved'
62                ]);
63            }
64        }
65
66        return Render::withHtml(
67            $response,
68            'page/scope.twig',
69            array(
70                'title' => 'Standort',
71                'menuActive' => 'owner',
72                'workstation' => $workstation,
73                'scope' => $entity,
74                'provider' => $entity->provider,
75                'organisation' => $organisation,
76                'department' => $department,
77                'providerList' => $providerList,
78                'sourceList' => $sourceList,
79                'source' => $currentSource,
80                'callDisplayImage' => $callDisplayImage,
81                'success' => $success,
82                'exception' => (isset($result)) ? $result : null
83            )
84        );
85    }
86
87    protected function readSourceList()
88    {
89        $sourceList = \App::$http->readGetResult('/source/')->getCollection();
90        return $sourceList;
91    }
92
93    protected function readCurrentSource($source)
94    {
95        $source = \App::$http->readGetResult('/source/' . $source . '/')->getEntity();
96        return $source;
97    }
98
99    protected function writeUpdatedEntity(array $input, $entityId = null, ?Entity $existingScope = null, $workstation = null)
100    {
101        $entity = (new Entity($input))->withCleanedUpFormData();
102        if ($workstation && !$workstation->getUseraccount()->hasPermissions(['scope'])) {
103            if (!$existingScope) {
104                throw new \BO\Zmsentities\Exception\UserAccountMissingRights();
105            }
106            $entity = $entity->withProviderSourceFrom($existingScope);
107        }
108        return $this->handleEntityWrite(function () use ($entity, $entityId, $existingScope) {
109            if ($existingScope) {
110                $entity->id = $entityId;
111                return \App::$http->readPostResult('/scope/' . $entity->id . '/', $entity)->getEntity();
112            }
113            return \App::$http
114                ->readPostResult('/department/' . $entityId . '/scope/', $entity)
115                ->getEntity();
116        });
117    }
118
119    protected function writeUploadedImage(\Psr\Http\Message\RequestInterface $request, $entityId, array $input): void
120    {
121        if (isset($input['removeImage']) && $input['removeImage']) {
122            \App::$http->readDeleteResult('/scope/' . $entityId . '/imagedata/calldisplay/');
123        } else {
124            (new Helper\FileUploader($request, 'uploadCallDisplayImage'))->writeUploadToScope($entityId);
125        }
126    }
127}

Inherited from BO\Zmsadmin\BaseController

21    public function __invoke(RequestInterface $request, ResponseInterface $response, array $args)
22    {
23        $request = $this->initRequest($request);
24        $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0');
25        return $this->readResponse($request, $noCacheResponse, $args);
26    }
41    public function getSchemaConstraintList($schema): array
42    {
43        $list = [];
44        $locale = \App::$language->getLocale();
45        foreach ($schema->properties as $key => $property) {
46            if (isset($property['x-locale'])) {
47                $constraints = $property['x-locale'][$locale];
48                if ($constraints) {
49                    $list[$key]['description'] = $constraints['messages'];
50                }
51            }
52        }
53        return $list;
54    }
65    protected function transformValidationErrors($errorData)
66    {
67        if (!is_array($errorData) && !($errorData instanceof \Traversable)) {
68            return [];
69        }
70        $transformed = [];
71        foreach ($errorData as $pointer => $item) {
72            // Extract field name from JSON pointer (e.g., "/id" -> "id", "/contact/email" -> "contact/email")
73            // If the key doesn't start with "/", it's already a field name, so use it as-is
74            $fieldName = (strpos($pointer, '/') === 0) ? ltrim($pointer, '/') : $pointer;
75            // Handle root level errors
76            if ($fieldName === '' || $fieldName === null) {
77                $fieldName = '_root';
78            }
79            // Ensure the item structure is correct (has 'messages' array)
80            if (is_array($item) && isset($item['messages'])) {
81                $transformed[$fieldName] = $item;
82            } elseif (is_array($item)) {
83                // If item is an array but doesn't have 'messages', wrap it
84                $transformed[$fieldName] = $item;
85            } else {
86                $transformed[$fieldName] = $item;
87            }
88        }
89        return $transformed;
90    }
99    protected function handleEntityWrite(callable $httpCall)
100    {
101        try {
102            return $httpCall();
103        } catch (\BO\Zmsclient\Exception $exception) {
104            if ('BO\Zmsentities\Exception\SchemaValidation' == $exception->template) {
105                return [
106                    'template' => 'exception/bo/zmsentities/exception/schemavalidation.twig',
107                    'include' => true,
108                    'data' => $this->transformValidationErrors($exception->data)
109                ];
110            }
111
112            $template = TwigExceptionHandler::getExceptionTemplate($exception);
113            if (
114                '' != $exception->template
115                && \App::$slim->getContainer()->get('view')->getLoader()->exists($template)
116            ) {
117                return [
118                    'template' => $template,
119                    'include' => true,
120                    'data' => $this->transformValidationErrors($exception->data)
121                ];
122            }
123
124            throw $exception;
125        }
126    }