Completed
Pull Request — master (#343)
by Leny
10:06
created

BaseFilter::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 3
b 1
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Bundle\FilterBundle\Filter;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\QueryBuilder;
7
use Symfony\Component\Form\AbstractType;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
abstract class BaseFilter extends AbstractType implements BaseFilterInterface
12
{
13
    protected $entityManager;
14
    protected $request;
15
16
    /**
17
     * @param EntityManager $em
18
     * @param Request       $request
19
     */
20
    public function __construct(EntityManager $em, Request $request)
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...
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
21
    {
22
        $this->entityManager = $em;
23
        $this->request = $request;
24
    }
25
26
    abstract public function buildQuery(QueryBuilder $qb, array $parameters);
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function configureOptions(OptionsResolver $resolver)
32
    {
33
        $resolver->setDefaults([
34
            'csrf_protection' => false,
35
            'widget'          => null,
36
            'multiple'        => false,
37
            'filter'          => null,
38
            'listing_id'      => null,
39
        ]);
40
    }
41
42
    /**
43
     * @return EntityManager
44
     */
45
    public function getEntityManager()
46
    {
47
        return $this->entityManager;
48
    }
49
50
    /**
51
     * @return Request
52
     */
53
    public function getRequest()
54
    {
55
        return $this->request;
56
    }
57
}
58