SourceSubscribingHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
namespace Elastification\Client\Serializer\JmsSerializer;
19
20
use JMS\Serializer\Context;
21
use JMS\Serializer\GraphNavigator;
22
use JMS\Serializer\Handler\SubscribingHandlerInterface;
23
use JMS\Serializer\JsonDeserializationVisitor;
24
25
/**
26
 * This handler enables us to dynamically inject a deserialization class for the _source element of the
27
 * elasticsearch response.
28
 *
29
 * @package Elastification\Client\Serializer\JmsSerializer
30
 * @author  Mario Mueller
31
 */
32
class SourceSubscribingHandler implements SubscribingHandlerInterface
33
{
34
    /**
35
     * @var string
36
     */
37
    private $sourceDeSerClass;
38
39
    /**
40
     * @param string $sourceDeSerClass
41
     */
42 11
    function __construct($sourceDeSerClass)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
    {
44 11
        $this->sourceDeSerClass = $sourceDeSerClass;
45 11
    }
46
47
    /**
48
     * Return format:
49
     *
50
     *      array(
51
     *          array(
52
     *              'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
53
     *              'format' => 'json',
54
     *              'type' => 'DateTime',
55
     *              'method' => 'serializeDateTimeToJson',
56
     *          ),
57
     *      )
58
     *
59
     * The direction and method keys can be omitted.
60
     *
61
     * @return array
62
     */
63 11
    public static function getSubscribingMethods()
64
    {
65
        return array(
66
            array(
67 11
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
68 11
                'format' => 'json',
69 11
                'type' => 'ElastificationSource',
70 11
                'method' => 'serializeElastificationSource',
71 11
            ),
72 11
        );
73
    }
74
75
    /**
76
     * @param JsonDeserializationVisitor $visitor
77
     * @param                            $data
78
     * @param array                      $type
79
     * @param Context                    $context
80
     *
81
     * @return mixed
82
     * @author Mario Mueller
83
     */
84 1
    public function serializeElastificationSource(
85
        JsonDeserializationVisitor $visitor,
86
        $data,
87
        array $type,
88
        Context $context
89
    ) {
90 1
        $this->sourceDeSerClass = empty($this->sourceDeSerClass) ? $type['name'] : $this->sourceDeSerClass;
91
92 1
        return $visitor->getNavigator()->accept($data, ['name' => $this->sourceDeSerClass], $context);
93
    }
94
95
    /**
96
     * @return string
97
     * @author Mario Mueller (autogenerated code)
98
     */
99 1
    public function getSourceDeSerClass()
100
    {
101 1
        return $this->sourceDeSerClass;
102
    }
103
104
    /**
105
     * @param string $sourceDeSerClass
106
     *
107
     * @return void
108
     * @author Mario Mueller (autogenerated code)
109
     */
110 5
    public function setSourceDeSerClass($sourceDeSerClass)
111
    {
112 5
        $this->sourceDeSerClass = $sourceDeSerClass;
113 5
    }
114
}
115