Completed
Pull Request — master (#1571)
by
unknown
10:12 queued 08:35
created

getEntityQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Doctrine\ORM;
13
14
use Doctrine\ORM\Query;
15
use FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer;
16
17
/**
18
 * Maps Elastica documents with Doctrine objects
19
 * This mapper assumes an exact match between
20
 * elastica documents ids and doctrine object ids.
21
 */
22
class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer
23
{
24
    const ENTITY_ALIAS = 'o';
25
26
    /**
27
     * Fetch objects for theses identifier values.
28
     *
29
     * @param array $identifierValues ids values
30
     * @param bool  $hydrate          whether or not to hydrate the objects, false returns arrays
31
     *
32
     * @return array of objects or arrays
33
     */
34
    protected function findByIdentifiers(array $identifierValues, $hydrate)
35
    {
36
        if (empty($identifierValues)) {
37
            return [];
38
        }
39
        $hydrationMode = $hydrate ? Query::HYDRATE_OBJECT : Query::HYDRATE_ARRAY;
40
41
        $qb = $this->getEntityQueryBuilder();
42
        $qb->andWhere($qb->expr()->in(static::ENTITY_ALIAS.'.'.$this->options['identifier'], ':values'))
43
            ->setParameter('values', $identifierValues);
44
45
        $query = $qb->getQuery();
46
47
        foreach ($this->options['hints'] as $hint) {
48
            $query->setHint($hint['name'], $hint['value']);
49
        }
50
51
        return $query->setHydrationMode($hydrationMode)->execute();
52
    }
53
54
    /**
55
     * Retrieves a query builder to be used for querying by identifiers.
56
     *
57
     * @return \Doctrine\ORM\QueryBuilder
58
     */
59
    protected function getEntityQueryBuilder()
60
    {
61
        $repository = $this->registry
62
            ->getManagerForClass($this->objectClass)
63
            ->getRepository($this->objectClass);
64
65
        return $repository->{$this->options['query_builder_method']}(static::ENTITY_ALIAS);
66
    }
67
}
68