XmlPost::reset()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
1
<?
2
3
namespace fangface\tools;
4
5
use fangface\tools\Xml;
6
use yii\base\Model;
7
8
class XmlPost extends Model {
9
10
    /**
11
     * @var Xml|false The request object
12
     */
13
    public $request = false;
14
15
    /**
16
     * @var string The URL to which the request should be sent
17
     */
18
    public $url = '';
19
20
    /**
21
     * @var integer The time before a request should be considered as failed
22
     */
23
    public $timeout = 60;
24
25
26
    /**
27
     * @var Xml|false The response object
28
     */
29
    public $response = false;
30
31
    /**
32
     * @var string The request document
33
     */
34
    public $remote_request = '';
35
36
    /**
37
     * @var mixed The response document
38
     */
39
    public $remote_response = '';
40
41
    /**
42
     * @var string The response status code
43
     */
44
    public $remote_status = '';
45
46
    /**
47
     * @var string The response status description
48
     */
49
    public $remote_reason = '';
50
51
    /**
52
     * @var boolean Should SSL certificate peer be verified
53
     */
54
    public $verify_peer = true;
55
56
    /**
57
     * @var boolean Should SSL certificate host be verified
58
     */
59
    public $verify_host = true;
60
61
62
    /**
63
     * Call by construct
64
     */
65
	public function init() {
66
        $this->reset();
67
	}
68
69
70
    /**
71
     * Reset response variables
72
     */
73
	public function reset() {
74
        if (!$this->response instanceof Xml) {
75
	       $this->response = New Xml();
76
        } else {
77
            $this->response->resetArray();
78
        }
79
		$this->remote_request = '';
80
		$this->remote_response = '';
81
		$this->remote_status = '';
82
		$this->remote_reason = '';
83
84
	}
85
86
87
	/**
88
	 * Send request to remote server and load up the response
89
	 *
90
	 * @param Xml $request
91
	 * @param string $url
92
	 * @param integer $timeout
93
	 * @return boolean Success
94
	 */
95
	public function process($request = false, $url = false, $timeout = false, $response = false) {
96
97
	    $result = false;
98
99
		if ($request instanceof Xml) {
100
		    $this->request = $request;
101
		}
102
103
		if ($url) {
104
		    $this->url = $url;
105
		}
106
107
		if ($timeout) {
108
		    $this->timeout = $timeout;
109
		}
110
111
		if ($response instanceof Xml) {
112
		    $this->response = $response;
113
		}
114
115
		$this->reset();
116
117
		if (!$this->request) {
118
            $this->remote_status = 'X502';
119
            $this->remote_reason = 'No request set';
120
		} elseif (!$this->request instanceof Xml) {
121
            $this->remote_status = 'X502';
122
            $this->remote_reason = 'Invalid request set';
123
		} elseif (!$this->url) {
124
		    $this->remote_status = 'X502';
125
		    $this->remote_reason = 'No URL set';
126
		} elseif (!$this->response instanceof Xml) {
127
            $this->remote_status = 'X502';
128
            $this->remote_reason = 'Invalid response set';
129
		} else {
130
131
            $POSTCh = curl_init();
132
133
            curl_setopt($POSTCh, CURLOPT_URL, $this->url);
134
    		curl_setopt($POSTCh, CURLOPT_POST, 1 );
135
136
    		$this->remote_request = '<' . '?xml version="1.0" encoding="UTF-8"' . '?' .'>' . "\n";
137
    		$this->remote_request .= $this->request->getDocument();
138
139
    		curl_setopt($POSTCh, CURLOPT_POSTFIELDS, $this->remote_request);
140
    		curl_setopt($POSTCh, CURLOPT_HTTPHEADER, array('Expect: ') );
141
    		curl_setopt($POSTCh, CURLOPT_RETURNTRANSFER, 1);
142
    		curl_setopt($POSTCh, CURLOPT_TIMEOUT, $this->timeout);
143
144
            if (!$this->verify_host) {
145
                curl_setopt($POSTCh, CURLOPT_SSL_VERIFYHOST, 0);
146
            }
147
148
            if (!$this->verify_peer) {
149
                curl_setopt($POSTCh, CURLOPT_SSL_VERIFYPEER, false);
150
            }
151
152
    		$POSTResponse = curl_exec($POSTCh);
153
    		$POSTErrNo = curl_errno($POSTCh);
154
    		$POSTErrStr = curl_error($POSTCh);
155
    		$POSTGetInfo = curl_getinfo($POSTCh);
156
    		curl_close($POSTCh);
157
158
    		$this->remote_response = $POSTResponse;
159
160
    		if ($POSTErrNo > 0) {
161
    			$this->remote_status = 'X502';
162
    			$this->remote_reason = $POSTErrNo . ' - ' . $POSTErrStr;
163
    		} elseif ($POSTGetInfo["http_code"] != 200) {
164
    			$this->remote_status = 'X' . $POSTGetInfo["http_code"];
165
    			$this->remote_reason = 'HTTP:'. $POSTGetInfo["http_code"];
166
    		} else {
167
    		    if ($this->response->getRootElement() == '' || strpos($POSTResponse,'<' . $this->response->getRootElement() . '>') !== false) {
168
    		        if ($this->response->readDocumentFromString($POSTResponse)) {
169
    				    $this->remote_status = 'P200';
170
    				    $this->remote_reason = 'HTTP:'. $POSTGetInfo["http_code"];
171
    					$result = true;
172
    				} else {
173
    					$this->remote_status = 'X502';
174
    					$this->remote_reason = 'Invalid response - failed to read document';
175
    				}
176
177
    			} else {
178
    				$this->remote_status = 'X502';
179
    				$this->remote_reason = 'Invalid response - document not as expected';
180
    			}
181
    		}
182
        }
183
184
        return $result;
185
	}
186
187
}
188