Completed
Pull Request — master (#1569)
by
unknown
29:54 queued 20:53
created

ObjectPersister::insertOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
    private $options;
34
35
    /**
36
     * @param Type                                $type
37
     * @param ModelToElasticaTransformerInterface $transformer
38
     * @param string                              $objectClass
39
     * @param array                               $fields
40
     */
41
    public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields, array $options = [])
42
    {
43
        $this->type = $type;
44
        $this->transformer = $transformer;
45
        $this->objectClass = $objectClass;
46
        $this->fields = $fields;
47
        $this->options = $options;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function handlesObject($object)
54
    {
55
        return $object instanceof $this->objectClass;
56
    }
57
58
    /**
59
     * @param LoggerInterface $logger
60
     */
61
    public function setLogger(LoggerInterface $logger)
62
    {
63
        $this->logger = $logger;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function insertOne($object)
70
    {
71
        $this->insertMany([$object]);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function replaceOne($object)
78
    {
79
        $this->replaceMany([$object]);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function deleteOne($object)
86
    {
87
        $this->deleteMany([$object]);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function deleteById($id, $routing = false)
94
    {
95
        $this->deleteManyByIdentifiers([$id], $routing);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 View Code Duplication
    public function insertMany(array $objects)
102
    {
103
        $documents = [];
104
        foreach ($objects as $object) {
105
            $documents[] = $this->transformToElasticaDocument($object);
106
        }
107
        try {
108
            $this->type->addDocuments($documents, $this->options);
109
        } catch (BulkException $e) {
110
            $this->log($e);
111
        }
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function replaceMany(array $objects)
118
    {
119
        $documents = [];
120
        foreach ($objects as $object) {
121
            $document = $this->transformToElasticaDocument($object);
122
            $document->setDocAsUpsert(true);
123
            $documents[] = $document;
124
        }
125
126
        try {
127
            $this->type->updateDocuments($documents, $this->options);
128
        } catch (BulkException $e) {
129
            $this->log($e);
130
        }
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 View Code Duplication
    public function deleteMany(array $objects)
137
    {
138
        $documents = [];
139
        foreach ($objects as $object) {
140
            $documents[] = $this->transformToElasticaDocument($object);
141
        }
142
        try {
143
            $this->type->deleteDocuments($documents);
144
        } catch (BulkException $e) {
145
            $this->log($e);
146
        }
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function deleteManyByIdentifiers(array $identifiers, $routing = false)
153
    {
154
        try {
155
            $this->type->deleteIds($identifiers, $routing);
156
        } catch (BulkException $e) {
157
            $this->log($e);
158
        }
159
    }
160
161
    /**
162
     * Transforms an object to an elastica document.
163
     *
164
     * @param object $object
165
     *
166
     * @return Document the elastica document
167
     */
168
    public function transformToElasticaDocument($object)
169
    {
170
        return $this->transformer->transform($object, $this->fields);
171
    }
172
173
    /**
174
     * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw.
175
     *
176
     * @param BulkException $e
177
     *
178
     * @throws BulkException
179
     */
180
    private function log(BulkException $e)
181
    {
182
        if (!$this->logger) {
183
            throw $e;
184
        }
185
186
        $this->logger->error($e);
187
    }
188
}
189