| Conditions | 4 |
| Paths | 4 |
| Total Lines | 34 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 16 |
| CRAP Score | 4 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 27 | 4 | public function getValidatedBody( |
|
| 28 | ServerRequestInterface $request, |
||
| 29 | string $schemaFileName |
||
| 30 | ): array { |
||
| 31 | 4 | $schemaFilePath = __DIR__ . '/../../../resource/api-schema/' . $schemaFileName; |
|
| 32 | |||
| 33 | 4 | if (!file_exists($schemaFilePath)) { |
|
| 34 | 1 | throw new Exception\ValidatorException( |
|
| 35 | 1 | sprintf('Schema `%s` not found', $schemaFileName) |
|
| 36 | ); |
||
| 37 | } |
||
| 38 | |||
| 39 | 3 | $body = (string) $request->getBody(); |
|
| 40 | |||
| 41 | 3 | $bodyDecoded = json_decode($body); |
|
| 42 | |||
| 43 | 3 | if (json_last_error() !== JSON_ERROR_NONE) { |
|
| 44 | 1 | throw new Exception\ValidatorException( |
|
| 45 | 1 | json_last_error_msg() |
|
| 46 | ); |
||
| 47 | } |
||
| 48 | |||
| 49 | 2 | $result = $this->validator->validate( |
|
| 50 | $bodyDecoded, |
||
| 51 | 2 | file_get_contents($schemaFilePath) |
|
| 52 | ); |
||
| 53 | |||
| 54 | 2 | if (!$result->isValid()) { |
|
| 55 | 1 | throw new Exception\ValidatorException( |
|
| 56 | 1 | (string) $result->error()?->message() |
|
| 57 | ); |
||
| 58 | } |
||
| 59 | |||
| 60 | 1 | return json_decode($body, true, 512, JSON_THROW_ON_ERROR); |
|
| 61 | } |
||
| 63 |