EntityManagerProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 6
c 5
b 1
f 0
lcom 1
cbo 3
dl 0
loc 76
ccs 19
cts 19
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A setProxyDir() 0 4 1
A setSqlLogger() 0 4 1
A get() 0 14 2
1
<?php
2
/**
3
 * This file is part of the Ray.DoctrineOrmModule package
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\DoctrineOrmModule;
8
9
use Doctrine\DBAL\Logging\SQLLogger;
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Doctrine\ORM\Proxy\ProxyFactory;
13
use Doctrine\ORM\Tools\Setup;
14
use Ray\Di\Di\Inject;
15
use Ray\Di\ProviderInterface;
16
use Ray\DoctrineOrmModule\Annotation\EntityManagerConfig;
17
use Ray\DoctrineOrmModule\Annotation\ProxyDir;
18
19
class EntityManagerProvider implements ProviderInterface
20
{
21
    /**
22
     * @var array
23
     */
24
    private $params;
25
26
    /**
27
     * @var array
28
     */
29
    private $entityDir;
30
31
    /**
32
     * @var string
33
     */
34
    private $proxyDir;
35
36
    /**
37
     * @var SQLLogger
38
     */
39
    private $logger;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param array $config
45
     *
46
     * @EntityManagerConfig
47
     */
48 11
    public function __construct(array $config)
49
    {
50 11
        list($this->params, $entityDir) = $config;
51 11
        $this->entityDir = is_array($entityDir) ? $entityDir : [$entityDir];
52 11
    }
53
54
    /**
55
     * @param string $dir
56
     *
57
     * @ProxyDir
58
     * @Inject(optional=true)
59
     */
60 2
    public function setProxyDir($dir)
61
    {
62 2
        $this->proxyDir = $dir;
63 2
    }
64
65
    /**
66
     * @param SQLLogger $logger
67
     *
68
     * @Inject(optional=true)
69
     */
70 2
    public function setSqlLogger(SQLLogger $logger)
71
    {
72 2
        $this->logger = $logger;
73 2
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @return EntityManagerInterface
79
     */
80 11
    public function get()
81
    {
82 11
        $config = Setup::createAnnotationMetadataConfiguration($this->entityDir, false, null, null, false);
83 11
        $config->setSQLLogger($this->logger);
84
85 11
        if ($this->proxyDir) {
86 2
            $config->setProxyDir($this->proxyDir);
87 2
            $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS);
88 2
        } else {
89 9
            $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
90
        }
91
92 11
        return EntityManager::create($this->params, $config);
93
    }
94
}
95