Installer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A install() 0 3 1
A uninstall() 0 3 1
A getSchemaTool() 0 3 1
A getClassMetaData() 0 3 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Store;
6
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Tools\SchemaTool;
9
10
/**
11
 * @since 0.1
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 * @author Jonas Kress
16
 */
17
class Installer {
18
19
	private $entityManager;
20
21 6
	public function __construct( EntityManager $entityManager ) {
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
22 6
		$this->entityManager = $entityManager;
23 6
	}
24
25 6
	public function install() {
26 6
		$this->getSchemaTool()->createSchema( $this->getClassMetaData() );
27 6
	}
28
29 6
	public function uninstall() {
30 6
		$this->getSchemaTool()->dropSchema( $this->getClassMetaData() );
31 6
	}
32
33 6
	private function getSchemaTool() {
34 6
		return new SchemaTool( $this->entityManager );
35
	}
36
37 6
	private function getClassMetaData() {
38 6
		return $this->entityManager->getMetadataFactory()->getAllMetadata();
39
	}
40
41
}
42