GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DoctrineORMPopulationFetcher::createQueryBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Nimble\ElasticBundle\Doctrine\ORM\Populator;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\QueryBuilder;
9
use Nimble\ElasticBundle\Populator\PopulationFetcherInterface;
10
11
class DoctrineORMPopulationFetcher implements PopulationFetcherInterface
12
{
13
    const ROOT_ALIAS = 'e';
14
15
    /**
16
     * @var EntityRepository
17
     */
18
    protected $entityRepository;
19
20
    /**
21
     * @var ClassMetadata
22
     */
23
    protected $entityClassMetadata;
24
25
    /**
26
     * @var EntityManager
27
     */
28
    protected $entityManager;
29
30
    /**
31
     * @param EntityManager $entityManager
32
     * @param string $entityName
33
     */
34
    public function __construct(EntityManager $entityManager, $entityName)
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...
35
    {
36
        $this->entityManager = $entityManager;
37
        $this->entityRepository = $entityManager->getRepository($entityName);
38
        $this->entityClassMetadata = $entityManager->getClassMetadata(
39
            $this->entityRepository->getClassName()
40
        );
41
    }
42
43
    /**
44
     * @return QueryBuilder
45
     */
46
    protected function createQueryBuilder()
47
    {
48
        return $this->entityRepository->createQueryBuilder(self::ROOT_ALIAS);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getEntityCount()
55
    {
56
        $qb = $this->createQueryBuilder();
57
58
        $qb->select($qb->expr()->count(self::ROOT_ALIAS));
59
60
        return $qb->getQuery()->getSingleScalarResult();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function fetchEntities($offset = 0, $limit = null)
67
    {
68
        $this->entityManager->clear(); /* TODO: Check if this gives us any value. */
69
70
        $qb = $this->createQueryBuilder();
71
72
        $qb->select(self::ROOT_ALIAS);
73
74
        foreach ($this->entityClassMetadata->getIdentifierFieldNames() as $fieldName) {
75
            $qb->addOrderBy(sprintf('%s.%s', self::ROOT_ALIAS, $fieldName));
76
        }
77
78
        $qb->setFirstResult($offset);
79
80
        if (null !== $limit) {
81
            $qb->setMaxResults($limit);
82
        }
83
84
        return $qb->getQuery()->getResult();
85
    }
86
}