Passed
Push — master ( abe2d8...3320d8 )
by Emmanuel
02:30 queued 11s
created

Reader   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 113
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigValues() 0 3 1
A getEntityConfig() 0 7 2
A __construct() 0 7 1
A registerDepreciationMessages() 0 10 5
A getPostActions() 0 3 1
A parseAndValidateConfig() 0 4 1
A getDepreciationMessages() 0 3 1
A getEntities() 0 3 1
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 44
    public function __construct(string $configFileName, array $configDirectories = ['.'])
51
    {
52 44
        $config = Yaml::parse(file_get_contents(
53 44
            (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 42
        $this->parseAndValidateConfig($config);
56 40
        $this->registerDepreciationMessages($config);
57 40
    }
58
59
60
    /**
61
     * Getter
62
     *
63
     * @return array Config Values
64
     */
65 37
    public function getConfigValues(): array
66
    {
67 37
        return $this->configValues;
68
    }
69
70
71
    /**
72
     * Get config values for an entity
73
     *
74
     * @param  string $entity
75
     *
76
     * @throws \InvalidArgumentException
77
     * @return array            Config Values
78
     */
79 16
    public function getEntityConfig(string $entity): array
80
    {
81 16
        if (!array_key_exists($entity, $this->configValues['entities'])) {
82 1
            throw new \InvalidArgumentException("$entity is not set in config");
83
        }
84
85 15
        return $this->configValues['entities'][$entity];
86
    }
87
88
    /**
89
     * Return the list of entities
90
     *
91
     * @return array
92
     */
93 24
    public function getEntities(): array
94
    {
95 24
        return array_keys($this->configValues['entities']);
96
    }
97
98
    /**
99
     * Return the list of post actions
100
     *
101
     * @return array
102
     */
103 17
    public function getPostActions(): array
104
    {
105 17
        return $this->configValues['post_actions'];
106
    }
107
108
    /**
109
     * Get a list of depreciation messages
110
     *
111
     * @return array
112
     */
113 19
    public function getDepreciationMessages(): array
114
    {
115 19
        return $this->depreciationMessages;
116
    }
117
118
    /**
119
     * @param array|null $config
120
     */
121
    protected function parseAndValidateConfig(?array $config): void
122
    {
123 17
        $configDefinition = new ConfigDefinition();
124
        $this->configValues = (new Processor)->processConfiguration($configDefinition, [$config]);
125 17
    }
126
127
    /**
128
     * @param array $config
129
     */
130
    private function registerDepreciationMessages(array $config): void
131 42
    {
132
        foreach ($config['entities'] as $entity) {
133 42
            if (empty($entity)) {
134 42
                return;
135 40
            }
136
137
            if (array_key_exists('delete', $entity) || array_key_exists('delete_where', $entity)) {
138
                $this->depreciationMessages[] = '"delete" and "delete_where" have been deprecated in favor of pre and post_actions';
139
                break;
140 40
            }
141
        }
142 40
    }
143
}
144