Failed Conditions
Push — release/0.1.0 ( 7e1885...a496de )
by Yo
01:46
created

MethodDocCreatedListener::enhanceMethodDoc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\SymfonyJsonRpcParamsSfConstraintsDoc\Listener;
3
4
use Yoanm\JsonRpcParamsSymfonyConstraintDoc\Infra\Transformer\ConstraintToParamsDocTransformer;
5
use Yoanm\JsonRpcParamsSymfonyValidator\Domain\MethodWithValidatedParamsInterface;
6
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\MethodDocCreatedEvent;
7
8
/**
9
 * Class MethodDocCreatedListener
10
 */
11
class MethodDocCreatedListener
12
{
13
    /** ConstraintToParamsDocTransformer */
14
    private $paramDocConverter;
15
16
    /**
17
     * @param ConstraintToParamsDocTransformer $paramDocConverter
18
     */
19
    public function __construct(ConstraintToParamsDocTransformer $paramDocConverter)
20
    {
21
        $this->paramDocConverter = $paramDocConverter;
22
    }
23
24
    /**
25
     * @param MethodDocCreatedEvent $event
26
     */
27
    public function enhanceMethodDoc(MethodDocCreatedEvent $event)
28
    {
29
        $jsonRpcMethod = $event->getMethod();
30
        if ($jsonRpcMethod instanceof MethodWithValidatedParamsInterface) {
31
            $paramsDoc = $this->paramDocConverter->transform($jsonRpcMethod->getParamsConstraint());
32
33
            // If method have arguments, they are required and not nullable
34
            $paramsDoc->setNullable(false)
35
                ->setRequired(true);
36
37
            $event->getDoc()->setParamsDoc($paramsDoc);
38
        }
39
    }
40
}
41