Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AddBasicAuthToOpenApiSubscriber
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 getSubscribedEvents
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 onOpenApiSchemaGenerated
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Common\BasicAuth;
3
4use Apie\RestApi\Events\OpenApiSchemaGeneratedEvent;
5use cebe\openapi\spec\SecurityScheme;
6use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
8class AddBasicAuthToOpenApiSubscriber implements EventSubscriberInterface
9{
10    public static function getSubscribedEvents()
11    {
12        return [
13            OpenApiSchemaGeneratedEvent::class => 'onOpenApiSchemaGenerated'
14        ];
15    }
16
17    public function onOpenApiSchemaGenerated(OpenApiSchemaGeneratedEvent $event): void
18    {
19        $openApi = $event->openApi;
20        $securitySchemes = $openApi->components->securitySchemes ?? [];
21        
22        $securitySchemes['BasicAuth'] = new SecurityScheme([
23            'type' => 'http',
24            'scheme' => 'basic',
25        ]);
26
27        $openApi->components->securitySchemes = $securitySchemes;
28
29        $security = $openApi->security ?? [];
30        $security[] = ['BasicAuth' => []];
31        $security[] = []; // add anonymous security option as well....
32        $openApi->security = $security;
33    }
34}