appendJsonRpcServerErrorsDoc()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 51
ccs 35
cts 35
cp 1
rs 9.376
cc 4
nc 6
nop 1
crap 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Yoanm\SymfonyJsonRpcHttpServerDoc\Listener;
3
4
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInternalErrorException;
5
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidParamsException;
6
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidRequestException;
7
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException;
8
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcParseErrorException;
9
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
10
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ArrayDoc;
11
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ObjectDoc;
12
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc;
13
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\ServerDocCreatedEvent;
14
15
/**
16
 * Class ServerDocCreatedListener
17
 */
18
class ServerDocCreatedListener
19
{
20
    /**
21
     * @param ServerDocCreatedEvent $event
22
     */
23 4
    public function appendJsonRpcServerErrorsDoc(ServerDocCreatedEvent $event) : void
24
    {
25 4
        $event->getDoc()
26 4
            ->addServerError( // Parse Error
27 4
                new ErrorDoc('Parse error', JsonRpcParseErrorException::CODE)
28
            )
29 4
            ->addServerError( // Invalid request
30 4
                (new ErrorDoc('Invalid request', JsonRpcInvalidRequestException::CODE))
31
            )
32 4
            ->addServerError( // Method not found
33 4
                (new ErrorDoc('Method not found', JsonRpcMethodNotFoundException::CODE))
34
            )
35
        ;
36
37 4
        $addParamsValidationError = true;
38
        // Search for existing error in server errors list (could have been already defined by an another bundle)
39
        // @see "yoanm/symfony-jsonrpc-params-sf-constraints-doc" package
40 4
        foreach ($event->getDoc()->getServerErrorList() as $serverError) {
41 4
            if (JsonRpcInvalidParamsException::CODE === $serverError->getCode()) {
42 1
                $addParamsValidationError = false;
43 4
                break;
44
            }
45
        }
46
47 4
        if (true === $addParamsValidationError) {
48 3
            $event->getDoc()->addServerError( // Params validations error
49 3
                (new ErrorDoc('Params validations error', JsonRpcInvalidParamsException::CODE))
50 3
                    ->setDataDoc(
51 3
                        (new ObjectDoc())
52 3
                            ->setAllowMissingSibling(false)
53 3
                            ->setAllowExtraSibling(false)
54 3
                            ->setRequired(true)
55 3
                            ->addSibling(
56 3
                                (new ArrayDoc())
57 3
                                    ->setName(JsonRpcInvalidParamsException::DATA_VIOLATIONS_KEY)
58
                            )
59
                    )
60
            );
61
        }
62
63 4
        $event->getDoc()->addServerError( // Internal error
64 4
            (new ErrorDoc('Internal error', JsonRpcInternalErrorException::CODE))
65 4
                ->setDataDoc(
66 4
                    (new ObjectDoc())
67 4
                        ->setAllowMissingSibling(false)
68 4
                        ->setAllowExtraSibling(false)
69 4
                        ->setRequired(false)
70 4
                        ->addSibling(
71 4
                            (new StringDoc())
72 4
                                ->setName(JsonRpcInternalErrorException::DATA_PREVIOUS_KEY)
73 4
                                ->setDescription('Previous error message')
74
                        )
75
                )
76
        );
77 4
    }
78
}
79