Passed
Push — master ( 20a97f...aaa4d8 )
by Yo
45s queued 10s
created

ServerDocCreatedListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 50
ccs 33
cts 33
cp 1
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 2
    public function enhanceParamValidationErrorDoc(ServerDocCreatedEvent $event) : void
20
    {
21 2
        $paramsValidationError = null;
22
23
        // Search for existing error in server errors list
24 2
        foreach ($event->getDoc()->getServerErrorList() as $serverError) {
25 1
            if (JsonRpcInvalidParamsException::CODE === $serverError->getCode()) {
26 1
                $paramsValidationError = $serverError;
27 1
                break;
28
            }
29
        }
30
31 2
        if (null === $paramsValidationError) {
32
            // In case not found => Create new one and append it to server error list
33 1
            $paramsValidationError = new ErrorDoc('Params validations error', JsonRpcInvalidParamsException::CODE);
34 1
            $event->getDoc()->addServerError($paramsValidationError);
35
        }
36
37
        // In case error was already defined, the data doc will be overridden
38 2
        $paramsValidationError->setDataDoc(
39 2
            (new ObjectDoc())
40 2
                ->setAllowMissingSibling(false)
41 2
                ->setAllowExtraSibling(false)
42 2
                ->setRequired(true)
43 2
                ->addSibling(
44 2
                    (new ArrayDoc())
45 2
                        ->setName(JsonRpcInvalidParamsException::DATA_VIOLATIONS_KEY)
46 2
                        ->setItemValidation(
47 2
                            (new ObjectDoc())
48 2
                                ->setAllowExtraSibling(false)
49 2
                                ->setRequired(true)
50 2
                                ->addSibling(
51 2
                                    (new StringDoc())
52 2
                                        ->setName('path')
53 2
                                        ->setExample('[key]')
54 2
                                        ->setRequired(true)
55
                                )
56 2
                                ->addSibling(
57 2
                                    (new StringDoc())
58 2
                                        ->setName('message')
59 2
                                        ->setRequired(true)
60
                                )
61 2
                                ->addSibling(
62 2
                                    (new StringDoc())
63 2
                                        ->setName('code')
64
                                )
65
                        )
66
                )
67
        );
68 2
    }
69
}
70