Completed
Push — 15.x ( 013daf...481d54 )
by Tim
01:32
created

JsonParser::parse()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Configuration\Jms\Parsers\JsonParser
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-configuration-jms
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Configuration\Jms\Parsers;
22
23
use TechDivision\Import\Adapter\PhpFilesystemAdapterInterface;
24
use TechDivision\Import\Configuration\Jms\Utils\ArrayUtilInterface;
25
use TechDivision\Import\Configuration\Jms\ConfigurationParserInterface;
26
27
/**
28
 * The JSON configuration parser implementation.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2019 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-configuration-jms
34
 * @link      http://www.techdivision.com
35
 */
36
class JsonParser implements ConfigurationParserInterface
37
{
38
39
    /**
40
     * The utility class that provides array handling functionality.
41
     *
42
     * @var \TechDivision\Import\Configuration\Jms\Utils\ArrayUtilInterface
43
     */
44
    protected $arrayUtil;
45
46
    /**
47
     * Initializes the parser with the array utility instance.
48
     *
49
     * @param \TechDivision\Import\Configuration\Jms\Utils\ArrayUtilInterface $arrayUtil The utility instance
50
     */
51
    public function __construct(ArrayUtilInterface $arrayUtil)
52
    {
53
        $this->arrayUtil = $arrayUtil;
54
    }
55
56
    /**
57
     * Parsing the configuration and merge it recursively.
58
     *
59
     * @param array $directories An array with diretories to parse
60
     *
61
     * @return void
62
     * @throws \Exception Is thrown if the configuration can not be loaded from the configuration files
63
     */
64
    public function parse(array $directories)
65
    {
66
67
        // initialize the array that'll contain the configuration structure
68
        $main = array();
69
70
        // iterate over the found directories to parse them for configuration files
71
        foreach ($directories as $directory) {
72
            // load the configuration filenames
73
            $filenames = $this->listContents($directory, '*.json');
74
75
            // load the content of each found configuration file and merge it
76
            foreach ($filenames as $filename) {
77
                if (is_file($filename) && $content = json_decode(file_get_contents($filename), true)) {
78
                    $main = $this->replace($main, $content);
79
                } else {
80
                    throw new \Exception(sprintf('Can\'t load content of file %s', $filename));
81
                }
82
            }
83
        }
84
85
        // return the JSON encoded configuration
86
        return json_encode($main, JSON_PRETTY_PRINT);
87
    }
88
89
    /**
90
     * Return's the array utility instance.
91
     *
92
     * @return \TechDivision\Import\Configuration\Jms\Utils\ArrayUtilInterface The utility instance
93
     */
94
    protected function getArrayUtil()
95
    {
96
        return $this->arrayUtil;
97
    }
98
99
    /**
100
     * Replaces the values of the first array with the ones from the arrays
101
     * that has been passed as additional arguments.
102
     *
103
     * @param array ...$arrays The arrays with the values that has to be replaced
104
     *
105
     * @return array The array with the replaced values
106
     */
107
    protected function replace(...$arrays)
108
    {
109
        return $this->getArrayUtil()->replace(...$arrays);
110
    }
111
112
    /**
113
     * List the filenames of a directory.
114
     *
115
     * @param string  $directory The directory to list
116
     * @param boolean $recursive Whether to list recursively
0 ignored issues
show
Bug introduced by
There is no parameter named $recursive. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
117
     *
118
     * @return array A list of filenames
119
     */
120
    protected function listContents($directory = '', $suffix = '*')
121
    {
122
123
        // parse the directory
124
        $files = glob($pattern = sprintf('%s/%s', $directory, $suffix), 0);
125
126
        // parse the subdirectories also
127
        $dirs = glob($directory. DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR|GLOB_NOSORT|GLOB_BRACE);
128
129
        // iterate over the subdirectories for its files
130
        foreach ($dirs as $dir) {
131
            $files = array_merge($files, $this->listContents($dir, $suffix));
132
        }
133
134
        // return the array with the files matching the glob pattern
135
        return $files;
136
    }
137
}
138