Completed
Pull Request — develop (#50)
by Axel
02:57
created

BetaTestManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1.037
1
<?php
2
3
namespace Developtech\AgilityBundle\Manager;
4
5
use Doctrine\ORM\EntityManager;
6
7
use Developtech\AgilityBundle\Entity\Project;
8
use Developtech\AgilityBundle\Entity\BetaTest;
9
10
class BetaTestManager {
11
    /** @var EntityManager **/
12
    protected $em;
13
14
    /**
15
     * @param EntityManager $em
16
     */
17 1
    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...
18 1
        $this->em = $em;
19
    }
20
21
    /**
22
     * @param Project $project
23
     * @return array
24
     */
25 1
    public function getByProject(Project $project) {
26
        return $this->em->getRepository(BetaTest::class)->findByProject($project);
27 1
    }
28
}
29