Failed Conditions
Push — master ( d49c03...6343d0 )
by Bernhard
05:03
created

RootModuleFileConverter::addJsonToRootModuleFile()   C

Complexity

Conditions 11
Paths 80

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
ccs 21
cts 21
cp 1
rs 5.2653
cc 11
eloc 20
nc 80
nop 2
crap 11

How to fix   Complexity   

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
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Module;
13
14
use Puli\Manager\Api\Environment;
15
use Puli\Manager\Api\Module\InstallInfo;
16
use Puli\Manager\Api\Module\RootModuleFile;
17
use Puli\Manager\Assert\Assert;
18
use Rhumsaa\Uuid\Uuid;
19
use stdClass;
20
21
/**
22
 * Converts root module files to JSON and back.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class RootModuleFileConverter extends ModuleFileConverter
29
{
30
    /**
31
     * The default order of the keys in the written module file.
32
     *
33
     * @var string[]
34
     */
35
    private static $keyOrder = array(
36
        'version',
37
        'name',
38
        'path-mappings',
39
        'bindings',
40
        'binding-types',
41
        'override',
42
        'override-order',
43
        'config',
44
        'plugins',
45
        'extra',
46
        'modules',
47
    );
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 6 View Code Duplication
    public function toJson($moduleFile, array $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 6
        Assert::isInstanceOf($moduleFile, 'Puli\Manager\Api\Module\RootModuleFile');
55
56 6
        $jsonData = new stdClass();
57
58 6
        $this->addModuleFileToJson($moduleFile, $jsonData);
59 6
        $this->addRootModuleFileToJson($moduleFile, $jsonData);
60
61
        // Sort according to key order
62 6
        $jsonArray = (array) $jsonData;
63 6
        $orderedKeys = array_intersect_key(array_flip(self::$keyOrder), $jsonArray);
64
65 6
        return (object) array_replace($orderedKeys, $jsonArray);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 30
    public function fromJson($jsonData, array $options = array())
72
    {
73 30
        $path = isset($options['path']) ? $options['path'] : null;
74 30
        $baseConfig = isset($options['baseConfig']) ? $options['baseConfig'] : null;
75
76 30
        Assert::isInstanceOf($jsonData, 'stdClass');
77 30
        Assert::nullOrString($path, 'The "path" option should be null or a string. Got: %s');
78 30
        Assert::nullOrIsInstanceOf($baseConfig, 'Puli\Manager\Api\Config\Config', 'The "baseConfig" option should be null or an instance of %2$s. Got: %s');
79
80 30
        $moduleFile = new RootModuleFile(null, $path, $baseConfig);
81
82 30
        $this->addJsonToModuleFile($jsonData, $moduleFile);
83 30
        $this->addJsonToRootModuleFile($jsonData, $moduleFile);
84
85 30
        return $moduleFile;
86
    }
87
88 6
    protected function addRootModuleFileToJson(RootModuleFile $moduleFile, stdClass $jsonData)
89
    {
90 6
        $overrideOrder = $moduleFile->getOverrideOrder();
91 6
        $installInfos = $moduleFile->getInstallInfos();
92
93
        // Pass false to exclude base configuration values
94 6
        $configValues = $moduleFile->getConfig()->toRawArray(false);
95
96 6
        if (count($overrideOrder) > 0) {
97 1
            $jsonData->{'override-order'} = $overrideOrder;
98
        }
99
100 6
        if (count($configValues) > 0) {
101 1
            $jsonData->config = (object) $configValues;
102
        }
103
104 6
        if (array() !== $moduleFile->getPluginClasses()) {
105 2
            $jsonData->plugins = $moduleFile->getPluginClasses();
106
107 2
            sort($jsonData->plugins);
108
        }
109
110 6
        if (count($installInfos) > 0) {
111 3
            $modulesData = array();
112
113 3
            foreach ($installInfos as $installInfo) {
114 3
                $installData = new stdClass();
115 3
                $installData->{'install-path'} = $installInfo->getInstallPath();
116
117 3
                if (InstallInfo::DEFAULT_INSTALLER_NAME !== $installInfo->getInstallerName()) {
118 1
                    $installData->installer = $installInfo->getInstallerName();
119
                }
120
121 3
                if ($installInfo->hasDisabledBindingUuids()) {
122 2
                    $installData->{'disabled-bindings'} = array();
123
124 2
                    foreach ($installInfo->getDisabledBindingUuids() as $uuid) {
125 2
                        $installData->{'disabled-bindings'}[] = $uuid->toString();
126
                    }
127
128 2
                    sort($installData->{'disabled-bindings'});
129
                }
130
131 3
                if (Environment::PROD !== $installInfo->getEnvironment()) {
132 1
                    $installData->env = $installInfo->getEnvironment();
133
                }
134
135 3
                $modulesData[$installInfo->getModuleName()] = $installData;
136
            }
137
138 3
            ksort($modulesData);
139
140 3
            $jsonData->modules = (object) $modulesData;
141
        }
142 6
    }
143
144 30
    protected function addJsonToRootModuleFile(stdClass $jsonData, RootModuleFile $moduleFile)
145
    {
146 30
        if (isset($jsonData->{'override-order'})) {
147 1
            $moduleFile->setOverrideOrder((array) $jsonData->{'override-order'});
148
        }
149
150 30
        if (isset($jsonData->plugins)) {
151 27
            $moduleFile->setPluginClasses($jsonData->plugins);
152
        }
153
154 30
        if (isset($jsonData->config)) {
155 2
            $config = $moduleFile->getConfig();
156
157 2
            foreach ($this->objectsToArrays($jsonData->config) as $key => $value) {
158 2
                $config->set($key, $value);
159
            }
160
        }
161
162 30
        if (isset($jsonData->modules)) {
163 27
            foreach ($jsonData->modules as $moduleName => $moduleData) {
164 27
                $installInfo = new InstallInfo($moduleName, $moduleData->{'install-path'});
165
166 27
                if (isset($moduleData->env)) {
167 1
                    $installInfo->setEnvironment($moduleData->env);
168
                }
169
170 27
                if (isset($moduleData->installer)) {
171 1
                    $installInfo->setInstallerName($moduleData->installer);
172
                }
173
174 27
                if (isset($moduleData->{'disabled-bindings'})) {
175 1
                    foreach ($moduleData->{'disabled-bindings'} as $uuid) {
176 1
                        $installInfo->addDisabledBindingUuid(Uuid::fromString($uuid));
177
                    }
178
                }
179
180 27
                $moduleFile->addInstallInfo($installInfo);
181
            }
182
        }
183 30
    }
184
185 2 View Code Duplication
    private function objectsToArrays($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187 2
        $data = (array) $data;
188
189 2
        foreach ($data as $key => $value) {
190 2
            $data[$key] = is_object($value) ? $this->objectsToArrays($value) : $value;
191
        }
192
193 2
        return $data;
194
    }
195
}
196