Completed
Push — master ( 657195...b00c1a )
by Francimar
07:03
created

CurlSoap   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 79.17%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 98
ccs 38
cts 48
cp 0.7917
rs 10
c 0
b 0
f 0

7 Methods

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