Failed Conditions
Pull Request — master (#47)
by Mateusz
24:21
created

src/Config/ConfigJsonSerializer.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Config;
15
use Puli\Manager\Api\Config\ConfigFile;
16
use Puli\Manager\Api\Config\ConfigFileSerializer;
17
use Puli\Manager\Api\InvalidConfigException;
18
use stdClass;
19
use Webmozart\Json\DecodingFailedException;
20
use Webmozart\Json\JsonDecoder;
21
use Webmozart\Json\JsonEncoder;
22
23
/**
24
 * Serializes and unserializes to/from JSON.
25
 *
26
 * @since  1.0
27
 *
28
 * @author Bernhard Schussek <[email protected]>
29
 */
30
class ConfigJsonSerializer implements ConfigFileSerializer
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function serializeConfigFile(ConfigFile $configFile)
36
    {
37 2
        $jsonData = new stdClass();
38
39 2
        foreach ($configFile->getConfig()->toRawArray(false) as $key => $value) {
40 1
            $jsonData->$key = $value;
41
        }
42
43 2
        return $this->encode($jsonData);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 51
    public function unserializeConfigFile($serialized, $path = null, Config $baseConfig = null)
50
    {
51 51
        $configFile = new ConfigFile($path, $baseConfig);
52 51
        $config = $configFile->getConfig();
53
54 51
        $jsonData = $this->objectsToArrays($this->decode($serialized, $path));
55
56 50
        foreach ($jsonData as $key => $value) {
57 2
            $config->set($key, $value);
58
        }
59
60 50
        return $configFile;
61
    }
62
63 2 View Code Duplication
    private function encode($jsonData)
0 ignored issues
show
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...
64
    {
65 2
        $encoder = new JsonEncoder();
66 2
        $encoder->setPrettyPrinting(true);
67 2
        $encoder->setEscapeSlash(false);
68 2
        $encoder->setTerminateWithLineFeed(true);
69
70 2
        return $encoder->encode($jsonData);
71
    }
72
73 51 View Code Duplication
    private function decode($json, $path = null)
0 ignored issues
show
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...
74
    {
75 51
        $decoder = new JsonDecoder();
76
77
        try {
78 51
            return $decoder->decode($json);
79 1
        } catch (DecodingFailedException $e) {
80 1
            throw new InvalidConfigException(sprintf(
81 1
                "The configuration%s could not be decoded:\n%s",
82 1
                $path ? ' in '.$path : '',
83 1
                $e->getMessage()
84 1
            ), $e->getCode(), $e);
85
        }
86
    }
87
88 50 View Code Duplication
    private function objectsToArrays($data)
89
    {
90 50
        $data = (array) $data;
91
92 50
        foreach ($data as $key => $value) {
93 2
            $data[$key] = is_object($value) ? $this->objectsToArrays($value) : $value;
94
        }
95
96 50
        return $data;
97
    }
98
}
99