RootModuleFileConverter::addJsonToRootModuleFile()   C
last analyzed

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 2
Bugs 0 Features 0
Metric Value
c 2
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
        '$schema',
37
        'name',
38
        'resources',
39
        'bindings',
40
        'binding-types',
41
        'depend',
42
        'order',
43
        'config',
44
        'plugins',
45
        'extra',
46
        'modules',
47
    );
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 6
    public function toJson($moduleFile, array $options = array())
53
    {
54 6
        Assert::isInstanceOf($moduleFile, 'Puli\Manager\Api\Module\RootModuleFile');
55
56 6
        $jsonData = new stdClass();
57 6
        $jsonData->{'$schema'} = sprintf(self::SCHEMA, self::VERSION);
58
59 6
        $this->addModuleFileToJson($moduleFile, $jsonData);
60 6
        $this->addRootModuleFileToJson($moduleFile, $jsonData);
61
62
        // Sort according to key order
63 6
        $jsonArray = (array) $jsonData;
64 6
        $orderedKeys = array_intersect_key(array_flip(self::$keyOrder), $jsonArray);
65
66 6
        return (object) array_replace($orderedKeys, $jsonArray);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 30
    public function fromJson($jsonData, array $options = array())
73
    {
74 30
        $path = isset($options['path']) ? $options['path'] : null;
75 30
        $baseConfig = isset($options['baseConfig']) ? $options['baseConfig'] : null;
76
77 30
        Assert::isInstanceOf($jsonData, 'stdClass');
78 30
        Assert::nullOrString($path, 'The "path" option should be null or a string. Got: %s');
79 30
        Assert::nullOrIsInstanceOf($baseConfig, 'Puli\Manager\Api\Config\Config', 'The "baseConfig" option should be null or an instance of %2$s. Got: %s');
80
81 30
        $moduleFile = new RootModuleFile(null, $path, $baseConfig);
82 30
        $moduleFile->setVersion($this->versioner->parseVersion($jsonData));
83
84 30
        $this->addJsonToModuleFile($jsonData, $moduleFile);
85 30
        $this->addJsonToRootModuleFile($jsonData, $moduleFile);
86
87 30
        return $moduleFile;
88
    }
89
90 6
    protected function addRootModuleFileToJson(RootModuleFile $moduleFile, stdClass $jsonData)
91
    {
92 6
        $moduleOrder = $moduleFile->getModuleOrder();
93 6
        $installInfos = $moduleFile->getInstallInfos();
94
95
        // Pass false to exclude base configuration values
96 6
        $configValues = $moduleFile->getConfig()->toRawArray(false);
97
98 6
        if (count($moduleOrder) > 0) {
99 1
            $jsonData->order = $moduleOrder;
100
        }
101
102 6
        if (count($configValues) > 0) {
103 1
            $jsonData->config = (object) $configValues;
104
        }
105
106 6
        if (array() !== $moduleFile->getPluginClasses()) {
107 2
            $jsonData->plugins = $moduleFile->getPluginClasses();
108
109 2
            sort($jsonData->plugins);
110
        }
111
112 6
        if (count($installInfos) > 0) {
113 3
            $modulesData = array();
114
115 3
            foreach ($installInfos as $installInfo) {
116 3
                $installData = new stdClass();
117 3
                $installData->{'install-path'} = $installInfo->getInstallPath();
118
119 3
                if (InstallInfo::DEFAULT_INSTALLER_NAME !== $installInfo->getInstallerName()) {
120 1
                    $installData->installer = $installInfo->getInstallerName();
121
                }
122
123 3
                if ($installInfo->hasDisabledBindingUuids()) {
124 2
                    $installData->{'disabled-bindings'} = array();
125
126 2
                    foreach ($installInfo->getDisabledBindingUuids() as $uuid) {
127 2
                        $installData->{'disabled-bindings'}[] = $uuid->toString();
128
                    }
129
130 2
                    sort($installData->{'disabled-bindings'});
131
                }
132
133 3
                if (Environment::PROD !== $installInfo->getEnvironment()) {
134 1
                    $installData->env = $installInfo->getEnvironment();
135
                }
136
137 3
                $modulesData[$installInfo->getModuleName()] = $installData;
138
            }
139
140 3
            ksort($modulesData);
141
142 3
            $jsonData->modules = (object) $modulesData;
143
        }
144 6
    }
145
146 30
    protected function addJsonToRootModuleFile(stdClass $jsonData, RootModuleFile $moduleFile)
147
    {
148 30
        if (isset($jsonData->order)) {
149 1
            $moduleFile->setModuleOrder((array) $jsonData->order);
150
        }
151
152 30
        if (isset($jsonData->plugins)) {
153 27
            $moduleFile->setPluginClasses($jsonData->plugins);
154
        }
155
156 30
        if (isset($jsonData->config)) {
157 2
            $config = $moduleFile->getConfig();
158
159 2
            foreach ($this->objectsToArrays($jsonData->config) as $key => $value) {
160 2
                $config->set($key, $value);
161
            }
162
        }
163
164 30
        if (isset($jsonData->modules)) {
165 27
            foreach ($jsonData->modules as $moduleName => $moduleData) {
166 27
                $installInfo = new InstallInfo($moduleName, $moduleData->{'install-path'});
167
168 27
                if (isset($moduleData->env)) {
169 1
                    $installInfo->setEnvironment($moduleData->env);
170
                }
171
172 27
                if (isset($moduleData->installer)) {
173 1
                    $installInfo->setInstallerName($moduleData->installer);
174
                }
175
176 27
                if (isset($moduleData->{'disabled-bindings'})) {
177 1
                    foreach ($moduleData->{'disabled-bindings'} as $uuid) {
178 1
                        $installInfo->addDisabledBindingUuid(Uuid::fromString($uuid));
179
                    }
180
                }
181
182 27
                $moduleFile->addInstallInfo($installInfo);
183
            }
184
        }
185 30
    }
186
187 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...
188
    {
189 2
        $data = (array) $data;
190
191 2
        foreach ($data as $key => $value) {
192 2
            $data[$key] = is_object($value) ? $this->objectsToArrays($value) : $value;
193
        }
194
195 2
        return $data;
196
    }
197
}
198