Failed Conditions
Push — master ( ebb1ea...d49c03 )
by Bernhard
06:05
created

ConfigFileConverter::objectsToArrays()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3
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\Config;
13
14
use Puli\Manager\Api\Config\ConfigFile;
15
use Puli\Manager\Assert\Assert;
16
use Webmozart\Json\Conversion\JsonConverter;
17
18
/**
19
 * Converts config files to JSON and back.
20
 *
21
 * @since  1.0
22
 *
23
 * @author Bernhard Schussek <[email protected]>
24
 */
25
class ConfigFileConverter implements JsonConverter
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30 2
    public function toJson($configFile, array $options = array())
31
    {
32 2
        Assert::isInstanceOf($configFile, 'Puli\Manager\Api\Config\ConfigFile');
33
34 2
        $jsonData = array();
35
36 2
        foreach ($configFile->getConfig()->toRawArray(false) as $key => $value) {
37 1
            $jsonData[$key] = $value;
38
        }
39
40
        // The topmost array is always an object, even if empty
41 2
        return (object) $this->arraysToObjects($jsonData);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 50
    public function fromJson($jsonData, array $options = array())
48
    {
49 50
        $path = isset($options['path']) ? $options['path'] : null;
50 50
        $baseConfig = isset($options['baseConfig']) ? $options['baseConfig'] : null;
51
52 50
        Assert::isInstanceOf($jsonData, 'stdClass');
53 50
        Assert::nullOrString($path, 'The "path" option should be null or a string. Got: %s');
54 50
        Assert::nullOrIsInstanceOf($baseConfig, 'Puli\Manager\Api\Config\Config', 'The "baseConfig" option should be null or an instance of %2$s. Got: %s');
55
56 50
        $configFile = new ConfigFile($path, $baseConfig);
57 50
        $config = $configFile->getConfig();
58
59 50
        $jsonData = $this->objectsToArrays($jsonData);
60
61 50
        foreach ($jsonData as $key => $value) {
62 1
            $config->set($key, $value);
63
        }
64
65 50
        return $configFile;
66
    }
67
68 50 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...
69
    {
70 50
        $data = (array) $data;
71
72 50
        foreach ($data as $key => $value) {
73 1
            $data[$key] = is_object($value) ? $this->objectsToArrays($value) : $value;
74
        }
75
76 50
        return $data;
77
    }
78
79 2
    private function arraysToObjects(array $data)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
80
    {
81 2
        $intKeys = true;
82
83 2
        foreach ($data as $key => $value) {
84 1
            $intKeys = $intKeys && is_int($key);
85 1
            $data[$key] = is_array($value) ? $this->arraysToObjects($value) : $value;
86
        }
87
88 2
        return $intKeys ? $data : (object) $data;
89
    }
90
}
91