Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| DatabaseFactory | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
| __invoke | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
2.00 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Factory; |
| 6 | |
| 7 | use App\Support\Row; |
| 8 | use PDO; |
| 9 | use Psr\Container\ContainerInterface; |
| 10 | use RuntimeException; |
| 11 | |
| 12 | use function sprintf; |
| 13 | |
| 14 | final 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 | $pdo = new PDO( |
| 34 | $dsn, |
| 35 | Row::string($db, 'username'), |
| 36 | Row::string($db, 'password'), |
| 37 | Row::array($db, 'options'), |
| 38 | ); |
| 39 | |
| 40 | // FSC-96: investment-side tables live in `treasury`, loan-side in `lending`. |
| 41 | // Setting the connection search_path lets every repository keep its |
| 42 | // unqualified SQL (FROM accounts, FROM loans) — Postgres resolves names |
| 43 | // left-to-right across the path. |
| 44 | $pdo->exec('SET search_path = treasury, lending, public'); |
| 45 | |
| 46 | return $pdo; |
| 47 | } |
| 48 | } |