Completed
Pull Request — master (#1079)
by Guillaume
07:46
created

ObjectPersister::deleteManyByIdentifiers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
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 19
    public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields)
32
    {
33 19
        $this->type            = $type;
34 19
        $this->transformer     = $transformer;
35 19
        $this->objectClass     = $objectClass;
36 19
        $this->fields          = $fields;
37 19
    }
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
        }
112
        try {
113 8
            $this->type->addDocuments($documents);
114
        } 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
        }
130
131
        try {
132 4
            $this->type->updateDocuments($documents);
133
        } 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
        }
147
        try {
148 2
            $this->type->deleteDocuments($documents);
149
        } 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