• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / config / 13066327659

29 Jan 2025 11:27PM UTC coverage: 100.0%. Remained the same
13066327659

push

github

natanfelles
Add debug icon

311 of 311 relevant lines covered (100.0%)

3.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Parsers/DatabaseParser.php
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
 */
10
namespace Framework\Config\Parsers;
11

12
use Framework\Database\Database;
13
use SensitiveParameter;
14

15
/**
16
 * Class DatabaseParser.
17
 *
18
 * @package config
19
 */
20
class 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);
6✔
35
        return static::parseOrThrow(static function () use ($config) : array {
2✔
36
            try {
37
                $database = new Database($config);
2✔
38
                $results = $database->select()
1✔
39
                    ->from($config['table'])
1✔
40
                    ->orderBy('key')
1✔
41
                    ->run()
1✔
42
                    ->fetchArrayAll();
1✔
43
            } catch (\Exception $exception) {
1✔
44
                throw new ParserException(
1✔
45
                    static::class . ': ' . $exception->getMessage(),
1✔
46
                    $exception->getCode(),
1✔
47
                    \E_ERROR,
1✔
48
                    $exception->getFile(),
1✔
49
                    $exception->getLine()
1✔
50
                );
1✔
51
            }
52
            $data = [];
1✔
53
            foreach ($results as $row) {
1✔
54
                $key = \explode('.', $row['key']);
1✔
55
                $value = static::getValue($row['value']);
1✔
56
                $parent = [];
1✔
57
                static::addChild($parent, $key, $value);
1✔
58
                $data = \array_replace_recursive($data, $parent);
1✔
59
            }
60
            return $data;
1✔
61
        });
2✔
62
    }
63

64
    protected static function checkConfig(#[SensitiveParameter] mixed $config) : void
65
    {
66
        if (!\is_array($config)) {
6✔
67
            throw new ParserException(static::class . ' config must be an array');
1✔
68
        }
69
        if (!isset($config['username'])) {
5✔
70
            throw new ParserException(static::class . ' config username not set');
1✔
71
        }
72
        if (!isset($config['schema'])) {
4✔
73
            throw new ParserException(static::class . ' config schema not set');
1✔
74
        }
75
        if (!isset($config['table'])) {
3✔
76
            throw new ParserException(static::class . ' config table not set');
1✔
77
        }
78
    }
79
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc