Response::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 1
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the GestPayWS library.
5
 *
6
 * (c) Manuel Dalla Lana <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EndelWar\GestPayWS\Response;
13
14
use Exception;
15
use InvalidArgumentException;
16
17
/**
18
 * Class Response
19
 * @package EndelWar\GestPayWS\Response
20
 */
21
abstract class Response implements \ArrayAccess
22
{
23
    protected $data = array();
24
    protected $parametersName = array();
25
26
    /**
27
     * @param \SimpleXMLElement $xml
28
     * @throws Exception
29
     */
30 21
    public function __construct($xml)
31
    {
32 21
        $array = json_decode(json_encode($xml), true);
33
        $array = array_map(function ($value) {
34 21
            if (is_array($value) && empty($value)) {
35 21
                return '';
36
            }
37
38 21
            return $value;
39 21
        }, $array);
40 21
        $this->fromArray($array);
41 21
    }
42
43
    /**
44
     * @param string $key
45
     * @param mixed $value
46
     * @throws InvalidArgumentException
47
     */
48 21 View Code Duplication
    public function set($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50 21
        if (!in_array($key, $this->parametersName, true)) {
51 1
            throw new InvalidArgumentException(sprintf('%s is not a valid parameter name.', $key));
52
        }
53 21
        $this->data[$key] = $value;
54 21
    }
55
56
    /**
57
     * @param string $key
58
     *
59
     * @return mixed|null The value at the specified index or null.
60
     */
61 11
    public function get($key)
62
    {
63 11
        if (array_key_exists($key, $this->data)) {
64 9
            return $this->data[$key];
65
        }
66
67 2
        return;
68
    }
69
70
    /**
71
     * @param array $data
72
     */
73 21
    public function fromArray($data)
74
    {
75 21
        foreach ($data as $key => $value) {
76 21
            $this->set($key, $value);
77 21
        }
78 21
    }
79
80
    /**
81
     * @return array
82
     */
83 4
    public function toArray()
84
    {
85 4
        return $this->data;
86
    }
87
88
    /**
89
     * @return mixed
90
     */
91 2
    public function toXML()
92
    {
93 2
        $data = $this->toArray();
94 2
        $xml = new \SimpleXMLElement('<GestPayCryptDecrypt/>');
95 2
        array_walk_recursive($data, function ($value, $key) use ($xml) {
96 2
            $xml->addChild($key, $value);
97 2
        });
98
99 2
        $dom = new \DOMDocument('1.0');
100 2
        $dom->preserveWhiteSpace = false;
101 2
        $dom->formatOutput = true;
102 2
        $dom->loadXML($xml->asXML());
103
104 2
        return $dom->saveXML(null, LIBXML_NOEMPTYTAG);
105
    }
106
107 5
    public function isOK()
108
    {
109 5
        return ($this->get('TransactionResult') === 'OK');
110
    }
111
112
    /**
113
     * Returns whether the requested index exists
114
     *
115
     * @param string $key
116
     *
117
     * @return bool
118
     */
119 1
    public function offsetExists($key)
120
    {
121 1
        return array_key_exists($key, $this->data);
122
    }
123
124
    /**
125
     * Returns the value at the specified index
126
     *
127
     * @param string $key The index with the value.
128
     *
129
     * @return mixed|null The value at the specified index or null.
130
     */
131 1
    public function offsetGet($key)
132
    {
133 1
        return $this->get($key);
134
    }
135
136
    /**
137
     * Sets the value at the specified index to $value
138
     *
139
     * @param string $key The index being set.
140
     * @param mixed $value The new value for the index
141
     */
142 1
    public function offsetSet($key, $value)
143
    {
144 1
        $this->set($key, $value);
145 1
    }
146
147
    /**
148
     * Unsets the value at the specified index
149
     *
150
     * @param string $key
151
     */
152 1
    public function offsetUnset($key)
153
    {
154 1
        unset($this->data[$key]);
155 1
    }
156
157
    /**
158
     * Magic getter, calls getXXX if exists.
159
     *
160
     * @param string $key
161
     *
162
     * @return mixed
163
     */
164 1 View Code Duplication
    public function __get($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
    {
166 1
        $getter = 'get' . $this->classify($key);
167 1
        if (method_exists($this, $getter)) {
168
            return call_user_func(array($this, $getter));
169
        }
170
171 1
        return $this->get($key);
172
    }
173
174
    /**
175
     * Magic setter, calls setXXX if exists.
176
     * @param $key
177
     * @param $value
178
     *
179
     * @return mixed
180
     */
181 1 View Code Duplication
    public function __set($key, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183 1
        $setter = 'set' . $this->classify($key);
184 1
        if (method_exists($this, $setter)) {
185
            return call_user_func_array(array($this, $setter), array($value));
186
        }
187 1
        $this->set($key, $value);
188 1
    }
189
190
    /**
191
     * Converts a string into a CamelCase word.
192
     *
193
     * @param string $string The string to classify.
194
     *
195
     * @return string The classified word.
196
     */
197 2
    private function classify($string)
198
    {
199 2
        return str_replace(' ', '', ucwords(strtr($string, '_-', ' ')));
200
    }
201
202
    /**
203
     * Returns whether the requested index exists
204
     *
205
     * @param string $key
206
     *
207
     * @return bool
208
     */
209 1
    public function __isset($key)
210
    {
211 1
        return isset($this->data[$key]);
212
    }
213
214
    /**
215
     * Unsets the value at the specified index
216
     *
217
     * @param string $key
218
     */
219 1
    public function __unset($key)
220
    {
221 1
        unset($this->data[$key]);
222 1
    }
223
}
224