Response   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 106
ccs 26
cts 26
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRawData() 0 4 1
A getSerializer() 0 4 1
A has() 0 4 1
A __construct() 0 6 1
A getData() 0 6 1
A processData() 0 8 3
A get() 0 9 2
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\Response;
19
20
use Elastification\Client\Serializer\SerializerInterface;
21
22
/**
23
 * Class Response
24
 *
25
 * @package Elastification\Client\Response
26
 * @author  Daniel Wendlandt
27
 */
28
class Response implements ResponseInterface
29
{
30
31
    /**
32
     * @var string
33
     */
34
    private $rawData;
35
36
    /**
37
     * @var mixed
38
     */
39
    protected $data = null;
40
41
    /**
42
     * @var SerializerInterface
43
     */
44
    private $serializer;
45
46
    /**
47
     * @var array
48
     */
49
    private $serializerParams;
50
51
    /**
52
     * @param                     $rawData
53
     * @param SerializerInterface $serializer
54
     * @param array               $serializerParams
55
     */
56 482
    public function __construct($rawData, SerializerInterface $serializer, array $serializerParams = array())
57
    {
58 482
        $this->rawData = $rawData;
59 482
        $this->serializer = $serializer;
60 482
        $this->serializerParams = $serializerParams;
61 482
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 9
    public function getData()
67
    {
68 9
        $this->processData();
69
70 9
        return $this->data;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 7
    public function getRawData()
77
    {
78 7
        return $this->rawData;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 4
    public function getSerializer()
85
    {
86 4
        return $this->serializer;
87
    }
88
89
    /**
90
     * checks if data is already deserialized.
91
     *
92
     * @author Daniel Wendlandt
93
     */
94 289
    protected function processData()
95
    {
96 289
        if (null === $this->data) {
97 289
            if (null !== $this->rawData) {
98 288
                $this->data = $this->serializer->deserialize($this->rawData, $this->serializerParams);
99 288
            }
100 289
        }
101 289
    }
102
103
    /**
104
     * gets a property of the data object/array
105
     *
106
     * @param string $property
107
     *
108
     * @return mixed
109
     * @author Daniel Wendlandt
110
     */
111 244
    protected function get($property)
112
    {
113 244
        $val = $this->data[$property];
114 244
        if (is_scalar($val)) {
115 172
            return $val;
116
        } else {
117 72
            return $val->getGatewayValue();
118
        }
119
    }
120
121
    /**
122
     * checks if a property of data array/object exists
123
     *
124
     * @param string $property
125
     *
126
     * @return bool
127
     * @author Daniel Wendlandt
128
     */
129 15
    protected function has($property)
130
    {
131 15
        return isset($this->data[$property]);
132
    }
133
}
134