GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TransactionManager   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A beginTransaction() 0 4 1
A commit() 0 4 1
A rollback() 0 4 1
1
<?php
2
/*
3
 * This file is part of the prooph/php-ddd-cargo-sample.
4
 * (c) Alexander Miertsch <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 * 
9
 * Date: 27.03.14 - 20:22
10
 */
11
12
namespace Codeliner\CargoBackend\Infrastructure\Persistence\Transaction;
13
14
use Doctrine\ORM\EntityManager;
15
16
/**
17
 * Class TransactionManager
18
 *
19
 * @package Codeliner\CargoBackend\Infrastructure\Persistence\Transaction
20
 * @author Alexander Miertsch <[email protected]>
21
 */
22
class TransactionManager 
23
{
24
    /**
25
     * @var EntityManager
26
     */
27
    private $entityManager;
28
29
    /**
30
     * @param EntityManager $anEntityManager
31
     */
32
    public function __construct(EntityManager $anEntityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $anEntityManager. 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...
33
    {
34
        $this->entityManager = $anEntityManager;
35
    }
36
37
    /**
38
     * @return void
39
     */
40
    public function beginTransaction()
41
    {
42
        $this->entityManager->beginTransaction();
43
    }
44
45
    /**
46
     * @return void
47
     */
48
    public function commit()
49
    {
50
        $this->entityManager->commit();
51
    }
52
53
    /**
54
     * @return void
55
     */
56
    public function rollback()
57
    {
58
        $this->entityManager->rollback();
59
    }
60
}
61