ConfigurationDenormalizer::denormalize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 66
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 64
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 64
cts 64
cp 1
rs 9.3191
c 0
b 0
f 0
cc 1
eloc 45
nc 1
nop 1
crap 1

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\ComposerConfigManager\Application\Serializer\Normalizer;
3
4
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
5
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile;
6
7
/**
8
 * Class ConfigurationDenormalizer
9
 */
10
class ConfigurationDenormalizer implements DenormalizerInterface
11
{
12
    /** @var AuthorListNormalizer */
13
    private $authorListNormalizer;
14
    /** @var PackageListNormalizer */
15
    private $packageListNormalizer;
16
    /** @var SuggestedPackageListNormalizer */
17
    private $suggestedPackageListNormalizer;
18
    /** @var SupportListNormalizer */
19
    private $supportListNormalizer;
20
    /** @var AutoloadListNormalizer */
21
    private $autoloadListNormalizer;
22
    /** @var ScriptListNormalizer */
23
    private $scriptListNormalizer;
24
25 2
    public function __construct(
26
        AuthorListNormalizer $authorListNormalizer,
27
        PackageListNormalizer $packageListNormalizer,
28
        SuggestedPackageListNormalizer $suggestedPackageListNormalizer,
29
        SupportListNormalizer $supportListNormalizer,
30
        AutoloadListNormalizer $autoloadListNormalizer,
31
        ScriptListNormalizer $scriptListNormalizer
32
    ) {
33 2
        $this->authorListNormalizer = $authorListNormalizer;
34 2
        $this->packageListNormalizer = $packageListNormalizer;
35 2
        $this->suggestedPackageListNormalizer = $suggestedPackageListNormalizer;
36 2
        $this->supportListNormalizer = $supportListNormalizer;
37 2
        $this->autoloadListNormalizer = $autoloadListNormalizer;
38 2
        $this->scriptListNormalizer = $scriptListNormalizer;
39 2
    }
40
41
    /**
42
     * @param array $configuration
43
     *
44
     * @return Configuration
45
     */
46 2
    public function denormalize(array $configuration)
47
    {
48 2
        return new Configuration(
49 2
            $this->valueOrNull($configuration, ConfigurationFile::KEY_NAME),
50 2
            $this->valueOrNull($configuration, ConfigurationFile::KEY_TYPE),
51 2
            $this->valueOrNull($configuration, ConfigurationFile::KEY_LICENSE),
52 2
            $this->valueOrNull($configuration, ConfigurationFile::KEY_VERSION),
53 2
            $this->valueOrNull($configuration, ConfigurationFile::KEY_DESCRIPTION),
54 2
            $this->extractKeywordList($configuration),
55 2
            $this->getNormalizedOrDefault(
56 2
                $this->authorListNormalizer,
57 2
                $configuration,
58 2
                ConfigurationFile::KEY_AUTHORS,
59 2
                []
60 2
            ),
61 2
            $this->getNormalizedOrDefault(
62 2
                $this->packageListNormalizer,
63 2
                $configuration,
64 2
                ConfigurationFile::KEY_PROVIDE,
65 2
                []
66 2
            ),
67 2
            $this->getNormalizedOrDefault(
68 2
                $this->suggestedPackageListNormalizer,
69 2
                $configuration,
70 2
                ConfigurationFile::KEY_SUGGEST,
71 2
                []
72 2
            ),
73 2
            $this->getNormalizedOrDefault(
74 2
                $this->supportListNormalizer,
75 2
                $configuration,
76 2
                ConfigurationFile::KEY_SUPPORT,
77 2
                []
78 2
            ),
79 2
            $this->getNormalizedOrDefault(
80 2
                $this->autoloadListNormalizer,
81 2
                $configuration,
82 2
                ConfigurationFile::KEY_AUTOLOAD,
83 2
                []
84 2
            ),
85 2
            $this->getNormalizedOrDefault(
86 2
                $this->autoloadListNormalizer,
87 2
                $configuration,
88 2
                ConfigurationFile::KEY_AUTOLOAD_DEV,
89 2
                []
90 2
            ),
91 2
            $this->getNormalizedOrDefault(
92 2
                $this->packageListNormalizer,
93 2
                $configuration,
94 2
                ConfigurationFile::KEY_REQUIRE,
95 2
                []
96 2
            ),
97 2
            $this->getNormalizedOrDefault(
98 2
                $this->packageListNormalizer,
99 2
                $configuration,
100 2
                ConfigurationFile::KEY_REQUIRE_DEV,
101 2
                []
102 2
            ),
103 2
            $this->getNormalizedOrDefault(
104 2
                $this->scriptListNormalizer,
105 2
                $configuration,
106 2
                ConfigurationFile::KEY_SCRIPTS,
107 2
                []
108 2
            ),
109 2
            $this->loadUnmanagedPropertyList($configuration)
110 2
        );
111
    }
112
113
    /**
114
     * @param array $configuration
115
     *
116
     * @return array
117
     */
118 2
    protected function loadUnmanagedPropertyList(array $configuration)
119
    {
120
        $managedKeyList = [
121 2
            ConfigurationFile::KEY_NAME => true,
122 2
            ConfigurationFile::KEY_TYPE => true,
123 2
            ConfigurationFile::KEY_LICENSE => true,
124 2
            ConfigurationFile::KEY_VERSION => true,
125 2
            ConfigurationFile::KEY_DESCRIPTION => true,
126 2
            ConfigurationFile::KEY_KEYWORDS => true,
127 2
            ConfigurationFile::KEY_AUTHORS => true,
128 2
            ConfigurationFile::KEY_PROVIDE => true,
129 2
            ConfigurationFile::KEY_SUGGEST => true,
130 2
            ConfigurationFile::KEY_SUPPORT => true,
131 2
            ConfigurationFile::KEY_REQUIRE => true,
132 2
            ConfigurationFile::KEY_REQUIRE_DEV => true,
133 2
            ConfigurationFile::KEY_AUTOLOAD => true,
134 2
            ConfigurationFile::KEY_AUTOLOAD_DEV => true,
135 2
            ConfigurationFile::KEY_SCRIPTS => true,
136 2
        ];
137 2
        $unmanagedPropertyList = [];
138 2
        foreach ($configuration as $key => $value) {
139 1
            if (!isset($managedKeyList[$key])) {
140 1
                $unmanagedPropertyList[$key] = $value;
141 1
            }
142 2
        }
143
144 2
        return $unmanagedPropertyList;
145
    }
146
147
    /**
148
     * @param DenormalizerInterface $denormalizer
149
     * @param array                 $configuration
150
     * @param string                $key
151
     * @param mixed                 $default
152
     *
153
     * @return array
154
     */
155 2
    protected function getNormalizedOrDefault(DenormalizerInterface $denormalizer, array $configuration, $key, $default)
156
    {
157 2
        return isset($configuration[$key]) ? $denormalizer->denormalize($configuration[$key]) : $default;
158
    }
159
160
    /**
161
     * @param array  $configuration
162
     * @param string $key
163
     *
164
     * @return string|null
165
     */
166 2
    protected function valueOrNull(array $configuration, $key)
167
    {
168 2
        return isset($configuration[$key]) ? $configuration[$key] : null;
169
    }
170
171
    /**
172
     * @param array $configuration
173
     *
174
     * @return array
175
     */
176 2
    protected function extractKeywordList(array $configuration)
177
    {
178 2
        return isset($configuration[ConfigurationFile::KEY_KEYWORDS])
179 2
            ? $configuration[ConfigurationFile::KEY_KEYWORDS]
180 2
            : [];
181
    }
182
}
183