Completed
Push — master ( cba6b5...9256c7 )
by Paul
06:29
created

BusinessEntityHiddenType::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Form\Field;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Component\Form\AbstractType;
7
use Symfony\Component\Form\CallbackTransformer;
8
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
9
use Symfony\Component\Form\FormBuilderInterface;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
class BusinessEntityHiddenType extends AbstractType
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
13
{
14
    protected $entityManager;
15
16
    public function __construct(EntityManager $entityManager)
1 ignored issue
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...
introduced by
Missing function doc comment
Loading history...
17
    {
18
        $this->entityManager = $entityManager;
19
    }
20
21
    public function getParent()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
22
    {
23
        return HiddenType::class;
24
    }
25
26
    public function buildForm(FormBuilderInterface $builder, array $options)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
27
    {
28
        $entityManager = $this->entityManager;
29
        parent::buildForm($builder, $options);
30
31
        $builder->addModelTransformer(
32
            new CallbackTransformer(
33
                function ($businessEntity) {
34
                    return $businessEntity;
35
                },
36
                function ($nameToBusinessEntity) use ($entityManager) {
37
                    return $entityManager->getRepository(
38
                        'VictoireBusinessEntityBundle:BusinessEntity'
39
                    )->findOneByName(
40
                        $nameToBusinessEntity
41
                    );
42
                }
43
            )
44
        );
45
    }
46
47
    /**
48
     * bind form to WidgetRedactor entity.
49
     *
50
     * @param OptionsResolver $resolver
51
     */
52
    public function configureOptions(OptionsResolver $resolver)
53
    {
54
        $resolver->setDefaults([
55
            'translation_domain' => 'victoire',
56
        ]);
57
    }
58
}
59