Completed
Pull Request — master (#1062)
by
unknown
08:06
created

AbstractElasticaToModelTransformer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 130
Duplicated Lines 2.31 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 4
dl 3
loc 130
ccs 30
cts 36
cp 0.8333
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getObjectClass() 0 4 1
A __construct() 0 6 1
C transform() 3 30 7
A hybridTransform() 0 17 3
A getIdentifierField() 0 4 1
findByIdentifiers() 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace FOS\ElasticaBundle\Doctrine;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use FOS\ElasticaBundle\HybridResult;
7
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer as BaseTransformer;
8
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
9
10
/**
11
 * Maps Elastica documents with Doctrine objects
12
 * This mapper assumes an exact match between
13
 * elastica documents ids and doctrine object ids.
14
 */
15
abstract class AbstractElasticaToModelTransformer extends BaseTransformer
16
{
17
    /**
18
     * Manager registry.
19
     *
20
     * @var ManagerRegistry
21
     */
22
    protected $registry = null;
23
24
    /**
25
     * Class of the model to map to the elastica documents.
26
     *
27
     * @var string
28
     */
29
    protected $objectClass = null;
30
31
    /**
32
     * Optional parameters.
33
     *
34
     * @var array
35
     */
36
    protected $options = array(
37
        'hints'        => array(),
38
        'hydrate'        => true,
39
        'identifier'     => 'id',
40
        'ignore_missing' => false,
41
        'query_builder_method' => 'createQueryBuilder',
42
    );
43
44
    /**
45
     * Instantiates a new Mapper.
46
     *
47
     * @param ManagerRegistry $registry
48
     * @param string $objectClass
49
     * @param array  $options
50
     */
51 4
    public function __construct(ManagerRegistry $registry, $objectClass, array $options = array())
52
    {
53 4
        $this->registry    = $registry;
54 4
        $this->objectClass = $objectClass;
55 4
        $this->options     = array_merge($this->options, $options);
56 4
    }
57
58
    /**
59
     * Returns the object class that is used for conversion.
60
     *
61
     * @return string
62
     */
63
    public function getObjectClass()
64
    {
65
        return $this->objectClass;
66
    }
67
68
    /**
69
     * Transforms an array of elastica objects into an array of
70
     * model objects fetched from the doctrine repository.
71
     *
72
     * @param array $elasticaObjects of elastica objects
73
     *
74
     * @throws \RuntimeException
75
     *
76
     * @return array
77
     **/
78 1
    public function transform(array $elasticaObjects)
79
    {
80 1
        $ids = $highlights = array();
81 1
        foreach ($elasticaObjects as $elasticaObject) {
82 1
            $ids[] = $elasticaObject->getId();
83 1
            $highlights[$elasticaObject->getId()] = $elasticaObject->getHighlights();
84
        }
85
86 1
        $objects = $this->findByIdentifiers($ids, $this->options['hydrate']);
87 1 View Code Duplication
        if (!$this->options['ignore_missing'] && count($objects) < count($elasticaObjects)) {
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...
88
            throw new \RuntimeException('Cannot find corresponding Doctrine objects for all Elastica results.');
89
        };
90
91 1
        foreach ($objects as $object) {
92 1
            if ($object instanceof HighlightableModelInterface) {
93 1
                $object->setElasticHighlights($highlights[$object->getId()]);
94
            }
95
        }
96
97
        // sort objects in the order of ids
98 1
        $idPos = array_flip($ids);
99 1
        if ($this->options['hydrate']) {
100 1
            $identifier = $this->options['identifier'];
101
        } else {
102
            $identifier = '[' . $this->options['identifier'] . ']';
103
        }
104 1
        usort($objects, $this->getSortingClosure($idPos, $identifier));
105
106 1
        return $objects;
107
    }
108
109 1
    public function hybridTransform(array $elasticaObjects)
110
    {
111 1
        $indexedElasticaResults = array();
112 1
        foreach ($elasticaObjects as $elasticaObject) {
113 1
            $indexedElasticaResults[$elasticaObject->getId()] = $elasticaObject;
114
        }
115
116 1
        $objects = $this->transform($elasticaObjects);
117
118 1
        $result = array();
119 1
        foreach ($objects as $object) {
120 1
            $id = $this->propertyAccessor->getValue($object, $this->options['identifier']);
121 1
            $result[] = new HybridResult($indexedElasticaResults[$id], $object);
122
        }
123
124 1
        return $result;
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130
    public function getIdentifierField()
131
    {
132
        return $this->options['identifier'];
133
    }
134
135
    /**
136
     * Fetches objects by theses identifier values.
137
     *
138
     * @param array   $identifierValues ids values
139
     * @param Boolean $hydrate          whether or not to hydrate the objects, false returns arrays
140
     *
141
     * @return array of objects or arrays
142
     */
143
    abstract protected function findByIdentifiers(array $identifierValues, $hydrate);
144
}
145