Passed
Push — master ( c1d1cf...f48ccd )
by Francimar
03:58
created

CurlSoap::send()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 10.0285

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 23
cts 30
cp 0.7667
rs 4.909
c 0
b 0
f 0
cc 9
eloc 27
nc 48
nop 4
crap 10.0285
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 19
    public function __construct($base_url = null)
61
    {
62 19
        parent::__construct($base_url);
63 19
        $this->setHeader('Content-Type', 'application/soap+xml; charset=utf-8');
64 19
        $this->setOpt(CURLOPT_SSL_VERIFYPEER, false);
65 19
        $this->setOpt(CURLOPT_SSLVERSION, 1);
66 19
        $this->setConnectTimeout(4);
67 19
        $this->setTimeout(6);
68 19
        $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 19
        });
76 19
    }
77
78 28
    public static function setPostFunction($post_fn)
79
    {
80 28
        return self::$post_fn = $post_fn;
81
    }
82
83 17
    public function setCertificate($certificate)
84
    {
85 17
        $this->certificate = $certificate;
86 17
    }
87
88 18
    public function getCertificate()
89
    {
90 18
        return $this->certificate;
91
    }
92
93 17
    public function setPrivateKey($private_key)
94
    {
95 17
        $this->private_key = $private_key;
96 17
    }
97
98 18
    public function getPrivateKey()
99
    {
100 18
        return $this->private_key;
101
    }
102
103
    public function getResponse()
104
    {
105
        return $this->response;
106
    }
107
108
    public function getHeader()
109
    {
110
        return $this->response->getElementsByTagName('Header')->item(0);
111
    }
112
113
    public function getBody()
114
    {
115
        return $this->response->getElementsByTagName('Body')->item(0);
116
    }
117
118 18
    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...
119
    {
120 18
        $this->setOpt(CURLOPT_SSLCERT, $this->getCertificate());
121 18
        $this->setOpt(CURLOPT_SSLKEY, $this->getPrivateKey());
122 18
        if ($header instanceof \DOMDocument) {
123 17
            $header = $header->saveXML($header->documentElement);
124 17
        }
125 18
        if ($body instanceof \DOMDocument) {
126 18
            $body = $body->saveXML($body->documentElement);
127 18
        }
128 18
        $dom = new \DOMDocument();
129 18
        $dom->preserveWhiteSpace = false;
130 18
        $dom->loadXML(self::ENVELOPE);
131 18
        $envelope = $dom->saveXML();
132 18
        $data = str_replace('<soap12:Header/>', '<soap12:Header>'.$header.'</soap12:Header>', $envelope);
133 18
        $data = str_replace('<soap12:Body/>', '<soap12:Body>'.$body.'</soap12:Body>', $data);
134 18
        if (is_null(self::$post_fn)) {
135
            $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...
136
        } else {
137 18
            call_user_func_array(self::$post_fn, array($this, $url, $data));
138
        }
139 18
        if (!$this->error) {
140 16
            return $this->response;
141
        }
142 2
        if (!empty($this->rawResponse) && ($this->response instanceof \DOMDocument)) {
143
            $text = $this->response->getElementsByTagName('Text');
144
            if ($text->length == 1) {
145
                throw new \Exception($text->item(0)->nodeValue, $this->errorCode);
146
            }
147
        }
148 2
        $transfer = $this->getInfo(CURLINFO_PRETRANSFER_TIME);
149 2
        if ($transfer == 0) { // never started the transfer
150 2
            throw new \NFe\Exception\NetworkException($this->errorMessage, $this->errorCode);
151
        }
152
        throw new \NFe\Exception\IncompleteRequestException($this->errorMessage, $this->errorCode);
153
    }
154
}
155