| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 100.00% | 12 / 12 |  | 100.00% | 2 / 2 | CRAP |  | 100.00% | 1 / 1 | 
| OpenApiDocumentationController |  | 100.00% | 12 / 12 |  | 100.00% | 2 / 2 | 3 |  | 100.00% | 1 / 1 | 
| __construct |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| __invoke |  | 100.00% | 11 / 11 |  | 100.00% | 1 / 1 | 2 | |||
| 1 | <?php | 
| 2 | namespace Apie\RestApi\Controllers; | 
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContextHashmap; | 
| 5 | use Apie\RestApi\OpenApi\OpenApiGenerator; | 
| 6 | use cebe\openapi\Writer; | 
| 7 | use Nyholm\Psr7\Factory\Psr17Factory; | 
| 8 | use Psr\Http\Message\ResponseInterface; | 
| 9 | use Psr\Http\Message\ServerRequestInterface; | 
| 10 | |
| 11 | class OpenApiDocumentationController | 
| 12 | { | 
| 13 | public function __construct( | 
| 14 | private BoundedContextHashmap $boundedContextHashmap, | 
| 15 | private OpenApiGenerator $openApiGenerator | 
| 16 | ) { | 
| 17 | } | 
| 18 | |
| 19 | public function __invoke(ServerRequestInterface $request): ResponseInterface | 
| 20 | { | 
| 21 | $boundedContextId = $request->getAttribute('boundedContextId'); | 
| 22 | $yaml = $request->getAttribute('yaml'); | 
| 23 | $boundedContext = $this->boundedContextHashmap[$boundedContextId]; | 
| 24 | $openapi = $this->openApiGenerator->create($boundedContext); | 
| 25 | $psr17Factory = new Psr17Factory(); | 
| 26 | $responseBody = $psr17Factory->createStream( | 
| 27 | $yaml ? Writer::writeToYaml($openapi) : Writer::writeToJson($openapi, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) | 
| 28 | ); | 
| 29 | return $psr17Factory->createResponse(200) | 
| 30 | ->withBody($responseBody) | 
| 31 | ->withHeader('Content-Type', 'application/openapi+yaml'); | 
| 32 | } | 
| 33 | } |