Doctrine2   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 2
dl 0
loc 68
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A doExecute() 0 8 2
A doExecuteInRepository() 0 8 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Dao;
22
23
use Doctrine\ORM\EntityManager;
24
25
/**
26
 * An abstract Data Access Object for the Doctrine ORM library.
27
 *
28
 * @copyright 2015-2018 LibreWorks contributors
29
 * @license   Apache-2.0
30
 */
31
abstract class Doctrine2 extends Dao
32
{
33
    /**
34
     * @var \Doctrine\ORM\EntityManager The entity manager
35
     */
36
    protected $manager;
37
    /**
38
     * @var string
39
     */
40
    protected $entityName;
41
    /**
42
     * @var \Doctrine\ORM\EntityRepository The entity repository
43
     */
44
    protected $repository;
45
46
    /**
47
     * Creates a new MongoDB DAO.
48
     *
49
     * @param \Doctrine\ORM\EntityManager $manager The entity manager
50
     * @param string $entityName The entity name
51
     * @param \Psr\Log\LoggerInterface $logger A logger
52
     */
53 4
    public function __construct(EntityManager $manager, string $entityName, \Psr\Log\LoggerInterface $logger = null)
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...
54
    {
55 4
        parent::__construct($logger);
56 4
        $this->manager = $manager;
57 4
        $this->entityName = $entityName;
58 4
        $this->repository = $manager->getRepository($entityName);
59 4
    }
60
61
    /**
62
     * Executes something in the context of the entityManager.
63
     *
64
     * Exceptions are caught and translated.
65
     *
66
     * @param Closure $cb The closure to execute, takes the entityManager
67
     * @return mixed whatever the function returns, this method also returns
68
     * @throws \Caridea\Dao\Exception If a database problem occurs
69
     * @see \Caridea\Dao\Exception\Translator\Doctrine
70
     */
71 2
    protected function doExecute(\Closure $cb)
72
    {
73
        try {
74 2
            return $cb($this->manager);
75 1
        } catch (\Exception $e) {
76 1
            throw Exception\Translator\Doctrine::translate($e);
77
        }
78
    }
79
80
    /**
81
     * Executes something in the context of the entityRepository.
82
     *
83
     * Exceptions are caught and translated.
84
     *
85
     * @param Closure $cb The closure to execute, takes the entityRepository
86
     * @return mixed whatever the function returns, this method also returns
87
     * @throws \Caridea\Dao\Exception If a database problem occurs
88
     * @see \Caridea\Dao\Exception\Translator\Doctrine
89
     */
90 2
    protected function doExecuteInRepository(\Closure $cb)
91
    {
92
        try {
93 2
            return $cb($this->repository);
94 1
        } catch (\Exception $e) {
95 1
            throw Exception\Translator\Doctrine::translate($e);
96
        }
97
    }
98
}
99