Test Failed
Push — master ( 0b1763...830d7c )
by Francimar
19:46
created

CurlSoap   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 79.59%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 99
ccs 39
cts 49
cp 0.7959
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 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
B send() 0 32 8
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:Body/>
45
</soap12:Envelope>
46
XML;
47
48
    private $certificate;
49
    private $private_key;
50
    private static $post_fn;
51
52
    /**
53
     * Construct
54
     *
55
     * @access public
56
     * @param  $base_url
57
     * @throws \ErrorException
58
     */
59 25
    public function __construct($base_url = null)
60
    {
61 25
        parent::__construct($base_url);
62 25
        $this->setOpt(CURLOPT_CAINFO, dirname(dirname(dirname(__DIR__))) . '/docs/cacert/cacert.pem');
63 25
        $this->setHeader('Content-Type', 'application/soap+xml; charset=utf-8');
64 25
        $this->setConnectTimeout(4);
65 25
        $this->setTimeout(6);
66 25
        $this->setXmlDecoder(function ($response) {
67
            $dom = new \DOMDocument();
68
            $xml_obj = $dom->loadXML($response);
69
            if (!($xml_obj === false)) {
70
                $response = $dom;
71
            }
72
            return $response;
73 25
        });
74 25
    }
75
76 34
    public static function setPostFunction($post_fn)
77
    {
78 34
        return self::$post_fn = $post_fn;
79
    }
80
81 23
    public function setCertificate($certificate)
82
    {
83 23
        $this->certificate = $certificate;
84 23
    }
85
86 24
    public function getCertificate()
87
    {
88 24
        return $this->certificate;
89
    }
90
91 23
    public function setPrivateKey($private_key)
92
    {
93 23
        $this->private_key = $private_key;
94 23
    }
95
96 24
    public function getPrivateKey()
97
    {
98 24
        return $this->private_key;
99
    }
100
101 24
    public function send($url, $body)
102
    {
103 24
        $this->setOpt(CURLOPT_SSLCERT, $this->getCertificate());
104 24
        $this->setOpt(CURLOPT_SSLKEY, $this->getPrivateKey());
105 24
        if ($body instanceof \DOMDocument) {
106 24
            $body = $body->saveXML($body->documentElement);
107
        }
108 24
        $dom = new \DOMDocument();
109 24
        $dom->preserveWhiteSpace = false;
110 24
        $dom->loadXML(self::ENVELOPE);
111 24
        $envelope = $dom->saveXML();
112 24
        $data = str_replace('<soap12:Body/>', '<soap12:Body>'.$body.'</soap12:Body>', $envelope);
113 24
        if (is_null(self::$post_fn)) {
114
            $this->post($url, $data);
115
        } else {
116 24
            call_user_func_array(self::$post_fn, [$this, $url, $data]);
117
        }
118 23
        if (!$this->error) {
119 21
            return $this->response;
120
        }
121 2
        if (!empty($this->rawResponse) && ($this->response instanceof \DOMDocument)) {
122
            $text = $this->response->getElementsByTagName('Text');
123
            if ($text->length == 1) {
124
                throw new \Exception($text->item(0)->nodeValue, $this->errorCode);
125
            }
126
        }
127 2
        $transfer = $this->getInfo(CURLINFO_PRETRANSFER_TIME);
128 2
        if ($transfer == 0) { // never started the transfer
129 2
            throw new \NFe\Exception\NetworkException($this->errorMessage, $this->errorCode);
130
        }
131
        throw new \NFe\Exception\IncompleteRequestException($this->errorMessage, $this->errorCode);
132
    }
133
}
134