Completed
Push — master ( a59c49...1dd4d3 )
by personal
26s queued 20s
created

ConfigFileReaderJson::collapseArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Hal\Application\Config\File;
4
5
use Hal\Application\Config\Config;
6
use RecursiveArrayIterator;
7
use RecursiveIteratorIterator;
8
9
class ConfigFileReaderJson implements ConfigFileReaderInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $filename;
15
16
    /**
17
     * @param string $filename
18
     */
19
    public function __construct($filename)
20
    {
21
        $this->filename = $filename;
22
    }
23
24
    /**
25
     * @param Config $config
26
     *
27
     * @return void
28
     */
29
    public function read(Config $config)
30
    {
31
        $jsonText = file_get_contents($this->filename);
32
33
        if (false === $jsonText) {
34
            throw new \InvalidArgumentException("Cannot read configuration file '{$this->filename}'");
35
        }
36
37
        $jsonData = json_decode($jsonText, true);
38
39
        if (false === $jsonData || null === $jsonData) {
40
            throw new \InvalidArgumentException("Bad json file '{$this->filename}'");
41
        }
42
43
        if (isset($jsonData['includes'])) {
44
            $includes = $jsonData['includes'];
45
            $files = [];
46
            // with config file, includes are relative to config file
47
            foreach ($includes as $include) {
48
                $include = $this->resolvePath($include);
49
                $files[] = $include;
50
            }
51
            $config->set('files', $files);
52
        }
53
54
        if (isset($jsonData['groups'])) {
55
            $config->set('groups', $jsonData['groups']);
56
        }
57
58
        if (isset($jsonData['extensions'])) {
59
            $config->set('extensions', implode(',', $jsonData['extensions']));
60
        }
61
62
        if (isset($jsonData['excludes'])) {
63
            // retro-compatibility
64
            // "exclude" is a string
65
            // excludes is an array
66
            $config->set('exclude', implode(',', $jsonData['excludes']));
67
        } else if (isset($jsonData['exclude'])) {
68
            $config->set('exclude', $jsonData['exclude']);
69
        }
70
71 View Code Duplication
        if (isset($jsonData['plugins'], $jsonData['plugins']['git'], $jsonData['plugins']['git']['binary'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
72
            $config->set('git', $jsonData['plugins']['git']['binary']);
73
        }
74
75 View Code Duplication
        if (isset($jsonData['plugins'], $jsonData['plugins']['junit'], $jsonData['plugins']['junit']["report"])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
76
            $config->set('junit', $jsonData['plugins']['junit']["report"]);
77
        }
78
79
        // reports
80
        if (isset($jsonData['report']) && is_array($jsonData['report'])) {
81
            foreach ($jsonData['report'] as $reportType => $path) {
82
                $path = $this->resolvePath($path);
83
                $config->set('report-' . $reportType, $path);
84
            }
85
        }
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    private function resolvePath($path)
92
    {
93
        if (DIRECTORY_SEPARATOR !== $path[0]) {
94
             $path = dirname($this->filename) . DIRECTORY_SEPARATOR . $path;
95
        }
96
97
        return $path;
98
    }
99
}
100