Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DatabaseParser
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 parse
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
3
 checkConfig
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Config Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Config\Parsers;
11
12use Framework\Database\Database;
13use SensitiveParameter;
14
15/**
16 * Class DatabaseParser.
17 *
18 * @package config
19 */
20class DatabaseParser extends Parser
21{
22    /**
23     * Get config from a database.
24     *
25     * @param mixed $config array with configs for database connection:
26     * host, port, username, password, schema and table
27     *
28     * @throws ParserException
29     *
30     * @return array<mixed> The database parsed data
31     */
32    public static function parse(#[SensitiveParameter] mixed $config) : array
33    {
34        static::checkConfig($config);
35        return static::parseOrThrow(static function () use ($config) : array {
36            try {
37                $database = new Database($config);
38                $results = $database->select()
39                    ->from($config['table'])
40                    ->orderBy('key')
41                    ->run()
42                    ->fetchArrayAll();
43            } catch (\Exception $exception) {
44                throw new ParserException(
45                    static::class . ': ' . $exception->getMessage(),
46                    $exception->getCode(),
47                    \E_ERROR,
48                    $exception->getFile(),
49                    $exception->getLine()
50                );
51            }
52            $data = [];
53            foreach ($results as $row) {
54                $key = \explode('.', $row['key']);
55                $value = static::getValue($row['value']);
56                $parent = [];
57                static::addChild($parent, $key, $value);
58                $data = \array_replace_recursive($data, $parent);
59            }
60            return $data;
61        });
62    }
63
64    protected static function checkConfig(#[SensitiveParameter] mixed $config) : void
65    {
66        if ( ! \is_array($config)) {
67            throw new ParserException(static::class . ' config must be an array');
68        }
69        if ( ! isset($config['username'])) {
70            throw new ParserException(static::class . ' config username not set');
71        }
72        if ( ! isset($config['schema'])) {
73            throw new ParserException(static::class . ' config schema not set');
74        }
75        if ( ! isset($config['table'])) {
76            throw new ParserException(static::class . ' config table not set');
77        }
78    }
79}