Completed
Push — master ( a1f222...7ceccc )
by Emmanuel
03:50
created

Reader::parseAndValidateConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
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
     * Stores the config values
31
     *
32
     * @var array
33
     */
34
    protected $configValues = [];
35
36
    /**
37
     * Stores the depreciation messages
38
     *
39
     * @var array
40
     */
41
    private $depreciationMessages = [];
42
43
44
    /**
45
     * Set a few properties, open the config file and parse it
46
     *
47
     * @param string $configFileName
48
     * @param array  $configDirectories
49
     */
50
    public function __construct(string $configFileName, array $configDirectories = ['.'])
51
    {
52
        $config = Yaml::parse(file_get_contents(
53
            (new FileLocator($configDirectories))->locate($configFileName)
0 ignored issues
show
Bug introduced by
It seems like new Symfony\Component\Co...locate($configFileName) can also be of type string[]; 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

53
            /** @scrutinizer ignore-type */ (new FileLocator($configDirectories))->locate($configFileName)
Loading history...
54
        ));
55
        $this->parseAndValidateConfig($config);
56
        $this->registerDepreciationMessages($config);
57
    }
58
59
60
    /**
61
     * Getter
62
     *
63
     * @return array Config Values
64
     */
65 45
    public function getConfigValues(): array
66
    {
67 45
        return $this->configValues;
68 45
    }
69
70 45
71 45
    /**
72
     * Get config values for an entity
73 43
     *
74 41
     * @param  string $entity
75
     *
76
     * @throws \InvalidArgumentException
77
     * @return array            Config Values
78
     */
79
    public function getEntityConfig(string $entity): array
80
    {
81
        if (!array_key_exists($entity, $this->configValues['entities'])) {
82 38
            throw new \InvalidArgumentException("$entity is not set in config");
83
        }
84 38
85
        return $this->configValues['entities'][$entity];
86
    }
87
88
    /**
89
     * Return the list of pre actions
90
     *
91
     * @return array
92
     */
93
    public function getPreActions(): array
94
    {
95 16
        return $this->configValues['pre_actions'];
96
    }
97 16
98 1
    /**
99
     * Return the list of entites
100 15
     *
101
     * @return array
102
     */
103
    public function getEntities(): array
104
    {
105
        return array_keys($this->configValues['entities']);
106
    }
107
108
    /**
109 17
     * Return the list of post actions
110
     *
111 17
     * @return array
112
     */
113
    public function getPostActions(): array
114
    {
115
        return $this->configValues['post_actions'];
116
    }
117
118 43
    /**
119
     * Get a list of depreciation messages
120 43
     *
121
     * @return array
122 43
     */
123 43
    public function getDepreciationMessages(): array
124 43
    {
125 41
        return $this->depreciationMessages;
126
    }
127
128
    /**
129
     * @param array|null $config
130
     */
131
    protected function parseAndValidateConfig(?array $config): void
132
    {
133
        $configDefinition = new ConfigDefinition();
134
        $this->configValues = (new Processor)->processConfiguration($configDefinition, [$config]);
135
    }
136
137
    /**
138
     * @param array $config
139
     */
140
    private function registerDepreciationMessages(array $config): void
141
    {
142
        foreach ($config['entities'] as $entity) {
143
            if (empty($entity)) {
144
                return;
145
            }
146
147
            if (array_key_exists('delete', $entity) || array_key_exists('delete_where', $entity)) {
148
                $this->depreciationMessages[] = '"delete" and "delete_where" have been deprecated in favor of pre and post_actions';
149
                break;
150
            }
151
        }
152
    }
153
}
154