Passed
Push — main ( 368bd5...fb5e62 )
by Thierry
02:28
created

UserFileReader::getOptionValues()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 16
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Config;
4
5
use Jaxon\Config\ConfigReader;
6
use Jaxon\Config\ConfigSetter;
7
8
use function array_filter;
9
use function env;
0 ignored issues
show
introduced by
The function env was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
10
use function is_array;
11
use function is_file;
12
13
class UserFileReader
14
{
15
    /**
16
     * The constructor
17
     *
18
     * @param AuthInterface $auth
19
     * @param string $configFilePath
20
     * @param bool $useEnv
21
     */
22
    public function __construct(private AuthInterface $auth,
23
        private string $configFilePath, private bool $useEnv = false)
24
    {}
25
26
    /**
27
     * Replace options with values from the .env config.
28
     *
29
     * @param array $values
30
     *
31
     * @return array
32
     */
33
    private function getOptionValues(array $values): array
34
    {
35
        if (!$this->useEnv) {
36
            return $values;
37
        }
38
39
        // The values in the server options are the names of the
40
        // corresponding options in the .env file.
41
        $values['servers'] = array_map(function(array $server) {
42
            foreach (['host', 'port', 'username', 'password'] as $option) {
43
                if (isset($server[$option])) {
44
                    $server[$option] = env($server[$option]);
0 ignored issues
show
Bug introduced by
The function env was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

44
                    $server[$option] = /** @scrutinizer ignore-call */ env($server[$option]);
Loading history...
45
                }
46
            }
47
        }, $values['servers'] ?? []);
48
        return $values;
49
    }
50
51
    /**
52
     * Get the options for the authenticated user.
53
     *
54
     * @param array $defaultOptions
55
     *
56
     * @return array
57
     */
58
    public function getOptions(array $defaultOptions): array
59
    {
60
        $setter = new ConfigSetter();
61
        $reader = new ConfigReader($setter);
62
        $userConfig = $setter->newConfig($defaultOptions);
63
64
        if (!is_file($this->configFilePath)) {
65
            return $userConfig->getValues();
66
        }
67
68
        $config = $reader->load($setter->newConfig(), $this->configFilePath);
69
        $commonOptions = $config->getOption('common', null);
70
        if (is_array($commonOptions)) {
71
            $userConfig = $setter->setOptions($config, $commonOptions);
72
        }
73
74
        $fallbackOptions = $config->getOption('fallback', null);
75
        $userOptions = array_filter($config->getOption('users', []),
76
            fn($options) => $options['id'] ?? null === $this->auth->user());
77
        $userOptions = $userOptions[0] ?? $fallbackOptions;
78
79
        if (!is_array($userOptions)) {
80
            return $this->getOptionValues($userConfig->getValues());
81
        }
82
83
        unset($userOptions['id']); // Remove the id field.
84
        $userConfig = $setter->setOptions($userConfig, $userOptions);
85
86
        return $this->getOptionValues($userConfig->getValues());
87
    }
88
}
89