Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
8 / 9 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| MigrationFactory | |
88.89% |
8 / 9 |
|
50.00% |
1 / 2 |
3.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray; |
| 6 | |
| 7 | use Celemas\Container\Container; |
| 8 | use Celemas\Quma\Connection; |
| 9 | use Celemas\Quma\Contract\Migration; |
| 10 | use Celemas\Quma\Contract\MigrationFactory as QumaMigrationFactory; |
| 11 | use Celemas\Quma\Database; |
| 12 | use Celemas\Quma\Environment; |
| 13 | use Override; |
| 14 | use UnexpectedValueException; |
| 15 | |
| 16 | final 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 | } |