Completed
Pull Request — master (#1118)
by Karel
05:07
created

setPropertyAccessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of the FOSElasticaBundle project.
5
 *
6
 * (c) FriendsOfSymfony <https://github.com/FriendsOfSymfony/FOSElasticaBundle/graphs/contributors>
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 9
    public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
31
    {
32 9
        $this->propertyAccessor = $propertyAccessor;
33 9
    }
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
     * @return callable
42
     */
43
    protected function getSortingClosure(array $idPos, $identifierPath)
44
    {
45
        $propertyAccessor = $this->propertyAccessor;
46
47 View Code Duplication
        return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
            return $idPos[(string) $propertyAccessor->getValue($a, $identifierPath)] > $idPos[(string) $propertyAccessor->getValue($b, $identifierPath)];
49
        };
50
    }
51
}
52