Payone_Api_Adapter_Http_Socket   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 80
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C doRequest() 0 76 10
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
class Payone_Api_Adapter_Http_Socket extends Payone_Api_Adapter_Http_Abstract
34
{
35
    protected function doRequest()
36
    {
37
        $response = array();
38
        $errno = '';
39
        $errstr = '';
40
        $urlArray = $this->generateUrlArray();
41
42
        $urlHost = $urlArray['host'];
43
        $urlPath = isset($urlArray['path']) ? $urlArray['path'] : '';
44
        $urlScheme = $urlArray['scheme'];
45
        $urlQuery = $urlArray['query'];
46
47
        $socketScheme = '';
48
        $socketPort = 80;
49
50
        if ($urlScheme == 'https') {
51
            $socketScheme = 'ssl://';
52
            $socketPort = 443;
53
        }
54
55
        // Request - Method
56
        $method = 'Post';
57
58
        // Request - Header
59
        $headers = array();
60
        $headers[] = "POST " . $urlPath . " HTTP/1.1\r\n";
61
        $headers[] = "Host: " . $urlHost . "\r\n";
62
        $headers[] = "Content-Type: application/x-www-form-urlencoded\r\n";
63
        $headers[] = "Content-Length: " . strlen($urlQuery) . "\r\n";
64
        $headers[] = "Connection: close\r\n\r\n";
65
66
        // Request - Body
67
        $body = $urlArray['query'];
68
69
        // Request - Build
70
        $request = "{$method} {$urlPath} HTTP/1.1\r\n";
71
        foreach ($headers as $k => $v) {
72
            $request .= "$v\r\n";
73
        }
74
75
        $request .= "\r\n" . $body;
76
77
        // Socket - Connect
78
        $flags = STREAM_CLIENT_CONNECT;
79
        $context = stream_context_create();
80
        $socket = @stream_socket_client(
81
            $socketScheme . $urlHost . ':' . $socketPort,
82
            $errno,
83
            $errstr,
84
            self::DEFAULT_TIMEOUT,
85
            $flags,
86
            $context
87
        );
88
89
        // Socket - Write
90
        if (!@fwrite($socket, $request)) {
91
            throw new Payone_Api_Exception_WritingRequestToServer();
92
        }
93
94
        $gotStatus = false;
95
        while (($line = @fgets($socket)) !== false) {
96
            $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
97
            if ($gotStatus) {
98
                $response[] = $line;
99
                if (rtrim($line) === '') {
100
                    break;
101
                }
102
            }
103
        }
104
105
        if (count($response) == 0) {
106
            throw new Payone_Api_Exception_InvalidResponse();
107
        }
108
109
        return $response;
110
    }
111
112
}
113