Installer::uninstall()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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