MethodDocCreatedListener::enhanceMethodDoc()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
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 3
    public function __construct(ConstraintToParamsDocTransformer $paramDocConverter)
20
    {
21 3
        $this->paramDocConverter = $paramDocConverter;
22
    }
23
24
    /**
25
     * @param MethodDocCreatedEvent $event
26
     *
27
     * @throws \ReflectionException
28
     */
29 3
    public function enhanceMethodDoc(MethodDocCreatedEvent $event)
30
    {
31 3
        $jsonRpcMethod = $event->getMethod();
32 3
        if ($jsonRpcMethod instanceof MethodWithValidatedParamsInterface) {
33 2
            $paramsDoc = $this->paramDocConverter->transform($jsonRpcMethod->getParamsConstraint());
34
35
            // If method have arguments, they are required and not nullable
36 2
            $paramsDoc->setNullable(false)
37 2
                ->setRequired(true);
38
39 2
            $event->getDoc()->setParamsDoc($paramsDoc);
40
        }
41
    }
42
}
43