Completed
Pull Request — master (#52)
by
unknown
02:20
created

Factory::setupEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace WMDE\Fundraising\Store;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
10
use Doctrine\ORM\Tools\Setup;
11
use Gedmo\Timestampable\TimestampableListener;
12
13
/**
14
 * @since 0.1
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Jonas Kress
19
 * @author Kai Nissen < [email protected] >
20
 */
21
class Factory {
22
23
	private $entityManager;
24
25
	public function __construct( Connection $connection ) {
26
		$this->connection = $connection;
0 ignored issues
show
Bug introduced by
The property connection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
	}
28
29
	/**
30
	 * @since 0.1
31
	 * @return Installer
32
	 */
33
	public function newInstaller() {
34
		return new Installer( $this->getEntityManager() );
35
	}
36
37
	/**
38
	 * @since 0.1
39
	 * @return EntityManager
40
	 * @throws \Doctrine\DBAL\DBALException
41
	 * @throws \Doctrine\ORM\ORMException
42
	 */
43
	public function getEntityManager() {
44
		if ( !$this->entityManager ) {
45
			$this->setupEntityManager();
46
		}
47
48
		return $this->entityManager;
49
	}
50
51
	public function setupEntityManager() {
52
		$paths = [ __DIR__ . '/../Entities/' ];
53
		$config = Setup::createConfiguration();
54
55
		$annotationReader = new AnnotationReader();
56
		$driver = new AnnotationDriver( $annotationReader, $paths );
57
		AnnotationRegistry::registerLoader( 'class_exists' );
58
		$config->setMetadataDriverImpl( $driver );
59
60
		$eventManager = $this->connection->getEventManager();
61
		$timestampableListener = new TimestampableListener;
62
		$timestampableListener->setAnnotationReader( $annotationReader );
63
		$eventManager->addEventSubscriber( $timestampableListener );
64
65
		$eventManager->addEventListener( 'prePersist', new TransferCodeListener( new TransferCodeGenerator() ) );
66
67
		$entityManager = EntityManager::create( $this->connection, $config, $eventManager );
68
69
		$platform = $entityManager->getConnection()->getDatabasePlatform();
70
		$platform->registerDoctrineTypeMapping( 'enum', 'string' );
71
72
		$this->entityManager = $entityManager;
73
	}
74
75
	/**
76
	 * @since 0.1
77
	 * @return Connection
78
	 */
79
	public function getConnection() {
80
		return $this->connection;
81
	}
82
83
}
84