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

MethodDocCreatedListener   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 27
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A enhanceMethodDoc() 0 11 2
A __construct() 0 3 1
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