Completed
Push — master ( fbc1fa...9bba21 )
by
unknown
14s
created

transformToElasticaDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 1
eloc 5
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\Type;
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 Type                                $type
31
     * @param ModelToElasticaTransformerInterface $transformer
32
     * @param string                              $objectClass
33
     * @param callable                            $serializer
34
     */
35
    public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, $serializer)
36
    {
37
        parent::__construct($type, $transformer, $objectClass, []);
38
39
        $this->serializer = $serializer;
40
    }
41
42
    /**
43
     * Transforms an object to an elastica document
44
     * with just the identifier set.
45
     *
46
     * @param object $object
47
     *
48
     * @return Document the elastica document
49
     */
50
    public function transformToElasticaDocument($object)
51
    {
52
        $document = $this->transformer->transform($object, []);
53
54
        $data = call_user_func($this->serializer, $object);
55
        $document->setData($data);
56
57
        return $document;
58
    }
59
}
60