Passed
Push — master ( 49293b...25c2e9 )
by Petr
04:03
created

EntityValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 48
ccs 0
cts 29
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findEntity() 0 16 2
A checkAnnotationParameters() 0 12 4
1
<?php
2
3
namespace AppBundle\Form\Validation\Infrastructure;
4
5
use AppBundle\Exception\HttpRuntimeException;
6
use AppBundle\Form\Validation\EntityDoesNotExists;
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\ORMException;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\ConstraintValidator;
11
12
/**
13
 * @author Vehsamrak
14
 */
15
abstract class EntityValidator extends ConstraintValidator
16
{
17
18
    /** @var EntityManager */
19
    private $entityManager;
20
21
    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...
22
    {
23
        $this->entityManager = $entityManager;
24
    }
25
26
    /**
27
     * @param string $entityFieldValue Entity field value
28
     * @param EntityDoesNotExists $constraint
29
     */
30
    public function findEntity($entityFieldValue, Constraint $constraint)
31
    {
32
        try {
33
            $repository = $this->entityManager->getRepository($constraint->entityClass);
34
        } catch (ORMException $exception) {
35
            throw new HttpRuntimeException($exception->getMessage());
36
        }
37
38
        $entity = $repository->findOneBy(
39
            [
40
                $constraint->entityField => $entityFieldValue,
41
            ]
42
        );
43
44
        return $entity;
45
    }
46
47
    /**
48
     * @throws HttpRuntimeException
49
     */
50
    public function checkAnnotationParameters(EntityConstraint $constraint)
51
    {
52
        if (empty($constraint->entityClass) || empty($constraint->entityField)) {
53
            throw new HttpRuntimeException('"entityClass" and "entityField" annotation parameters are mandatory.');
54
        }
55
56
        if (!class_exists($constraint->entityClass)) {
57
            throw new HttpRuntimeException(
58
                sprintf('Class "%s" given in annotation does not exist.', $constraint->entityClass)
59
            );
60
        }
61
    }
62
}
63