HttpServerDocCreator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 23
c 1
b 0
f 0
dl 0
loc 65
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A appendMethodsDoc() 0 13 2
A __construct() 0 4 1
A addJsonRpcMethod() 0 3 1
A create() 0 16 3
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 string|null              $jsonRpcEndpoint
28
     */
29 5
    public function __construct(EventDispatcherInterface $dispatcher, string $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);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        $this->dispatcher->/** @scrutinizer ignore-call */ 
53
                           dispatch($event::EVENT_NAME, $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            $this->dispatcher->/** @scrutinizer ignore-call */ 
79
                               dispatch($event::EVENT_NAME, $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
79
80 2
            $serverDoc->addMethod($event->getDoc());
81
        }
82 5
    }
83
}
84