Completed
Push — master ( dc5f67...84520a )
by Francimar
11:49 queued 07:55
created

CurlSoap   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 61.83%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 0
loc 133
ccs 47
cts 76
cp 0.6183
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A setPostFunction() 0 4 1
A setCertificate() 0 4 1
A getCertificate() 0 4 1
A setPrivateKey() 0 4 1
A getPrivateKey() 0 4 1
A getResponse() 0 4 1
A getHeader() 0 4 1
A reparseResponse() 0 11 4
A getBody() 0 4 1
D send() 0 37 9
1
<?php
2
/**
3
 * MIT License
4
 *
5
 * Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA
6
 *
7
 * @author Francimar Alves <[email protected]>
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
namespace NFe\Common;
29
30
use Curl\Curl;
31
32
/**
33
 * Faz requisições SOAP 1.2
34
 */
35
class CurlSoap extends Curl
36
{
37
38
    const ENVELOPE = <<<XML
39
<?xml version="1.0" encoding="utf-8"?>
40
<soap12:Envelope 
41
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
42
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
43
    xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
44
    <soap12:Header/>
45
    <soap12:Body/>
46
</soap12:Envelope>
47
XML;
48
49
    private $certificate;
50
    private $private_key;
51
    private static $post_fn;
52
53
    /**
54
     * Construct
55
     *
56
     * @access public
57
     * @param  $base_url
58
     * @throws \ErrorException
59
     */
60 21
    public function __construct($base_url = null)
61
    {
62 21
        parent::__construct($base_url);
63 21
        $this->setHeader('Content-Type', 'application/soap+xml; charset=utf-8');
64 21
        $this->setOpt(CURLOPT_SSL_VERIFYPEER, false);
65 21
        $this->setOpt(CURLOPT_SSLVERSION, 1);
66 21
        $this->setConnectTimeout(4);
67 21
        $this->setTimeout(6);
68 21
        $this->setXmlDecoder(function ($response) {
69
            $dom = new \DOMDocument();
70
            $xml_obj = $dom->loadXML($response);
71
            if (!($xml_obj === false)) {
72
                $response = $dom;
73
            }
74
            return $response;
75 21
        });
76 21
    }
77
78
    private function reparseResponse()
79
    {
80
        if (isset($this->responseHeaders['Content-Type'])) {
81
            $xmlPattern = '~^application/soap\+xml~i';
0 ignored issues
show
Coding Style introduced by
$xmlPattern does not seem to conform to the naming convention (^[a-z_][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
82
            if (preg_match($xmlPattern, $this->responseHeaders['Content-Type'])) {
0 ignored issues
show
Coding Style introduced by
$xmlPattern does not seem to conform to the naming convention (^[a-z_][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
83
                if ($this->xmlDecoder) {
84
                    $this->response = call_user_func($this->xmlDecoder, $this->rawResponse);
85
                }
86
            }
87
        }
88
    }
89
90 30
    public static function setPostFunction($post_fn)
91
    {
92 30
        return self::$post_fn = $post_fn;
93
    }
94
95 19
    public function setCertificate($certificate)
96
    {
97 19
        $this->certificate = $certificate;
98 19
    }
99
100 20
    public function getCertificate()
101
    {
102 20
        return $this->certificate;
103
    }
104
105 19
    public function setPrivateKey($private_key)
106
    {
107 19
        $this->private_key = $private_key;
108 19
    }
109
110 20
    public function getPrivateKey()
111
    {
112 20
        return $this->private_key;
113
    }
114
115
    public function getResponse()
116
    {
117
        return $this->response;
118
    }
119
120
    public function getHeader()
121
    {
122
        return $this->response->getElementsByTagName('Header')->item(0);
123
    }
124
125 1
    public function getBody()
126
    {
127
        return $this->response->getElementsByTagName('Body')->item(0);
128 1
    }
129
130 20
    public function send($url, $body, $header = '', $action = null)
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132 20
        $this->setOpt(CURLOPT_SSLCERT, $this->getCertificate());
133 20
        $this->setOpt(CURLOPT_SSLKEY, $this->getPrivateKey());
134 20
        if ($header instanceof \DOMDocument) {
135 19
            $header = $header->saveXML($header->documentElement);
136 19
        }
137 20
        if ($body instanceof \DOMDocument) {
138 20
            $body = $body->saveXML($body->documentElement);
139 20
        }
140 20
        $dom = new \DOMDocument();
141 20
        $dom->preserveWhiteSpace = false;
142 20
        $dom->loadXML(self::ENVELOPE);
143 20
        $envelope = $dom->saveXML();
144 20
        $data = str_replace('<soap12:Header/>', '<soap12:Header>'.$header.'</soap12:Header>', $envelope);
145 20
        $data = str_replace('<soap12:Body/>', '<soap12:Body>'.$body.'</soap12:Body>', $data);
146 20
        if (is_null(self::$post_fn)) {
147
            $this->post($url, $data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
            $this->reparseResponse();
149
        } else {
150 20
            call_user_func_array(self::$post_fn, array($this, $url, $data));
151
        }
152 20
        if (!$this->error) {
153 18
            return $this->response;
154
        }
155 2
        if (!empty($this->rawResponse) && ($this->response instanceof \DOMDocument)) {
156
            $text = $this->response->getElementsByTagName('Text');
157
            if ($text->length == 1) {
158
                throw new \Exception($text->item(0)->nodeValue, $this->errorCode);
159
            }
160
        }
161 2
        $transfer = $this->getInfo(CURLINFO_PRETRANSFER_TIME);
162 2
        if ($transfer == 0) { // never started the transfer
163 2
            throw new \NFe\Exception\NetworkException($this->errorMessage, $this->errorCode);
164
        }
165
        throw new \NFe\Exception\IncompleteRequestException($this->errorMessage, $this->errorCode);
166
    }
167
}
168