Completed
Pull Request — master (#1290)
by
unknown
04:42
created

ObjectPersister::deleteById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
34
    /**
35
     * @param Type                                $type
36
     * @param ModelToElasticaTransformerInterface $transformer
37
     * @param string                              $objectClass
38
     * @param array                               $fields
39
     */
40 17
    public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, array $fields)
41
    {
42 17
        $this->type = $type;
43 17
        $this->transformer = $transformer;
44 17
        $this->objectClass = $objectClass;
45 17
        $this->fields = $fields;
46 17
    }
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
        return $this->insertMany([$object]);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 5
    public function replaceOne($object)
92
    {
93 5
        return $this->replaceMany([$object]);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 3
    public function deleteOne($object)
100
    {
101 3
        return $this->deleteMany([$object]);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function deleteById($id)
108
    {
109
        return $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
            return $this->type->addDocuments($documents);
123
        } catch (BulkException $e) {
124
            $this->log($e);
125
        }
126
    }
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
            return $this->type->updateDocuments($documents);
142
        } catch (BulkException $e) {
143
            $this->log($e);
144
        }
145
    }
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
            return $this->type->deleteDocuments($documents);
158
        } catch (BulkException $e) {
159
            $this->log($e);
160
        }
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function deleteManyByIdentifiers(array $identifiers)
167
    {
168
        try {
169
            return $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