appendJsonRpcServerErrorsDoc()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 63
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 54
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 63
ccs 54
cts 54
cp 1
rs 9.1781
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\IntegerDoc;
12
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ObjectDoc;
13
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc;
14
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\ServerDocCreatedEvent;
15
16
/**
17
 * Class ServerDocCreatedListener
18
 */
19
class ServerDocCreatedListener
20
{
21
    /**
22
     * @param ServerDocCreatedEvent $event
23
     */
24 7
    public function appendJsonRpcServerErrorsDoc(ServerDocCreatedEvent $event) : void
25
    {
26 7
        $event->getDoc()
27 7
            ->addServerError( // Parse Error
28 7
                new ErrorDoc('Parse error', JsonRpcParseErrorException::CODE)
29 7
            )
30 7
            ->addServerError( // Invalid request
31 7
                (new ErrorDoc('Invalid request', JsonRpcInvalidRequestException::CODE))
32 7
            )
33 7
            ->addServerError( // Method not found
34 7
                (new ErrorDoc('Method not found', JsonRpcMethodNotFoundException::CODE))
35 7
            )
36 7
        ;
37
38 7
        $addParamsValidationError = true;
39
        // Search for existing error in server errors list (could have been already defined by an another bundle)
40
        // @see "yoanm/symfony-jsonrpc-params-sf-constraints-doc" package
41 7
        foreach ($event->getDoc()->getServerErrorList() as $serverError) {
42 7
            if (JsonRpcInvalidParamsException::CODE === $serverError->getCode()) {
43 1
                $addParamsValidationError = false;
44 1
                break;
45
            }
46
        }
47
48 7
        if (true === $addParamsValidationError) {
49 6
            $event->getDoc()->addServerError( // Params validations error
50 6
                (new ErrorDoc('Params validations error', JsonRpcInvalidParamsException::CODE))
51 6
                    ->setDataDoc(
52 6
                        (new ObjectDoc())
53 6
                            ->setAllowMissingSibling(false)
54 6
                            ->setAllowExtraSibling(false)
55 6
                            ->setRequired(true)
56 6
                            ->addSibling(
57 6
                                (new ArrayDoc())
58 6
                                    ->setName(JsonRpcInvalidParamsException::DATA_VIOLATIONS_KEY)
59 6
                            )
60 6
                    )
61 6
            );
62
        }
63
64 7
        $event->getDoc()->addServerError( // Internal error
65 7
            (new ErrorDoc('Internal error', JsonRpcInternalErrorException::CODE))
66 7
                ->setDataDoc(
67 7
                    (new ObjectDoc())
68 7
                        ->setAllowMissingSibling(true)
69 7
                        ->setAllowExtraSibling(false)
70 7
                        ->setRequired(false)
71 7
                        ->addSibling((
72 7
                            new StringDoc())
73 7
                                ->setName('_class')
74 7
                                ->setDescription('Exception class'))
75 7
                        ->addSibling((
76 7
                            new IntegerDoc())
77 7
                                ->setName('_code')
78 7
                                ->setDescription('Exception code'))
79 7
                        ->addSibling((
80 7
                            new StringDoc())
81 7
                                ->setName('_message')
82 7
                                ->setDescription('Exception message'))
83 7
                        ->addSibling((
84 7
                            new ArrayDoc())
85 7
                                ->setName('_trace')
86 7
                                ->setDescription('PHP stack trace'))
87 7
                )
88 7
        );
89
    }
90
}
91