Completed
Push — 3.1.x ( b8b6d2...fd2df9 )
by Karel
14:12 queued 11:28
created

ObjectPersister   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 177
Duplicated Lines 13.56 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 67.19%

Importance

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

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handlesObject() 0 4 1
A setLogger() 0 4 1
A log() 0 8 2
A insertOne() 0 4 1
A replaceOne() 0 4 1
A deleteOne() 0 4 1
A deleteById() 0 4 1
A insertMany() 12 12 3
A replaceMany() 0 15 3
A deleteMany() 12 12 3
A deleteManyByIdentifiers() 0 8 2
A transformToElasticaDocument() 0 4 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\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 13
    public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields)
26
    {
27 13
        $this->type            = $type;
28 13
        $this->transformer     = $transformer;
29 13
        $this->objectClass     = $objectClass;
30 13
        $this->fields          = $fields;
31 13
    }
32
33
    /**
34
     * If the ObjectPersister handles a given object.
35
     *
36
     * @param object $object
37
     *
38
     * @return bool
39
     */
40
    public function handlesObject($object)
41
    {
42
        return $object instanceof $this->objectClass;
43
    }
44
45
    /**
46
     * @param LoggerInterface $logger
47
     */
48
    public function setLogger(LoggerInterface $logger)
49
    {
50
        $this->logger = $logger;
51
    }
52
53
    /**
54
     * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw.
55
     *
56
     * @param BulkException $e
57
     *
58
     * @throws BulkException
59
     */
60
    private function log(BulkException $e)
61
    {
62
        if (! $this->logger) {
63
            throw $e;
64
        }
65
66
        $this->logger->error($e);
67
    }
68
69
    /**
70
     * Insert one object into the type
71
     * The object will be transformed to an elastica document.
72
     *
73
     * @param object $object
74
     */
75 4
    public function insertOne($object)
76
    {
77 4
        $this->insertMany(array($object));
78 3
    }
79
80
    /**
81
     * Replaces one object in the type.
82
     *
83
     * @param object $object
84
     **/
85 4
    public function replaceOne($object)
86
    {
87 4
        $this->replaceMany(array($object));
88 3
    }
89
90
    /**
91
     * Deletes one object in the type.
92
     *
93
     * @param object $object
94
     **/
95 3
    public function deleteOne($object)
96
    {
97 3
        $this->deleteMany(array($object));
98 2
    }
99
100
    /**
101
     * Deletes one object in the type by id.
102
     *
103
     * @param mixed $id
104
     **/
105
    public function deleteById($id)
106
    {
107
        $this->deleteManyByIdentifiers(array($id));
108
    }
109
110
    /**
111
     * Bulk insert an array of objects in the type for the given method.
112
     *
113
     * @param array $objects array of domain model objects
114
     * @param string Method to call
115
     */
116 7 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...
117
    {
118 7
        $documents = array();
119 7
        foreach ($objects as $object) {
120 7
            $documents[] = $this->transformToElasticaDocument($object);
121 5
        }
122
        try {
123 5
            $this->type->addDocuments($documents);
124 5
        } catch (BulkException $e) {
125
            $this->log($e);
126
        }
127 5
    }
128
129
    /**
130
     * Bulk update an array of objects in the type.  Create document if it does not already exist.
131
     *
132
     * @param array $objects array of domain model objects
133
     */
134 4
    public function replaceMany(array $objects)
135
    {
136 4
        $documents = array();
137 4
        foreach ($objects as $object) {
138 4
            $document = $this->transformToElasticaDocument($object);
139 3
            $document->setDocAsUpsert(true);
140 3
            $documents[] = $document;
141 3
        }
142
143
        try {
144 3
            $this->type->updateDocuments($documents);
145 3
        } catch (BulkException $e) {
146
            $this->log($e);
147
        }
148 3
    }
149
150
    /**
151
     * Bulk deletes an array of objects in the type.
152
     *
153
     * @param array $objects array of domain model objects
154
     */
155 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...
156
    {
157 3
        $documents = array();
158 3
        foreach ($objects as $object) {
159 3
            $documents[] = $this->transformToElasticaDocument($object);
160 2
        }
161
        try {
162 2
            $this->type->deleteDocuments($documents);
163 2
        } catch (BulkException $e) {
164
            $this->log($e);
165
        }
166 2
    }
167
168
    /**
169
     * Bulk deletes records from an array of identifiers.
170
     *
171
     * @param array $identifiers array of domain model object identifiers
172
     */
173
    public function deleteManyByIdentifiers(array $identifiers)
174
    {
175
        try {
176
            $this->type->getIndex()->getClient()->deleteIds($identifiers, $this->type->getIndex(), $this->type);
177
        } catch (BulkException $e) {
178
            $this->log($e);
179
        }
180
    }
181
182
    /**
183
     * Transforms an object to an elastica document.
184
     *
185
     * @param object $object
186
     *
187
     * @return Document the elastica document
188
     */
189 4
    public function transformToElasticaDocument($object)
190
    {
191 4
        return $this->transformer->transform($object, $this->fields);
192
    }
193
}
194