Completed
Push — master ( c83d03...65d547 )
by Karel
03:34
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 60.66%

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 6
dl 24
loc 161
ccs 37
cts 61
cp 0.6066
rs 10
c 0
b 0
f 0

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