Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
9 / 10 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RunMigrationsOnConnect | |
90.00% |
9 / 10 |
|
50.00% |
1 / 2 |
3.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| wrap | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\DoctrineEntityDatalayer\Middleware; |
| 4 | |
| 5 | use Doctrine\DBAL\Driver; |
| 6 | use Doctrine\DBAL\Driver\API\ExceptionConverter; |
| 7 | use Doctrine\DBAL\Driver\Connection; |
| 8 | use Doctrine\DBAL\Driver\Middleware; |
| 9 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 10 | use Doctrine\DBAL\ServerVersionProvider; |
| 11 | |
| 12 | class RunMigrationsOnConnect implements Middleware |
| 13 | { |
| 14 | public function __construct(private readonly \Closure $action) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | public function wrap(Driver $driver): Driver |
| 19 | { |
| 20 | return new class($driver, $this->action) implements Driver { |
| 21 | private ?Connection $connection = null; |
| 22 | |
| 23 | public function __construct(private readonly Driver $driver, private readonly \Closure $action) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | public function connect(array $params): Connection |
| 28 | { |
| 29 | if ($this->connection === null) { |
| 30 | $this->connection = $this->driver->connect($params); |
| 31 | call_user_func($this->action); |
| 32 | } |
| 33 | |
| 34 | return $this->connection; |
| 35 | } |
| 36 | |
| 37 | public function getDatabasePlatform(ServerVersionProvider $versionProvider): AbstractPlatform |
| 38 | { |
| 39 | return $this->driver->getDatabasePlatform($versionProvider); |
| 40 | } |
| 41 | |
| 42 | public function getExceptionConverter(): ExceptionConverter |
| 43 | { |
| 44 | return $this->driver->getExceptionConverter(); |
| 45 | } |
| 46 | }; |
| 47 | } |
| 48 | } |