Completed
Pull Request — master (#1411)
by Floran
09:07
created

AbstractElasticaToModelTransformer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 37.5%

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 37
ccs 3
cts 8
cp 0.375
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setPropertyAccessor() 0 4 1
A getSortingClosure() 0 8 1
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\Transformer;
13
14
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
15
16
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
17
{
18
    /**
19
     * PropertyAccessor instance.
20
     *
21
     * @var PropertyAccessorInterface
22
     */
23
    protected $propertyAccessor;
24
25
    /**
26
     * Set the PropertyAccessor instance.
27
     *
28
     * @param PropertyAccessorInterface $propertyAccessor
29
     */
30 10
    public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
31
    {
32 10
        $this->propertyAccessor = $propertyAccessor;
33 10
    }
34
35
    /**
36
     * Returns a sorting closure to be used with usort() to put retrieved objects
37
     * back in the order that they were returned by ElasticSearch.
38
     *
39
     * @param array  $idPos
40
     * @param string $identifierPath
41
     *
42
     * @return callable
43
     */
44
    protected function getSortingClosure(array $idPos, $identifierPath)
45
    {
46
        $propertyAccessor = $this->propertyAccessor;
47
48
        return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
49
            return $idPos[(string) $propertyAccessor->getValue($a, $identifierPath)] > $idPos[(string) $propertyAccessor->getValue($b, $identifierPath)];
50
        };
51
    }
52
}
53