Passed
Push — main ( c73e00...0a00da )
by
unknown
15:13 queued 14s
created

newEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\AddressChangeContext\Tests;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DriverManager;
9
use Doctrine\DBAL\Exception;
10
use Doctrine\ORM\Configuration;
11
use Doctrine\ORM\EntityManager;
12
use Doctrine\ORM\ORMSetup;
13
use WMDE\Fundraising\AddressChangeContext\AddressChangeContextFactory;
14
15
class TestAddressChangeContextFactory {
16
17
	private Configuration $doctrineConfig;
18
	private ?EntityManager $entityManager;
19
	private AddressChangeContextFactory $contextFactory;
20
21
	/**
22
	 * @param array<string,mixed> $config
23
	 *
24
	 * @throws Exception
25
	 */
26
	public function __construct( private array $config ) {
27
		$this->contextFactory = new AddressChangeContextFactory();
28
		$this->doctrineConfig = ORMSetup::createXMLMetadataConfiguration(
29
			$this->contextFactory->getDoctrineMappingPaths(),
30
			true
31
		);
32
		$this->entityManager = null;
33
	}
34
35
	private function newConnection(): Connection {
36
		$connection = DriverManager::getConnection( $this->config['db'] );
37
		$this->contextFactory->registerCustomTypes( $connection );
38
		return $connection;
39
	}
40
41
	public function newEntityManager(): EntityManager {
42
		return new EntityManager( $this->newConnection(), $this->doctrineConfig );
43
	}
44
45
	public function getEntityManager(): EntityManager {
46
		if ( $this->entityManager === null ) {
47
			$this->entityManager = $this->newEntityManager();
48
		}
49
		return $this->entityManager;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->entityManager could return the type null which is incompatible with the type-hinted return Doctrine\ORM\EntityManager. Consider adding an additional type-check to rule them out.
Loading history...
50
	}
51
}
52