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

ObjectSerializerPersister   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 35
ccs 0
cts 12
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A transformToElasticaDocument() 0 9 1
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