Passed
Push — feature/init ( aaad7a...5c8912 )
by Yo
02:13
created

DocProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServerOpenAPIDoc\Provider;
3
4
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
5
use Yoanm\JsonRpcHttpServerOpenAPIDoc\Infra\Normalizer\DocNormalizer;
6
use Yoanm\SymfonyJsonRpcHttpServerDoc\Creator\HttpServerDocCreator;
7
use Yoanm\SymfonyJsonRpcHttpServerDoc\Provider\DocProviderInterface;
8
use Yoanm\SymfonyJsonRpcHttpServerOpenAPIDoc\Event\OpenAPIDocCreatedEvent;
9
10
/**
11
 * Class DocProvider
12
 */
13
class DocProvider implements DocProviderInterface
14
{
15
    /** @var EventDispatcherInterface */
16
    private $dispatcher;
17
    /** @var HttpServerDocCreator */
18
    private $httpServerDocCreator;
19
    /** @var DocNormalizer */
20
    private $docNormalizer;
21
22
    /**
23
     * @param EventDispatcherInterface $dispatcher
24
     * @param HttpServerDocCreator         $HttpServerDocCreator
25
     * @param DocNormalizer     $docNormalizer
26
     */
27
    public function __construct(
28
        EventDispatcherInterface $dispatcher,
29
        HttpServerDocCreator $HttpServerDocCreator,
30
        DocNormalizer $docNormalizer
31
    ) {
32
        $this->dispatcher = $dispatcher;
33
        $this->httpServerDocCreator = $HttpServerDocCreator;
34
        $this->docNormalizer = $docNormalizer;
35
    }
36
37
    /**
38
     * @param string|null $host
39
     *
40
     * @return array
41
     */
42
    public function getDoc($host = null) : array
43
    {
44
        $rawDoc = $this->httpServerDocCreator->create($host);
45
46
        $openApiDoc = $this->docNormalizer->normalize($rawDoc);
47
48
        $event = new OpenAPIDocCreatedEvent($openApiDoc, $rawDoc);
49
        $this->dispatcher->dispatch($event::EVENT_NAME, $event);
50
51
        return $event->getOpenAPIDoc();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $event->getOpenAPIDoc() returns the type Yoanm\JsonRpcServerDoc\Domain\Model\HttpServerDoc which is incompatible with the type-hinted return array.
Loading history...
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function supports($filename, $host = null) : bool
58
    {
59
        return 'openapi.json' === $filename;
60
    }
61
}
62