Completed
Push — master ( bada3e...b76633 )
by Jeroen De
02:31
created

Factory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 8
Bugs 0 Features 4
Metric Value
wmc 6
c 8
b 0
f 4
lcom 1
cbo 8
dl 0
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A newInstaller() 0 3 1
A getEntityManager() 0 7 2
A setupEntityManager() 0 21 1
A getConnection() 0 3 1
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->entityManager = $this->setupEntityManager();
46
		}
47
48
		return $this->entityManager;
49
	}
50
51
	private 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
		$entityManager = EntityManager::create( $this->connection, $config, $eventManager );
66
67
		$platform = $entityManager->getConnection()->getDatabasePlatform();
68
		$platform->registerDoctrineTypeMapping( 'enum', 'string' );
69
70
		return $entityManager;
71
	}
72
73
	/**
74
	 * @since 0.1
75
	 * @return Connection
76
	 */
77
	public function getConnection() {
78
		return $this->connection;
79
	}
80
81
}
82