Completed
Pull Request — master (#1134)
by Istvan
14:27
created

ObjectPersister   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 161
Duplicated Lines 14.91 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 67.19%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 21
c 1
b 0
f 1
lcom 2
cbo 6
dl 24
loc 161
ccs 43
cts 64
cp 0.6719
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A replaceOne() 0 4 1
A deleteOne() 0 4 1
A deleteById() 0 4 1
A handlesObject() 0 4 1
A setLogger() 0 4 1
A log() 0 8 2
A insertOne() 0 4 1
A deleteManyByIdentifiers() 0 8 2
A transformToElasticaDocument() 0 4 1
A insertMany() 12 12 3
A replaceMany() 0 15 3
A deleteMany() 12 12 3

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\Persister;
4
5
use Psr\Log\LoggerInterface;
6
use Elastica\Exception\BulkException;
7
use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
8
use Elastica\Type;
9
use Elastica\Document;
10
11
/**
12
 * Inserts, replaces and deletes single documents in an elastica type
13
 * Accepts domain model objects and converts them to elastica documents.
14
 *
15
 * @author Thibault Duplessis <[email protected]>
16
 */
17
class ObjectPersister implements ObjectPersisterInterface
18
{
19
    protected $type;
20
    protected $transformer;
21
    protected $objectClass;
22
    protected $fields;
23
    protected $logger;
24
25
    /**
26
     * @param Type                                $type
27
     * @param ModelToElasticaTransformerInterface $transformer
28
     * @param string                              $objectClass
29
     * @param array                               $fields
30
     */
31 21
    public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields)
32
    {
33 21
        $this->type            = $type;
34 21
        $this->transformer     = $transformer;
35 21
        $this->objectClass     = $objectClass;
36 21
        $this->fields          = $fields;
37 21
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function handlesObject($object)
43
    {
44
        return $object instanceof $this->objectClass;
45
    }
46
47
    /**
48
     * @param LoggerInterface $logger
49
     */
50
    public function setLogger(LoggerInterface $logger)
51
    {
52
        $this->logger = $logger;
53
    }
54
55
    /**
56
     * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw.
57
     *
58
     * @param BulkException $e
59
     *
60
     * @throws BulkException
61
     */
62
    private function log(BulkException $e)
63
    {
64
        if (! $this->logger) {
65
            throw $e;
66
        }
67
68
        $this->logger->error($e);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 7
    public function insertOne($object)
75
    {
76 7
        $this->insertMany(array($object));
77 6
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 5
    public function replaceOne($object)
83
    {
84 5
        $this->replaceMany(array($object));
85 4
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 3
    public function deleteOne($object)
91
    {
92 3
        $this->deleteMany(array($object));
93 2
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function deleteById($id)
99
    {
100
        $this->deleteManyByIdentifiers(array($id));
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 10 View Code Duplication
    public function insertMany(array $objects)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
107
    {
108 10
        $documents = array();
109 10
        foreach ($objects as $object) {
110 10
            $documents[] = $this->transformToElasticaDocument($object);
111 8
        }
112
        try {
113 8
            $this->type->addDocuments($documents);
114 8
        } catch (BulkException $e) {
115
            $this->log($e);
116
        }
117 8
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 5
    public function replaceMany(array $objects)
123
    {
124 5
        $documents = array();
125 5
        foreach ($objects as $object) {
126 5
            $document = $this->transformToElasticaDocument($object);
127 4
            $document->setDocAsUpsert(true);
128 4
            $documents[] = $document;
129 4
        }
130
131
        try {
132 4
            $this->type->updateDocuments($documents);
133 4
        } catch (BulkException $e) {
134
            $this->log($e);
135
        }
136 4
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 3 View Code Duplication
    public function deleteMany(array $objects)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
142
    {
143 3
        $documents = array();
144 3
        foreach ($objects as $object) {
145 3
            $documents[] = $this->transformToElasticaDocument($object);
146 2
        }
147
        try {
148 2
            $this->type->deleteDocuments($documents);
149 2
        } catch (BulkException $e) {
150
            $this->log($e);
151
        }
152 2
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function deleteManyByIdentifiers(array $identifiers)
158
    {
159
        try {
160
            $this->type->getIndex()->getClient()->deleteIds($identifiers, $this->type->getIndex(), $this->type);
161
        } catch (BulkException $e) {
162
            $this->log($e);
163
        }
164
    }
165
166
    /**
167
     * Transforms an object to an elastica document.
168
     *
169
     * @param object $object
170
     *
171
     * @return Document the elastica document
172
     */
173 6
    public function transformToElasticaDocument($object)
174
    {
175 6
        return $this->transformer->transform($object, $this->fields);
176
    }
177
}
178