Passed
Push — master ( 80b57f...2460a5 )
by Yo
02:49
created

ConfigurationNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 163
ccs 72
cts 72
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
B normalize() 0 83 1
A appendIfNotEmpty() 0 8 2
A appendIfDefined() 0 8 2
1
<?php
2
namespace Yoanm\ComposerConfigManager\Application\Serializer\Normalizer;
3
4
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
5
6
class ConfigurationNormalizer
7
{
8
    const KEY_NAME = 'name';
9
    const KEY_TYPE = 'type';
10
    const KEY_LICENSE = 'license';
11
    const KEY_VERSION = 'version';
12
    const KEY_DESCRIPTION = 'description';
13
    const KEY_KEYWORDS = 'keywords';
14
    const KEY_AUTHORS = 'authors';
15
    const KEY_PROVIDE = 'provide';
16
    const KEY_SUGGEST = 'suggest';
17
    const KEY_SUPPORT = 'support';
18
    const KEY_REQUIRE = 'require';
19
    const KEY_REQUIRE_DEV = 'require-dev';
20
    const KEY_AUTOLOAD = 'autoload';
21
    const KEY_AUTOLOAD_DEV = 'autoload-dev';
22
    const KEY_SCRIPT = 'script';
23
24
    /** @var AuthorListNormalizer */
25
    private $authorListNormalizer;
26
    /** @var PackageListNormalizer */
27
    private $packageListNormalizer;
28
    /** @var SuggestedPackageListNormalizer */
29
    private $suggestedPackageListNormalizer;
30
    /** @var SupportListNormalizer */
31
    private $supportListNormalizer;
32
    /** @var AutoloadListNormalizer */
33
    private $autoloadListNormalizer;
34
    /** @var ScriptListNormalizer */
35
    private $scriptListNormalizer;
36
37 4
    public function __construct(
38
        AuthorListNormalizer $authorListNormalizer,
39
        PackageListNormalizer $packageListNormalizer,
40
        SuggestedPackageListNormalizer $suggestedPackageListNormalizer,
41
        SupportListNormalizer $supportListNormalizer,
42
        AutoloadListNormalizer $autoloadListNormalizer,
43
        ScriptListNormalizer $scriptListNormalizer
44
    ) {
45 4
        $this->authorListNormalizer = $authorListNormalizer;
46 4
        $this->packageListNormalizer = $packageListNormalizer;
47 4
        $this->suggestedPackageListNormalizer = $suggestedPackageListNormalizer;
48 4
        $this->supportListNormalizer = $supportListNormalizer;
49 4
        $this->autoloadListNormalizer = $autoloadListNormalizer;
50 4
        $this->scriptListNormalizer = $scriptListNormalizer;
51 4
    }
52
53 4
    public function normalize(Configuration $configuration)
54
    {
55
        $normalizedConfiguration = [
56 4
            self::KEY_NAME => $configuration->getPackageName(),
57 4
            self::KEY_TYPE =>$configuration->getType(),
58 4
            self::KEY_LICENSE => $configuration->getLicense()
59 4
        ];
60
61
        // package version
62 4
        $normalizedConfiguration = $this->appendIfDefined(
63 4
            $normalizedConfiguration,
64 4
            $configuration->getPackageVersion(),
65
            self::KEY_VERSION
66 4
        );
67
        // description
68 4
        $normalizedConfiguration = $this->appendIfDefined(
69 4
            $normalizedConfiguration,
70 4
            $configuration->getDescription(),
71
            self::KEY_DESCRIPTION
72 4
        );
73
        // keywords
74 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
75 4
            $normalizedConfiguration,
76 4
            $configuration->getKeywordList(),
77
            self::KEY_KEYWORDS
78 4
        );
79
        // authors
80 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
81 4
            $normalizedConfiguration,
82 4
            $this->authorListNormalizer->normalize($configuration->getAuthorList()),
83
            self::KEY_AUTHORS
84 4
        );
85
        // provide
86 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
87 4
            $normalizedConfiguration,
88 4
            $this->packageListNormalizer->normalize($configuration->getProvidedPackageList()),
89
            self::KEY_PROVIDE
90 4
        );
91
        // suggest
92 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
93 4
            $normalizedConfiguration,
94 4
            $this->suggestedPackageListNormalizer->normalize($configuration->getSuggestedPackageList()),
95
            self::KEY_SUGGEST
96 4
        );
97
        // support
98 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
99 4
            $normalizedConfiguration,
100 4
            $this->supportListNormalizer->normalize($configuration->getSupportList()),
101
            self::KEY_SUPPORT
102 4
        );
103
        // require
104 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
105 4
            $normalizedConfiguration,
106 4
            $this->packageListNormalizer->normalize($configuration->getRequiredPackageList()),
107
            self::KEY_REQUIRE
108 4
        );
109
        // require-dev
110 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
111 4
            $normalizedConfiguration,
112 4
            $this->packageListNormalizer->normalize($configuration->getRequiredDevPackageList()),
113
            self::KEY_REQUIRE_DEV
114 4
        );
115
        // autoload
116 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
117 4
            $normalizedConfiguration,
118 4
            $this->autoloadListNormalizer->normalize($configuration->getAutoloadList()),
119
            self::KEY_AUTOLOAD
120 4
        );
121
        // autoload-dev
122 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
123 4
            $normalizedConfiguration,
124 4
            $this->autoloadListNormalizer->normalize($configuration->getAutoloadDevList()),
125
            self::KEY_AUTOLOAD_DEV
126 4
        );
127
        // script
128 4
        $normalizedConfiguration = $this->appendIfNotEmpty(
129 4
            $normalizedConfiguration,
130 4
            $this->scriptListNormalizer->normalize($configuration->getScriptList()),
131
            self::KEY_SCRIPT
132 4
        );
133
134 4
        return $normalizedConfiguration;
135
    }
136
137
    /**
138
     * @param array  $normalizedConfiguration
139
     * @param array  $list
140
     * @param string $key
141
     *
142
     * @return array
143
     */
144 4
    protected function appendIfNotEmpty(array $normalizedConfiguration, array $list, $key)
145
    {
146 4
        if (count($list)) {
147 2
            $normalizedConfiguration[$key] = $list;
148 2
        }
149
150 4
        return $normalizedConfiguration;
151
    }
152
153
    /**
154
     * @param array  $normalizedConfiguration
155
     * @param string $value
156
     * @param string $key
157
     *
158
     * @return array
159
     */
160 4
    protected function appendIfDefined(array $normalizedConfiguration, $value, $key)
161
    {
162 4
        if ($value) {
163 3
            $normalizedConfiguration[$key] = $value;
164 3
        }
165
166 4
        return $normalizedConfiguration;
167
    }
168
}
169