Completed
Push — master ( fef1f2...3eb154 )
by Tim
7s
created

ElasticaToModelTransformer::findByIdentifiers()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

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