Factory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 83
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A newInstaller() 0 3 1
A getEntityManager() 0 7 2
A setupEntityManager() 0 27 2
A getConnection() 0 3 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Store;
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Annotations\AnnotationRegistry;
9
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
10
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
11
use Doctrine\DBAL\Connection;
12
use Doctrine\ORM\EntityManager;
13
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
14
use Doctrine\ORM\Tools\Setup;
15
use Gedmo\Timestampable\TimestampableListener;
16
17
/**
18
 * @since 0.1
19
 *
20
 * @license GNU GPL v2+
21
 */
22
class Factory {
23
24
	private $entityManager;
25
	private $proxyDir;
26
	private $doctrineEntityPaths;
27
	private $additionalMetadataDrivers;
28
29
	private const DEFAULT_DOCTRINE_ENTITY_PATHS = [__DIR__ . '/../Entities/'];
30
31
	/**
32
	 * Factory constructor.
33
	 * @param Connection $connection
34
	 * @param string $proxyDir
35
	 * @param string[] $doctrineEntityPaths Paths to additional annotated entities that are not part of the store
36
	 * @param MappingDriver[] $additionalMetadataDrivers namespace => driver
37
	 */
38 7
	public function __construct( Connection $connection, $proxyDir = '/tmp', array $doctrineEntityPaths = [],
39
			$additionalMetadataDrivers = [] ) {
40 7
		$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...
41 7
		$this->proxyDir = $proxyDir;
42 7
		$this->doctrineEntityPaths = $doctrineEntityPaths;
43 7
		$this->additionalMetadataDrivers = $additionalMetadataDrivers;
44 7
	}
45
46
	/**
47
	 * @since 0.1
48
	 * @return Installer
49
	 */
50 6
	public function newInstaller() {
51 6
		return new Installer( $this->getEntityManager() );
52
	}
53
54
	/**
55
	 * @since 0.1
56
	 * @return EntityManager
57
	 * @throws \Doctrine\DBAL\DBALException
58
	 * @throws \Doctrine\ORM\ORMException
59
	 */
60 7
	public function getEntityManager() {
61 7
		if ( !$this->entityManager ) {
62 7
			$this->entityManager = $this->setupEntityManager();
63
		}
64
65 7
		return $this->entityManager;
66
	}
67
68 7
	private function setupEntityManager() {
69 7
		$config = Setup::createConfiguration();
70 7
		$paths = array_merge( self::DEFAULT_DOCTRINE_ENTITY_PATHS, $this->doctrineEntityPaths );
71
72 7
		$annotationReader = new AnnotationReader();
73 7
		$annotationDriver = new AnnotationDriver( $annotationReader, $paths );
74 7
		AnnotationRegistry::registerLoader( 'class_exists' );
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...istry::registerLoader() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
75 7
		$driver = new MappingDriverChain();
76 7
		$driver->addDriver( $annotationDriver, 'WMDE\Fundraising\Entities' );
0 ignored issues
show
Documentation introduced by
$annotationDriver is of type object<Doctrine\ORM\Mapp...river\AnnotationDriver>, but the function expects a object<Doctrine\Common\P...g\Driver\MappingDriver>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77 7
		foreach ( $this->additionalMetadataDrivers as $namespace => $additionalMetadataDriver ) {
78
			$driver->addDriver( $additionalMetadataDriver, $namespace );
79
		}
80 7
		$config->setMetadataDriverImpl( $driver );
81 7
		$config->setProxyDir( $this->proxyDir );
82
83 7
		$eventManager = $this->connection->getEventManager();
84 7
		$timestampableListener = new TimestampableListener;
85 7
		$timestampableListener->setAnnotationReader( $annotationReader );
86 7
		$eventManager->addEventSubscriber( $timestampableListener );
87
88 7
		$entityManager = EntityManager::create( $this->connection, $config, $eventManager );
89
90 7
		$platform = $entityManager->getConnection()->getDatabasePlatform();
91 7
		$platform->registerDoctrineTypeMapping( 'enum', 'string' );
92
93 7
		return $entityManager;
94
	}
95
96
	/**
97
	 * @since 0.1
98
	 * @return Connection
99
	 */
100 6
	public function getConnection() {
101 6
		return $this->connection;
102
	}
103
104
}
105