Completed
Push — master ( ddb963...5f6b4c )
by Hugues
07:37
created

AbstractOrmPersistentMessageRepository   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getByMessage() 0 6 1
1
<?php
2
3
namespace HMLB\DDDBundle\Doctrine\ORM;
4
5
use Doctrine\ORM\EntityManager;
6
use HMLB\DDDBundle\Repository\PersistentMessageRepository;
7
8
/**
9
 * AbstractOrmPersistentMessageRepository.
10
 *
11
 * @author Hugues Maignol <[email protected]>
12
 */
13
abstract class AbstractOrmPersistentMessageRepository implements PersistentMessageRepository
14
{
15
    /**
16
     * @var EntityManager
17
     */
18
    protected $em;
19
20
    public function __construct(EntityManager $em)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. 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...
21
    {
22
        $this->em = $em;
23
    }
24
25
    public function getByMessage(string $class)
26
    {
27
        $repo = $this->em->getRepository($class);
28
29
        return $repo->findAll();
30
    }
31
}
32