Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.80% |
36 / 41 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| BaseController | |
87.80% |
36 / 41 |
|
75.00% |
3 / 4 |
21.80 | |
0.00% |
0 / 1 |
| __invoke | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| readResponse | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| getSchemaConstraintList | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| transformValidationErrors | |
61.54% |
8 / 13 |
|
0.00% |
0 / 1 |
15.69 | |||
| handleEntityWrite | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package Zmsadmin |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsadmin; |
| 9 | |
| 10 | use BO\Zmsadmin\Helper\TwigExceptionHandler; |
| 11 | use Psr\Http\Message\RequestInterface; |
| 12 | use Psr\Http\Message\ResponseInterface; |
| 13 | |
| 14 | /** |
| 15 | * @SuppressWarnings(NumberOfChildren) |
| 16 | * |
| 17 | */ |
| 18 | abstract class BaseController extends \BO\Slim\Controller |
| 19 | { |
| 20 | public function __invoke(RequestInterface $request, ResponseInterface $response, array $args) |
| 21 | { |
| 22 | $request = $this->initRequest($request); |
| 23 | $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0'); |
| 24 | return $this->readResponse($request, $noCacheResponse, $args); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @codeCoverageIgnore |
| 29 | * |
| 30 | */ |
| 31 | public function readResponse(RequestInterface $request, ResponseInterface $response, array $args) |
| 32 | { |
| 33 | return parent::__invoke($request, $response, $args); |
| 34 | } |
| 35 | |
| 36 | public function getSchemaConstraintList($schema) |
| 37 | { |
| 38 | $list = []; |
| 39 | $locale = \App::$language->getLocale(); |
| 40 | foreach ($schema->properties as $key => $property) { |
| 41 | if (isset($property['x-locale'])) { |
| 42 | $constraints = $property['x-locale'][$locale]; |
| 43 | if ($constraints) { |
| 44 | $list[$key]['description'] = $constraints['messages']; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | return $list; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Transform validation error data from JSON pointer format to field names |
| 53 | * Maps pointers like "/id" to "id", "/changePassword" to "changePassword", etc. |
| 54 | * Handles nested paths like "/contact/email" by flattening them. |
| 55 | * Also handles data that's already in field name format (for test compatibility). |
| 56 | * |
| 57 | * @param array|null $errorData The exception data with JSON pointers as keys, or field names |
| 58 | * @return array Transformed data with field names as keys |
| 59 | */ |
| 60 | protected function transformValidationErrors($errorData) |
| 61 | { |
| 62 | if (!is_array($errorData) && !($errorData instanceof \Traversable)) { |
| 63 | return []; |
| 64 | } |
| 65 | $transformed = []; |
| 66 | foreach ($errorData as $pointer => $item) { |
| 67 | // Extract field name from JSON pointer (e.g., "/id" -> "id", "/contact/email" -> "contact/email") |
| 68 | // If the key doesn't start with "/", it's already a field name, so use it as-is |
| 69 | $fieldName = (strpos($pointer, '/') === 0) ? ltrim($pointer, '/') : $pointer; |
| 70 | // Handle root level errors |
| 71 | if ($fieldName === '' || $fieldName === null) { |
| 72 | $fieldName = '_root'; |
| 73 | } |
| 74 | // Ensure the item structure is correct (has 'messages' array) |
| 75 | if (is_array($item) && isset($item['messages'])) { |
| 76 | $transformed[$fieldName] = $item; |
| 77 | } elseif (is_array($item)) { |
| 78 | // If item is an array but doesn't have 'messages', wrap it |
| 79 | $transformed[$fieldName] = $item; |
| 80 | } else { |
| 81 | $transformed[$fieldName] = $item; |
| 82 | } |
| 83 | } |
| 84 | return $transformed; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Wraps an HTTP write operation with standardized exception handling. |
| 89 | * |
| 90 | * @param callable $httpCall |
| 91 | * @return mixed |
| 92 | * @throws \BO\Zmsclient\Exception |
| 93 | */ |
| 94 | protected function handleEntityWrite(callable $httpCall) |
| 95 | { |
| 96 | try { |
| 97 | return $httpCall(); |
| 98 | } catch (\BO\Zmsclient\Exception $exception) { |
| 99 | if ('BO\Zmsentities\Exception\SchemaValidation' == $exception->template) { |
| 100 | return [ |
| 101 | 'template' => 'exception/bo/zmsentities/exception/schemavalidation.twig', |
| 102 | 'include' => true, |
| 103 | 'data' => $this->transformValidationErrors($exception->data) |
| 104 | ]; |
| 105 | } |
| 106 | |
| 107 | $template = TwigExceptionHandler::getExceptionTemplate($exception); |
| 108 | if ( |
| 109 | '' != $exception->template |
| 110 | && \App::$slim->getContainer()->get('view')->getLoader()->exists($template) |
| 111 | ) { |
| 112 | return [ |
| 113 | 'template' => $template, |
| 114 | 'include' => true, |
| 115 | 'data' => $this->transformValidationErrors($exception->data) |
| 116 | ]; |
| 117 | } |
| 118 | |
| 119 | throw $exception; |
| 120 | } |
| 121 | } |
| 122 | } |