Completed
Pull Request — release/1.0.0 (#8)
by Yo
01:46
created

ServerDocNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 29 5
A __construct() 0 8 1
1
<?php
2
namespace Yoanm\JsonRpcServerDoc\Infra\Normalizer;
3
4
use Yoanm\JsonRpcServerDoc\Domain\Model\ServerDoc;
5
6
/**
7
 * Class ServerDocNormalizer
8
 */
9
class ServerDocNormalizer
10
{
11
    /** @var MethodDocNormalizer */
12
    private $methodDocNormalizer;
13
    /** @var TagDocNormalizer */
14
    private $tagDocNormalizer;
15
    /** @var ErrorDocNormalizer */
16
    private $errorDocNormalizer;
17
18
    /**
19
     * @param MethodDocNormalizer $methodDocNormalizer
20
     * @param TagDocNormalizer    $tagDocNormalizer
21
     */
22 6
    public function __construct(
23
        MethodDocNormalizer $methodDocNormalizer,
24
        TagDocNormalizer $tagDocNormalizer,
25
        ErrorDocNormalizer $errorDocNormalizer
26
    ) {
27 6
        $this->methodDocNormalizer = $methodDocNormalizer;
28 6
        $this->tagDocNormalizer = $tagDocNormalizer;
29 6
        $this->errorDocNormalizer = $errorDocNormalizer;
30 6
    }
31
32
    /**
33
     * @param ServerDoc $doc
34
     *
35
     * @return array
36
     */
37 6
    public function normalize(ServerDoc $doc) : array
38
    {
39
        // Info
40 6
        $rawInfo = $tagsDoc = $errorsDoc = [];
41 6
        if (null !== $doc->getName()) {
42 1
            $rawInfo['info']['name'] = $doc->getName();
43
        }
44 6
        if (null !== $doc->getVersion()) {
45 1
            $rawInfo['info']['version'] = $doc->getVersion();
46
        }
47
48
        // Tags
49 6
        $tagDocList = array_map([$this->tagDocNormalizer, 'normalize'], $doc->getTagList());
50 6
        if (count($tagDocList)) {
51 1
            $tagsDoc['tags'] = $tagDocList;
52
        }
53
54
        // Errors
55 6
        $serverErrorList = array_map([$this->errorDocNormalizer, 'normalize'], $doc->getServerErrorList());
56 6
        $globalErrorList = array_map([$this->errorDocNormalizer, 'normalize'], $doc->getGlobalErrorList());
57 6
        $errorList = array_merge($serverErrorList, $globalErrorList);
58 6
        if (count($errorList)) {
59 2
            $errorsDoc['errors'] = $errorList;
60
        }
61
62
        return $rawInfo
63 6
            + $tagsDoc
64 6
            + ['methods' => array_map([$this->methodDocNormalizer, 'normalize'], $doc->getMethodList())]
65 6
            + $errorsDoc;
66
    }
67
}
68