Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
8 / 9
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MigrationFactory
88.89% covered (warning)
88.89%
8 / 9
50.00% covered (danger)
50.00%
1 / 2
3.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
2.01
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray;
6
7use Celemas\Container\Container;
8use Celemas\Quma\Connection;
9use Celemas\Quma\Contract\Migration;
10use Celemas\Quma\Contract\MigrationFactory as QumaMigrationFactory;
11use Celemas\Quma\Database;
12use Celemas\Quma\Environment;
13use Override;
14use UnexpectedValueException;
15
16final class MigrationFactory implements QumaMigrationFactory
17{
18    public function __construct(
19        protected readonly Container $container,
20    ) {}
21
22    /** @param class-string<Migration> $class */
23    #[Override]
24    public function create(string $class, Environment $env): Migration
25    {
26        $container = $this->container->scope();
27        $container->add(Environment::class, $env)->value();
28        $container->add(Connection::class, $env->conn)->value();
29        $container->add(Database::class, $env->db)->value();
30
31        $migration = $container->get($class);
32
33        if (!$migration instanceof Migration) {
34            throw new UnexpectedValueException("Migration factory must create instances of {$class}");
35        }
36
37        return $migration;
38    }
39}