src/OpenApi/JwtDecorator.php line 21

Open in your IDE?
  1. <?php
  2. // api/src/OpenApi/JwtDecorator.php
  3. declare(strict_types = 1);
  4. namespace App\OpenApi;
  5. use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
  6. use ApiPlatform\Core\OpenApi\OpenApi;
  7. use ApiPlatform\Core\OpenApi\Model;
  8. final class JwtDecorator implements OpenApiFactoryInterface
  9. {
  10. private $decorated;
  11. public function __construct(OpenApiFactoryInterface $decorated)
  12. {
  13. $this->decorated = $decorated;
  14. }
  15. public function __invoke(array $context = []): OpenApi
  16. {
  17. $openApi = ($this->decorated)($context);
  18. $schemas = $openApi->getComponents()->getSchemas();
  19. $schemas['Token'] = new \ArrayObject([
  20. 'type' => 'object',
  21. 'properties' => [
  22. 'token' => [
  23. 'type' => 'string',
  24. 'readOnly' => true
  25. ]
  26. ]
  27. ]);
  28. $schemas['Credentials'] = new \ArrayObject([
  29. 'type' => 'object',
  30. 'properties' => [
  31. 'email' => [
  32. 'type' => 'string',
  33. 'example' => 'johndoe@example.com'
  34. ],
  35. 'password' => [
  36. 'type' => 'string',
  37. 'example' => 'apassword'
  38. ]
  39. ]
  40. ]);
  41. $pathItem = new Model\PathItem(
  42. ref: 'JWT Token',
  43. post: new Model\Operation(
  44. operationId: 'postCredentialsItem',
  45. tags: ['Token'],
  46. responses: [
  47. '200' => [
  48. 'description' => 'Get JWT token',
  49. 'content' => [
  50. 'application/json' => [
  51. 'schema' => [
  52. '$ref' => '#/components/schemas/Token',
  53. ],
  54. ],
  55. ],
  56. ],
  57. ],
  58. summary: 'Get JWT token to login.',
  59. requestBody: new Model\RequestBody(
  60. description: 'Generate new JWT Token',
  61. content: new \ArrayObject([
  62. 'application/json' => [
  63. 'schema' => [
  64. '$ref' => '#/components/schemas/Credentials',
  65. ],
  66. ],
  67. ]),
  68. ),
  69. ),
  70. );
  71. $openApi->getPaths()->addPath('/login', $pathItem);
  72. return $openApi;
  73. }
  74. }