ServerDocNormalizer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 58
ccs 22
cts 22
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A normalize() 0 29 5
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
     * @param ErrorDocNormalizer  $errorDocNormalizer
22
     */
23 18
    public function __construct(
24
        MethodDocNormalizer $methodDocNormalizer,
25
        TagDocNormalizer $tagDocNormalizer,
26
        ErrorDocNormalizer $errorDocNormalizer
27
    ) {
28 18
        $this->methodDocNormalizer = $methodDocNormalizer;
29 18
        $this->tagDocNormalizer = $tagDocNormalizer;
30 18
        $this->errorDocNormalizer = $errorDocNormalizer;
31
    }
32
33
    /**
34
     * @param ServerDoc $doc
35
     *
36
     * @return array
37
     */
38 18
    public function normalize(ServerDoc $doc) : array
39
    {
40
        // Info
41 18
        $rawInfo = $tagsDoc = $errorsDoc = [];
42 18
        if (null !== $doc->getName()) {
43 5
            $rawInfo['info']['name'] = $doc->getName();
44
        }
45 18
        if (null !== $doc->getVersion()) {
46 3
            $rawInfo['info']['version'] = $doc->getVersion();
47
        }
48
49
        // Tags
50 18
        $tagDocList = array_map([$this->tagDocNormalizer, 'normalize'], $doc->getTagList());
51 18
        if (count($tagDocList)) {
52 5
            $tagsDoc['tags'] = $tagDocList;
53
        }
54
55
        // Errors
56 18
        $serverErrorList = array_map([$this->errorDocNormalizer, 'normalize'], $doc->getServerErrorList());
57 18
        $globalErrorList = array_map([$this->errorDocNormalizer, 'normalize'], $doc->getGlobalErrorList());
58 18
        $errorList = array_merge($serverErrorList, $globalErrorList);
59 18
        if (count($errorList)) {
60 8
            $errorsDoc['errors'] = $errorList;
61
        }
62
63 18
        return $rawInfo
64 18
            + $tagsDoc
65 18
            + ['methods' => array_map([$this->methodDocNormalizer, 'normalize'], $doc->getMethodList())]
66 18
            + $errorsDoc;
67
    }
68
}
69