Completed
Push — master ( 3cdfc2...4aa078 )
by Maksim
03:09
created

ElasticaToModelTransformer::findByIdentifiers()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 5
nop 2
crap 4.0119
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 1
    protected function findByIdentifiers(array $identifierValues, $hydrate)
35
    {
36 1
        if (empty($identifierValues)) {
37
            return [];
38
        }
39 1
        $hydrationMode = $hydrate ? Query::HYDRATE_OBJECT : Query::HYDRATE_ARRAY;
40
41 1
        $qb = $this->getEntityQueryBuilder();
42 1
        $qb->andWhere($qb->expr()->in(static::ENTITY_ALIAS.'.'.$this->options['identifier'], ':values'))
43 1
            ->setParameter('values', $identifierValues);
44
45 1
        $query = $qb->getQuery();
46
47 1
        foreach ($this->options['hints'] as $hint) {
48 1
            $query->setHint($hint['name'], $hint['value']);
49
        }
50
51 1
        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 3
    protected function getEntityQueryBuilder()
60
    {
61 3
        $repository = $this->registry
62 3
            ->getManagerForClass($this->objectClass)
63 3
            ->getRepository($this->objectClass);
64
65 3
        return $repository->{$this->options['query_builder_method']}(static::ENTITY_ALIAS);
66
    }
67
}
68