Completed
Pull Request — master (#9)
by Sander
04:49
created

Reader::getEntityConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * neuralyzer : Data Anonymization Library and CLI Tool
4
 *
5
 * PHP Version 7.1
6
 *
7
 * @author    Emmanuel Dyan
8
 * @author    Rémi Sauvat
9
 * @copyright 2018 Emmanuel Dyan
10
 *
11
 * @package   edyan/neuralyzer
12
 *
13
 * @license   GNU General Public License v2.0
14
 *
15
 * @link      https://github.com/edyan/neuralyzer
16
 */
17
18
namespace Edyan\Neuralyzer\Configuration;
19
20
use Symfony\Component\Config\Definition\Processor;
21
use Symfony\Component\Config\FileLocator;
22
use Symfony\Component\Yaml\Yaml;
23
24
/**
25
 * Configuration Reader
26
 */
27
class Reader
28
{
29
    /**
30
     * Configuration file name
31
     *
32
     * @var string
33
     */
34
    protected $configFileName;
35
36
    /**
37
     * Define the directories to search in. Only the first file found is taken into account
38
     * Can be defined via the constructor
39
     *
40
     * @var array
41
     */
42
    protected $configDirectories;
43
44
    /**
45
     * Configuration file name
46
     *
47
     * @var string|array
48
     */
49
    protected $configFilePath;
50
51
    /**
52
     * Stores the config values
53
     *
54
     * @var array
55
     */
56
    protected $configValues = [];
57
58
59
    /**
60
     * Set a few properties, open the config file and parse it
61
     *
62
     * @param string $configFileName
63
     * @param array  $configDirectories
64
     */
65 1
    public function __construct(string $configFileName, array $configDirectories = ['.'])
66
    {
67 1
        $this->configFileName = $configFileName;
68 1
        $this->configDirectories = $configDirectories;
69
70 1
        $locator = new FileLocator($this->configDirectories);
71 1
        $this->configFilePath = $locator->locate($this->configFileName);
72
73 1
        $this->parseAndValidateConfig();
74 1
    }
75
76
77
    /**
78
     * Getter
79
     *
80
     * @return array Config Values
81
     */
82 1
    public function getConfigValues(): array
83
    {
84 1
        return $this->configValues;
85
    }
86
87
88
    /**
89
     * Get config values for an entity
90
     *
91
     * @param  string $entity
92
     *
93
     * @throws \InvalidArgumentException
94
     * @return array            Config Values
95
     */
96
    public function getEntityConfig(string $entity): array
97
    {
98
        if (!array_key_exists($entity, $this->configValues['entities'])) {
99
            throw new \InvalidArgumentException("$entity is not set in config");
100
        }
101
102
        return $this->configValues['entities'][$entity];
103
    }
104
105
    /**
106
     * Return the list of pre actions
107
     *
108
     * @return array
109
     */
110
    public function getPreActions(): array
111
    {
112
        return $this->configValues['pre_actions'];
113
    }
114
115
    /**
116
     * Return the list of entites
117
     *
118
     * @return array
119
     */
120
    public function getEntities(): array
121
    {
122
        return array_keys($this->configValues['entities']);
123
    }
124
125
    /**
126
     * Return the list of post actions
127
     *
128
     * @return array
129
     */
130
    public function getPostActions(): array
131
    {
132
        return $this->configValues['post_actions'];
133
    }
134
135
    /**
136
     * Parse and validate the configuration
137
     */
138 1
    protected function parseAndValidateConfig(): void
139
    {
140 1
        $config = Yaml::parse(file_get_contents($this->configFilePath));
0 ignored issues
show
Bug introduced by
It seems like $this->configFilePath can also be of type array; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
        $config = Yaml::parse(file_get_contents(/** @scrutinizer ignore-type */ $this->configFilePath));
Loading history...
141
142 1
        $processor = new Processor();
143 1
        $configDefinition = new ConfigDefinition();
144 1
        $this->configValues = $processor->processConfiguration($configDefinition, [$config]);
145 1
    }
146
}
147