-
Notifications
You must be signed in to change notification settings - Fork 3
/
EntityManagerBuilder.php
103 lines (80 loc) · 2.7 KB
/
EntityManagerBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
namespace Tests\Tool;
use DDDStarterPack\Util\EnvVarUtil;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\ORMSetup;
use Doctrine\Persistence\ObjectRepository;
use InvalidArgumentException;
use Nyholm\Dsn\DsnParser;
use Tests\Support\Model\Doctrine\DoctrineUuidPersonId;
class EntityManagerBuilder
{
/** @var list<string> */
private array $paths;
private bool $isDevMode;
private Configuration $config;
/** @var array{dbname: string, driver: string, host: string, password: string, port: int, user: string} */
private array $connectionParams;
/** @var EntityManager[] */
private array $ems = [];
private function __construct(bool $isDevMode)
{
$this->isDevMode = $isDevMode;
$this->createPaths();
$this->prepareConnectionParams();
$this->config = ORMSetup::createXMLMetadataConfiguration($this->paths, $this->isDevMode);
}
private function createPaths(): void
{
$this->paths = [
Path::test().'/Support/DbSchema',
];
}
private function prepareConnectionParams(): void
{
$dsn = DsnParser::parse(EnvVarUtil::get('DATABASE_URL'));
$dbParamsAuth = [
'driver' => 'pdo_mysql',
'user' => (string) $dsn->getUser(),
'password' => (string) $dsn->getPassword(),
'dbname' => ltrim((string) $dsn->getPath(), '/'),
'host' => (string) $dsn->getHost(),
'port' => (int) $dsn->getPort(),
];
$this->connectionParams = $dbParamsAuth;
}
public static function create(bool $isDevMode = false): self
{
$builder = new self($isDevMode);
if (!Type::hasType('PersonId')) {
Type::addType('PersonId', DoctrineUuidPersonId::class);
}
$builder->ems['default'] = EntityManager::create($builder->connectionParams, $builder->config);
return $builder;
}
public function getEntityManager(string $em = 'default'): EntityManager
{
if (!array_key_exists($em, $this->ems)) {
throw new InvalidArgumentException('Invalid key for entity manager');
}
return $this->ems[$em];
}
/**
* @param class-string $entity
* @param string $entityManager
*
* @return EntityRepository|ObjectRepository
*/
public function getRepository(string $entity, string $entityManager = 'default'): EntityRepository|ObjectRepository
{
return $this->ems[$entityManager]->getRepository($entity);
}
public function getConfig(): Configuration
{
return $this->config;
}
}