Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
DatabaseFactory
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 __invoke
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
2.00
1<?php
2
3declare(strict_types=1);
4
5namespace App\Factory;
6
7use App\Support\Row;
8use PDO;
9use Psr\Container\ContainerInterface;
10use RuntimeException;
11
12use function sprintf;
13
14final class DatabaseFactory
15{
16    public function __invoke(ContainerInterface $container): PDO
17    {
18        $allSettings = $container->get('settings');
19        if (!is_array($allSettings)) {
20            throw new RuntimeException('Container "settings" must be an array');
21        }
22
23        $db = Row::array($allSettings, 'db');
24
25        $dsn = sprintf(
26            '%s:host=%s;port=%s;dbname=%s',
27            Row::string($db, 'driver'),
28            Row::string($db, 'host'),
29            Row::string($db, 'port'),
30            Row::string($db, 'database'),
31        );
32
33        return new PDO(
34            $dsn,
35            Row::string($db, 'username'),
36            Row::string($db, 'password'),
37            Row::array($db, 'options'),
38        );
39    }
40}