Completed
Push — develop ( 22307e...eb2da7 )
by Mario
01:42
created

FormUnloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace AppBundle\Tenant\Unloader;
4
5
use AppBundle\Entity\Form;
6
use Doctrine\ORM\EntityManager;
7
use Ds\Component\Tenant\Entity\Tenant;
8
use Ds\Component\Tenant\Loader\Unloader;
9
10
/**
11
 * Class FormUnloader
12
 */
13
class FormUnloader implements Unloader
14
{
15
    /**
16
     * @var \Doctrine\ORM\EntityManager
17
     */
18
    protected $entityManager;
19
20
    /**
21
     * Constructor
22
     *
23
     * @param \Doctrine\ORM\EntityManager $entityManager
24
     */
25
    public function __construct(EntityManager $entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. 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...
26
    {
27
        $this->entityManager = $entityManager;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function unload(Tenant $tenant)
34
    {
35
        $builder = $this->entityManager->getRepository(Form::class)->createQueryBuilder('e');
36
        $builder
37
            ->delete()
38
            ->where('e.tenant = :tenant')
39
            ->setParameter('tenant', $tenant->getUuid());
40
        $query = $builder->getQuery();
41
        $query->execute();
42
    }
43
}
44