Passed
Push — master ( 28effa...a0de38 )
by Giancarlos
04:32
created

BaseSunat::setUrlWsdl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 15/07/2017
6
 * Time: 23:13
7
 */
8
9
namespace Greenter\Ws\Services;
10
11
use Greenter\Model\Response\Error;
12
use Greenter\Ws\Header\WSSESecurityHeader;
13
use Greenter\Ws\Reader\DomCdrReader;
14
use Greenter\Ws\Reader\XmlErrorReader;
15
use Greenter\Zip\ZipFactory;
16
use SoapClient;
17
18
/**
19
 * Class BaseSunat
20
 * @package Greenter\Ws\Services
21
 */
22
class BaseSunat
23
{
24
    /**
25
     * Url del servicio.
26
     * @var string
27
     */
28
    private $service;
29
30
    /**
31
     * Usuario (RUC + User SOL).
32
     * @var string
33
     */
34
    private $user;
35
    /**
36
     * Clave SOL
37
     *
38
     * @var string
39
     */
40
    private $password;
41
42
    /**
43
     * Parametros del Soap.
44
     * @var array
45
     */
46
    protected $parameters = [];
47
48
    /**
49
     * Url del WSDL.
50
     * @var string
51
     */
52
    protected $urlWsdl;
53
54
    /**
55
     * BaseSunat constructor.
56
     */
57 50
    public function __construct()
58
    {
59 50
        $this->urlWsdl = __DIR__.'/../../Resources/wsdl/billService.wsdl';
60 50
    }
61
62
    /**
63
     * Set Credentiasl WebService.
64
     *
65
     * @param string $user
66
     * @param string $password
67
     */
68 50
    public function setCredentials($user, $password)
69
    {
70 50
        $this->user = $user;
71 50
        $this->password = $password;
72 50
    }
73
74
    /**
75
     * Create a new SoapClient.
76
     * @return SoapClient
77
     */
78 32
    public function getClient()
79
    {
80 32
        $client = new SoapClient($this->urlWsdl, $this->parameters);
81 32
        $client->__setLocation($this->service);
82 32
        $client->__setSoapHeaders(new WSSESecurityHeader($this->user, $this->password));
83
84 32
        return $client;
85
    }
86
87
    /**
88
     * Set Service of SUNAT.
89
     *
90
     * @param string $service
91
     * @return BaseSunat
92
     */
93 50
    public function setService($service)
94
    {
95 50
        $this->service = $service;
96 50
        return $this;
97
    }
98
99
    /**
100
     * Set Url del WSDL.
101
     *
102
     * @param string $urlWsdl
103
     * @return BaseSunat
104
     */
105 2
    public function setUrlWsdl($urlWsdl)
106
    {
107 2
        $this->urlWsdl = $urlWsdl;
108 2
        return $this;
109
    }
110
111
    /**
112
     * Get error from Fault Exception.
113
     *
114
     * @param \SoapFault $fault
115
     * @return Error
116
     */
117 20
    protected function getErrorFromFault(\SoapFault $fault)
118
    {
119 20
        $err = new Error();
120 20
        $fcode = $fault->faultcode;
0 ignored issues
show
Bug introduced by
The property faultcode does not seem to exist in SoapFault.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
121 20
        $code = preg_replace('/[^0-9]+/', '', $fcode);
122 20
        $msg = '';
123
124 20
        if ($code) {
125 12
            $msg = $this->getMessageError($code);
126 12
            $fcode = $code;
127 12
        } else {
128 8
            $code = preg_replace('/[^0-9]+/', '', $fault->faultstring);
0 ignored issues
show
Bug introduced by
The property faultstring does not seem to exist. Did you mean string?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
129
130 8
            if ($code) {
131 8
                $msg = $this->getMessageError($code);
132 8
                $fcode = $code;
133 8
            }
134
        }
135
136 20
        if (!$msg) {
137
            $msg = isset($fault->detail) ? $fault->detail->message : $fault->faultstring;
0 ignored issues
show
Bug introduced by
The property detail does not seem to exist in SoapFault.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property faultstring does not seem to exist. Did you mean string?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
138
        }
139
140 20
        $err->setCode($fcode);
141 20
        $err->setMessage($msg);
142
143 20
        return $err;
144
    }
145
146 10
    protected function extractResponse($zipContent)
147
    {
148 10
        $zip = new ZipFactory();
149 10
        $xml = $zip->decompressLastFile($zipContent);
150 10
        $reader = new DomCdrReader();
151
152 10
        return $reader->getCdrResponse($xml);
153
    }
154
155 20
    private function getMessageError($code)
156
    {
157 20
        $search = new XmlErrorReader();
158 20
        $msg = $search->getMessageByCode(intval($code));
159
160 20
        return $msg;
161
    }
162
}