1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * neuralyzer : Data Anonymization Library and CLI Tool |
||
7 | * |
||
8 | * PHP Version 7.2 |
||
9 | * |
||
10 | * @author Emmanuel Dyan |
||
11 | * @author Rémi Sauvat |
||
12 | * |
||
13 | * @copyright 2020 Emmanuel Dyan |
||
14 | * |
||
15 | * @package edyan/neuralyzer |
||
16 | * |
||
17 | * @license GNU General Public License v2.0 |
||
18 | * |
||
19 | * @link https://github.com/edyan/neuralyzer |
||
20 | */ |
||
21 | |||
22 | namespace Edyan\Neuralyzer\Configuration; |
||
23 | |||
24 | use Symfony\Component\Config\Definition\Processor; |
||
25 | use Symfony\Component\Config\FileLocator; |
||
26 | use Symfony\Component\Yaml\Yaml; |
||
27 | |||
28 | /** |
||
29 | * Configuration Reader |
||
30 | */ |
||
31 | class Reader |
||
32 | { |
||
33 | /** |
||
34 | * Stores the config values |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $configValues = []; |
||
39 | |||
40 | /** |
||
41 | * Stores the depreciation messages |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $depreciationMessages = []; |
||
46 | |||
47 | /** |
||
48 | * Set a few properties, open the config file and parse it |
||
49 | * |
||
50 | 44 | * @param array $configDirectories |
|
51 | */ |
||
52 | 44 | public function __construct(string $configFileName, array $configDirectories = ['.']) |
|
53 | 44 | { |
|
54 | $config = Yaml::parse(file_get_contents( |
||
55 | 42 | (new FileLocator($configDirectories))->locate($configFileName) |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
56 | 40 | )); |
|
57 | 40 | $this->parseAndValidateConfig($config); |
|
58 | $this->registerDepreciationMessages($config); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Getter |
||
63 | * |
||
64 | * @return array Config Values |
||
65 | 37 | */ |
|
66 | public function getConfigValues(): array |
||
67 | 37 | { |
|
68 | return $this->configValues; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get config values for an entity |
||
73 | * |
||
74 | * @throws \InvalidArgumentException |
||
75 | * |
||
76 | * @return array Config Values |
||
77 | */ |
||
78 | public function getEntityConfig(string $entity): array |
||
79 | 16 | { |
|
80 | if (! array_key_exists($entity, $this->configValues['entities'])) { |
||
81 | 16 | throw new \InvalidArgumentException("${entity} is not set in config"); |
|
82 | 1 | } |
|
83 | |||
84 | return $this->configValues['entities'][$entity]; |
||
85 | 15 | } |
|
86 | |||
87 | /** |
||
88 | * Return the list of entities |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getEntities(): array |
||
93 | 24 | { |
|
94 | return array_keys($this->configValues['entities']); |
||
95 | 24 | } |
|
96 | |||
97 | /** |
||
98 | * Return the list of post actions |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | public function getPostActions(): array |
||
103 | 17 | { |
|
104 | return $this->configValues['post_actions']; |
||
105 | 17 | } |
|
106 | |||
107 | /** |
||
108 | * Get a list of depreciation messages |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getDepreciationMessages(): array |
||
113 | 19 | { |
|
114 | return $this->depreciationMessages; |
||
115 | 19 | } |
|
116 | |||
117 | /** |
||
118 | * @param array|null $config |
||
119 | */ |
||
120 | protected function parseAndValidateConfig(?array $config): void |
||
121 | { |
||
122 | $configDefinition = new ConfigDefinition(); |
||
123 | 17 | $this->configValues = (new Processor())->processConfiguration($configDefinition, [$config]); |
|
124 | } |
||
125 | 17 | ||
126 | /** |
||
127 | * @param array $config |
||
128 | */ |
||
129 | private function registerDepreciationMessages(array $config): void |
||
130 | { |
||
131 | 42 | foreach ($config['entities'] as $entity) { |
|
132 | if (empty($entity)) { |
||
133 | 42 | return; |
|
134 | 42 | } |
|
135 | 40 | ||
136 | if (array_key_exists('delete', $entity) || array_key_exists('delete_where', $entity)) { |
||
137 | $this->depreciationMessages[] = '"delete" and "delete_where" have been deprecated in favor of pre and post_actions'; |
||
138 | break; |
||
139 | } |
||
140 | 40 | } |
|
141 | } |
||
142 | } |
||
143 |