Failed Conditions
Pull Request — release/0.1.0 (#2)
by Yo
03:26
created

HttpServerDocCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServerDoc\Creator;
3
4
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
5
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodAwareInterface;
6
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
7
use Yoanm\JsonRpcServerDoc\Domain\Model\HttpServerDoc;
8
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc;
9
use Yoanm\JsonRpcServerDoc\Domain\Model\ServerDoc;
10
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\MethodDocCreatedEvent;
11
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\ServerDocCreatedEvent;
12
13
/**
14
 * Class HttpServerDocCreator
15
 */
16
class HttpServerDocCreator implements JsonRpcMethodAwareInterface
17
{
18
    /** @var EventDispatcherInterface */
19
    private $dispatcher;
20
    /** @var JsonRpcMethodInterface[] */
21
    private $methodList = [];
22
    /** @var string|null */
23
    private $jsonRpcEndpoint = null;
24
25
    /**
26
     * @param EventDispatcherInterface $dispatcher
27
     * @param null $jsonRpcEndpoint
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $jsonRpcEndpoint is correct as it would always require null to be passed?
Loading history...
28
     */
29 5
    public function __construct(EventDispatcherInterface $dispatcher, $jsonRpcEndpoint = null)
30
    {
31 5
        $this->dispatcher = $dispatcher;
32 5
        $this->jsonRpcEndpoint = $jsonRpcEndpoint;
33 5
    }
34
    /**
35
     * @param string|null $host
36
     *
37
     * @return HttpServerDoc
38
     */
39 5
    public function create($host = null) : HttpServerDoc
40
    {
41 5
        $serverDoc = new HttpServerDoc();
42 5
        if (null !== $this->jsonRpcEndpoint) {
43 5
            $serverDoc->setEndpoint($this->jsonRpcEndpoint);
44
        }
45 5
        if (null !== $host) {
46 1
            $serverDoc->setHost($host);
47
        }
48
49 5
        $this->appendMethodsDoc($serverDoc);
50
51 5
        $event = new ServerDocCreatedEvent($serverDoc);
52 5
        $this->dispatcher->dispatch($event::EVENT_NAME, $event);
53
54 5
        return $serverDoc;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function addJsonRpcMethod(string $methodName, JsonRpcMethodInterface $method) : void
61
    {
62 2
        $this->methodList[$methodName] = $method;
63 2
    }
64
65
    /**
66
     * @param ServerDoc $serverDoc
67
     */
68 5
    protected function appendMethodsDoc(ServerDoc $serverDoc)
69
    {
70 5
        foreach ($this->methodList as $methodName => $method) {
71
            $event = (
72 2
                new MethodDocCreatedEvent(
73 2
                    new MethodDoc($methodName)
74
                )
75
            )
76 2
                ->setMethod($method);
77
78 2
            $this->dispatcher->dispatch($event::EVENT_NAME, $event);
79
80 2
            $serverDoc->addMethod($event->getDoc());
81
        }
82 5
    }
83
}
84