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

ServerDocCreatedListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 50
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A enhanceParamValidationErrorDoc() 0 45 4
1
<?php
2
namespace Yoanm\SymfonyJsonRpcParamsSfConstraintsDoc\Listener;
3
4
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidParamsException;
5
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
6
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ArrayDoc;
7
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ObjectDoc;
8
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc;
9
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\ServerDocCreatedEvent;
10
11
/**
12
 * Class ServerDocCreatedListener
13
 */
14
class ServerDocCreatedListener
15
{
16
    /**
17
     * @param ServerDocCreatedEvent $event
18
     */
19
    public function enhanceParamValidationErrorDoc(ServerDocCreatedEvent $event) : void
20
    {
21
        $paramsValidationError = null;
22
23
        // Search for existing error in server errors list
24
        foreach ($event->getDoc()->getServerErrorList() as $serverError) {
25
            if (JsonRpcInvalidParamsException::CODE === $serverError->getCode()) {
26
                $paramsValidationError = $serverError;
27
                break;
28
            }
29
        }
30
31
        if (null === $paramsValidationError) {
32
            // In case not found => Create new one and append it to server error list
33
            $paramsValidationError = new ErrorDoc('Params validations error', JsonRpcInvalidParamsException::CODE);
34
            $event->getDoc()->addServerError($paramsValidationError);
35
        }
36
37
        // In case error was already defined, the data doc will be overridden
38
        $paramsValidationError->setDataDoc(
39
            (new ObjectDoc())
40
                ->setAllowMissingSibling(false)
41
                ->setAllowExtraSibling(false)
42
                ->setRequired(true)
43
                ->addSibling(
44
                    (new ArrayDoc())
45
                        ->setName(JsonRpcInvalidParamsException::DATA_VIOLATIONS_KEY)
46
                        ->setItemValidation(
47
                            (new ObjectDoc())
48
                                ->setAllowExtraSibling(false)
49
                                ->setRequired(true)
50
                                ->addSibling(
51
                                    (new StringDoc())
52
                                        ->setName('path')
53
                                        ->setExample('[key]')
54
                                        ->setRequired(true)
55
                                )
56
                                ->addSibling(
57
                                    (new StringDoc())
58
                                        ->setName('message')
59
                                        ->setRequired(true)
60
                                )
61
                                ->addSibling(
62
                                    (new StringDoc())
63
                                        ->setName('code')
64
                                )
65
                        )
66
                )
67
        );
68
    }
69
}
70