Passed
Push — master ( 90eaf0...c1d1cf )
by Francimar
12:38
created

CurlSoap::send()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 0
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 24
nc 24
nop 4
crap 72
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
52
    /**
53
     * Construct
54
     *
55
     * @access public
56
     * @param  $base_url
57
     * @throws \ErrorException
58
     */
59 1
    public function __construct($base_url = null)
60
    {
61 1
        parent::__construct($base_url);
62 1
        $this->setHeader('Content-Type', 'application/soap+xml; charset=utf-8');
63 1
        $this->setOpt(CURLOPT_SSL_VERIFYPEER, false);
64 1
        $this->setOpt(CURLOPT_SSLVERSION, 1);
65 1
        $this->setConnectTimeout(4);
66 1
        $this->setTimeout(6);
67 1
        $this->setXmlDecoder(function ($response) {
68
            $dom = new \DOMDocument();
69
            $xml_obj = $dom->loadXML($response);
70
            if (!($xml_obj === false)) {
71
                $response = $dom;
72
            }
73
            return $response;
74 1
        });
75 1
    }
76
77
    public function setCertificate($certificate)
78
    {
79
        $this->certificate = $certificate;
80
    }
81
82
    public function getCertificate()
83
    {
84
        return $this->certificate;
85
    }
86
87
    public function setPrivateKey($private_key)
88
    {
89
        $this->private_key = $private_key;
90
    }
91
92
    public function getPrivateKey()
93
    {
94
        return $this->private_key;
95
    }
96
97
    public function getResponse()
98
    {
99
        return $this->response;
100
    }
101
102
    public function getHeader()
103
    {
104
        return $this->response->getElementsByTagName('Header')->item(0);
105
    }
106
107
    public function getBody()
108
    {
109
        return $this->response->getElementsByTagName('Body')->item(0);
110
    }
111
112
    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...
113
    {
114
        $this->setOpt(CURLOPT_SSLCERT, $this->getCertificate());
115
        $this->setOpt(CURLOPT_SSLKEY, $this->getPrivateKey());
116
        if ($header instanceof \DOMDocument) {
117
            $header = $header->saveXML($header->documentElement);
118
        }
119
        if ($body instanceof \DOMDocument) {
120
            $body = $body->saveXML($body->documentElement);
121
        }
122
        $dom = new \DOMDocument();
123
        $dom->preserveWhiteSpace = false;
124
        $dom->loadXML(self::ENVELOPE);
125
        $envelope = $dom->saveXML();
126
        $data = str_replace('<soap12:Header/>', '<soap12:Header>'.$header.'</soap12:Header>', $envelope);
127
        $data = str_replace('<soap12:Body/>', '<soap12:Body>'.$body.'</soap12:Body>', $data);
128
        $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...
129
        if (!$this->error) {
130
            return $this->response;
131
        }
132
        if (!empty($this->rawResponse) && ($this->response instanceof \DOMDocument)) {
133
            $text = $this->response->getElementsByTagName('Text');
134
            if ($text->length == 1) {
135
                throw new \Exception($text->item(0)->nodeValue, $this->errorCode);
136
            }
137
        }
138
        $transfer = $this->getInfo(CURLINFO_PRETRANSFER_TIME);
139
        if ($transfer == 0) { // never started the transfer
140
            throw new \NFe\Exception\NetworkException($this->errorMessage, $this->errorCode);
141
        }
142
        throw new \NFe\Exception\IncompleteRequestException($this->errorMessage, $this->errorCode);
143
    }
144
}
145