Completed
Push — 2.0 ( ee0168...76e968 )
by Marco
11:26
created

Database::getEntityManager()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 50
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 8.8571
c 0
b 0
f 0
cc 4
eloc 27
nc 5
nop 0
1
<?php namespace Comodojo\Extender\Components;
2
3
use \Comodojo\Foundation\Base\Configuration;
4
use \Comodojo\Extender\Traits\ConfigurationTrait;
5
use \Doctrine\ORM\Tools\Setup;
6
use \Doctrine\ORM\EntityManager;
7
use \Doctrine\DBAL\DriverManager;
8
use \Doctrine\DBAL\Configuration as DoctrineConfiguration;
9
use \Doctrine\ORM\Query\ResultSetMappingBuilder;
10
use \Doctrine\Common\Cache\ApcCache;
11
12
/**
13
* @package     Comodojo Extender
14
* @author      Marco Giovinazzi <[email protected]>
15
* @license     MIT
16
*
17
* LICENSE:
18
*
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
* THE SOFTWARE.
26
 */
27
28
class Database {
29
30
    use ConfigurationTrait;
31
32
    protected $entity_manager;
33
34
    public function __construct(Configuration $configuration) {
35
36
        $this->setConfiguration($configuration);
37
38
    }
39
40
    public function getConnection() {
41
42
        $configuration = $this->getConfiguration();
43
        $connection_params = self::getConnectionParameters($configuration);
44
        $driverConfig = new DoctrineConfiguration();
45
46
        $devmode = $configuration->get('database-devmode') === true ? true : false;
47
48
        // currently only ApcCache driver is supported
49
        if ( empty($devmode) ) {
50
51
            $cache = new ApcCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ApcCache has been deprecated with message: since version 1.6, use ApcuCache instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
52
            $driverConfig->setResultCacheImpl($cache);
53
54
        }
55
56
        return DriverManager::getConnection($connection_params, $driverConfig);
57
58
    }
59
60
    public function getEntityManager() {
61
62
        if ( $this->entity_manager === null ) {
63
64
            $configuration = $this->getConfiguration();
65
66
            $connection_params = self::getConnectionParameters($configuration);
67
68
            $entity_repositories = self::getEntityRepositories($configuration);
69
70
            $devmode = $configuration->get('database-devmode') === true ? true : false;
71
            $proxies_folder = $configuration->get('database-proxies');
72
73
            $base_folder = $configuration->get('base-path');
74
75
            $driverConfig = new DoctrineConfiguration();
0 ignored issues
show
Unused Code introduced by
$driverConfig is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
77
            $db_config = Setup::createAnnotationMetadataConfiguration(
78
                $entity_repositories,
79
                $devmode,
80
                "$base_folder/$proxies_folder",
81
                null,
82
                false
83
            );
84
85
            if ( $devmode ) {
86
87
                $db_config->setAutoGenerateProxyClasses(true);
88
89
            } else {
90
91
                $cache = new ApcCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ApcCache has been deprecated with message: since version 1.6, use ApcuCache instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
92
                $db_config->setAutoGenerateProxyClasses(false);
93
                $db_config->setQueryCacheImpl($cache);
94
                $db_config->setResultCacheImpl($cache);
95
                $db_config->setMetadataCacheImpl($cache);
96
97
            }
98
99
            $this->entity_manager = EntityManager::create($connection_params, $db_config);
100
101
        } else {
102
103
            $this->entity_manager->clear();
104
105
        }
106
107
        return $this->entity_manager;
108
109
    }
110
111
    public function setEntityManager(EntityManager $manager) {
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $manager. 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...
112
113
        $this->entity_manager = $manager;
114
115
        return $this;
116
117
    }
118
119
    public static function init(Configuration $configuration) {
120
121
        return new Database($configuration);
122
123
    }
124
125
    protected static function getConnectionParameters(Configuration $configuration) {
126
127
        $connection_params = $configuration->get('database-params');
128
        $base_folder = $configuration->get('base-path');
129
130
        if ( isset($connection_params['path']) ) $connection_params['path'] = $base_folder."/".$connection_params['path'];
131
132
        return $connection_params;
133
134
    }
135
136
    protected static function getEntityRepositories(Configuration $configuration) {
137
138
        $base_folder = $configuration->get('base-path');
139
        $repos = $configuration->get('database-repositories');
140
141
        return array_map(function($repo) use ($base_folder) {
142
            return "$base_folder/$repo";
143
        }, $repos);
144
145
    }
146
147
}
148