generateUrlArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * NOTICE OF LICENSE
5
 *
6
 * This source file is subject to the GNU General Public License (GPL 3)
7
 * that is bundled with this package in the file LICENSE.txt
8
 *
9
 * DISCLAIMER
10
 *
11
 * Do not edit or add to this file if you wish to upgrade Payone to newer
12
 * versions in the future. If you wish to customize Payone for your
13
 * needs please refer to http://www.payone.de for more information.
14
 *
15
 * @category        Payone
16
 * @package         Payone_Api
17
 * @subpackage      Adapter
18
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
19
 * @author          Matthias Walter <[email protected]>
20
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
21
 * @link            http://www.noovias.com
22
 */
23
24
/**
25
 *
26
 * @category        Payone
27
 * @package         Payone_Api
28
 * @subpackage      Adapter
29
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
30
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
31
 * @link            http://www.noovias.com
32
 */
33
abstract class Payone_Api_Adapter_Http_Abstract
34
    implements Payone_Api_Adapter_Interface
35
{
36
    /**
37
     * @todo refactor to use some kind of config
38
     * @todo use timeout from config and fallback to default
39
     */
40
    const DEFAULT_TIMEOUT   = 45;
41
42
    const SDK_VERSION_KEY   = 'sdk_version';
43
    const SDK_TYPE_KEY      = 'sdk_type';
44
    const SDK_VERSION       = '2.0.0';
45
    const SDK_TYPE          = 'php';
46
47
    /**
48
     * @var string
49
     */
50
    protected $url = '';
51
    /**
52
     * @var array
53
     */
54
    protected $params = array();
55
56
    /** @var string */
57
    protected $rawResponse = '';
58
59
    /**
60
     * @param array $params
61
     * @throws Payone_Api_Exception_InvalidParameters
62
     * @throws Payone_Api_Exception_InvalidUrl
63
     * @throws Payone_Api_Exception_InvalidResponse
64
     * @return array
65
     */
66
    public function request(array $params)
67
    {
68
        $this->setParams($params);
69
70
        $this->validate();
71
72
        $this->params[self::SDK_TYPE_KEY]    = self::SDK_TYPE;
73
        $this->params[self::SDK_VERSION_KEY] = self::SDK_VERSION;
74
75
        $responseRaw = $this->doRequest();
76
        
77
        $result = $this->parseResponse($responseRaw);
78
79
        return $result;
80
    }
81
82
    /**
83
     * @return array
84
     * @throws Payone_Api_Exception_InvalidResponse
85
     */
86
    abstract protected function doRequest();
87
88
    /**
89
     * @throws Payone_Api_Exception_InvalidParameters
90
     * @throws Payone_Api_Exception_InvalidUrl
91
     */
92
    protected function validate()
93
    {
94
        if ($this->getUrl() == '') {
95
            throw new Payone_Api_Exception_InvalidUrl();
96
        }
97
98
        if (count($this->getParams()) <= 0) {
99
            throw new Payone_Api_Exception_InvalidParameters();
100
        }
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    protected function generateUrlArray()
107
    {
108
        $urlRequest = $this->getUrl() . '?' . http_build_query($this->getParams(), null, '&');
109
        $urlArray = parse_url($urlRequest);
110
        return $urlArray;
111
    }
112
113
    /**
114
     * @param array $responseRaw
115
     * @return array
116
     */
117
    protected function parseResponse(array $responseRaw = array())
118
    {
119
        $result = array();
120
121
        if (count($responseRaw) == 0) {
122
            return $result;
123
        }
124
125
        foreach ($responseRaw as $key => $line) {
126
            $pos = strpos($line, "=");
127
128
            if ($pos === false) {
129
                if (strlen($line) > 0) {
130
                    $result[$key] = $line;
131
                }
132
133
                continue;
134
            }
135
136
            $lineArray = explode('=', $line);
137
            $resultKey = array_shift($lineArray);
138
            $result[$resultKey] = implode('=', $lineArray);
139
        }
140
141
        return $result;
142
    }
143
144
    /**
145
     * @param string $url
146
     */
147
    public function setUrl($url)
148
    {
149
        $this->url = $url;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getUrl()
156
    {
157
        return $this->url;
158
    }
159
160
    /**
161
     * @param array $params
162
     */
163
    public function setParams(array $params)
164
    {
165
        $this->params = $params;
166
    }
167
168
    /**
169
     * @return array
170
     */
171
    public function getParams()
172
    {
173
        return $this->params;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getRawResponse()
180
    {
181
        return $this->rawResponse;
182
    }
183
184
    /**
185
     * @param string $rawResponse
186
     */
187
    protected function setRawResponse($rawResponse)
188
    {
189
        $this->rawResponse = $rawResponse;
190
    }
191
192
}
193