Completed
Push — release/1.0.0 ( faa89b...153a36 )
by Yo
01:39
created

MethodDocNormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A appendErrorsSchema() 0 20 3
B normalize() 0 30 5
1
<?php
2
namespace Yoanm\JsonRpcServerDoc\Infra\Normalizer;
3
4
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc;
5
6
/**
7
 * Class MethodDocNormalizer
8
 */
9
class MethodDocNormalizer
10
{
11
    /** @var TypeDocNormalizer */
12
    private $typeDocNormalizer;
13
    /** @var ErrorDocNormalizer */
14
    private $errorDocNormalizer;
15
16
    /**
17
     * @param TypeDocNormalizer $typeDocNormalizer
18
     */
19 7
    public function __construct(
20
        TypeDocNormalizer $typeDocNormalizer,
21
        ErrorDocNormalizer $errorDocNormalizer
22
    ) {
23 7
        $this->typeDocNormalizer = $typeDocNormalizer;
24 7
        $this->errorDocNormalizer = $errorDocNormalizer;
25 7
    }
26
27
    /**
28
     * @param MethodDoc $doc
29
     *
30
     * @return array
31
     */
32 7
    public function normalize(MethodDoc $doc) : array
33
    {
34 7
        $docDescription = $docTags = $paramsSchema = $responseSchema = [];
35
36 7
        if (null !== $doc->getDescription()) {
37 1
            $docDescription['description'] = $doc->getDescription();
38
        }
39 7
        if (count($doc->getTagList())) {
40 1
            $docTags['tags'] = $doc->getTagList();
41
        }
42 7
        if (null !== $doc->getParamsDoc()) {
43
            $paramsSchema = [
44 1
                'params' => $this->typeDocNormalizer->normalize($doc->getParamsDoc())
45
            ];
46
        }
47
        // Create custom result schema only if provided
48 7
        if (null !== $doc->getResultDoc()) {
49 1
            $responseSchema['result'] = $this->typeDocNormalizer->normalize($doc->getResultDoc());
50
        }
51
52
53
        return [
54 7
            'identifier' => $doc->getIdentifier(),
55 7
            'name' =>$doc->getMethodName(),
56
        ]
57 7
            + $docDescription
58 7
            + $docTags
59 7
            + $paramsSchema
60 7
            + $responseSchema
61 7
            + $this->appendErrorsSchema($doc);
62
        ;
63
    }
64
65
    /**
66
     * @param MethodDoc $docObject
67
     *
68
     * @return array
69
     */
70 7
    private function appendErrorsSchema(MethodDoc $docObject)
71
    {
72 7
        $docArray = [];
73
        // Create custom result schema only if provided
74 7
        if (count($docObject->getCustomErrorList()) || count($docObject->getGlobalErrorRefList())) {
75 2
            $docArray['errors'] = array_merge(
76 2
                array_map(
77 2
                    [$this->errorDocNormalizer, 'normalize'],
78 2
                    $docObject->getCustomErrorList()
79
                ),
80 2
                array_map(
81
                    function ($errorIdentifier) {
82 1
                        return ['$ref' => sprintf('#/errors/%s', $errorIdentifier)];
83 2
                    },
84 2
                    $docObject->getGlobalErrorRefList()
85
                )
86
            );
87
        }
88
89 7
        return $docArray;
90
    }
91
}
92