Completed
Pull Request — master (#1246)
by Istvan
04:23
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
/**
13
 * This file is part of the FOSElasticaBundle project.
14
 *
15
 * (c) FriendsOfSymfony <https://github.com/FriendsOfSymfony/FOSElasticaBundle/graphs/contributors>
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 */
20
21
namespace FOS\ElasticaBundle\Transformer;
22
23
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
24
25
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
26
{
27
    /**
28
     * PropertyAccessor instance.
29
     *
30
     * @var PropertyAccessorInterface
31
     */
32
    protected $propertyAccessor;
33
34
    /**
35
     * Set the PropertyAccessor instance.
36
     *
37
     * @param PropertyAccessorInterface $propertyAccessor
38
     */
39 10
    public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
40
    {
41 10
        $this->propertyAccessor = $propertyAccessor;
42 10
    }
43
44
    /**
45
     * Returns a sorting closure to be used with usort() to put retrieved objects
46
     * back in the order that they were returned by ElasticSearch.
47
     *
48
     * @param array  $idPos
49
     * @param string $identifierPath
50
     *
51
     * @return callable
52
     */
53
    protected function getSortingClosure(array $idPos, $identifierPath)
54
    {
55
        $propertyAccessor = $this->propertyAccessor;
56
57
        return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
58
            return $idPos[(string) $propertyAccessor->getValue($a, $identifierPath)] > $idPos[(string) $propertyAccessor->getValue($b, $identifierPath)];
59
        };
60
    }
61
}
62