Completed
Pull Request — master (#1569)
by
unknown
04:30
created

transformToElasticaDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.9666
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\Index;
16
use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
17
18
/**
19
 * Inserts, replaces and deletes single objects in an elastica type, making use
20
 * of elastica's serializer support to convert objects in to elastica documents.
21
 * Accepts domain model objects and passes them directly to elastica.
22
 *
23
 * @author Lea Haensenberber <[email protected]>
24
 */
25
class ObjectSerializerPersister extends ObjectPersister
26
{
27
    protected $serializer;
28
29
    /**
30
     * @param Index                               $index
31
     * @param ModelToElasticaTransformerInterface $transformer
32
     * @param string                              $objectClass
33
     * @param callable                            $serializer
34
     */
35
    public function __construct(Index $index, ModelToElasticaTransformerInterface $transformer, $objectClass, $serializer, array $options = [])
36
    {
37
        parent::__construct($index, $transformer, $objectClass, [], $options);
38
39
        $this->serializer = $serializer;
40
    }
41
42
    /**
43
     * Transforms an object to an elastica document
44
     * with just the identifier set.
45
     */
46
    public function transformToElasticaDocument(object $object): Document
47
    {
48
        $document = $this->transformer->transform($object, []);
49
50
        $data = call_user_func($this->serializer, $object);
51
        $document->setData($data);
52
53
        return $document;
54
    }
55
}
56