NfgWsHelper::isVoterRegistrationValid()   B
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 10
nop 2
dl 0
loc 34
ccs 0
cts 25
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace PROCERGS\LoginCidadao\CoreBundle\Helper;
4
5
use LoginCidadao\CoreBundle\Entity\Person;
6
use PROCERGS\LoginCidadao\CoreBundle\Exception\MissingNfgAccessTokenException;
7
8
/* sample http to test raw send
9
10
  Host: pro-pae-4729.procergs.reders
11
  User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
12
  Connection: keep-alive
13
  Content-Type: text/xml; charset=utf-8
14
  SOAPAction: https://m-nfg.sefaz.rs.gov.br/LoginCidadaoWs/LoginCidadaoService/ConsultaCadastro
15
16
  <?xml version="1.0" encoding="UTF-8"?>
17
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
18
  <s:Body>
19
  <ConsultaCadastro xmlns="https://m-nfg.sefaz.rs.gov.br/LoginCidadaoWs/">
20
  <cpf>02284135080</cpf>
21
  <tituloEleitor>053619320434</tituloEleitor>
22
  <nome>ivo</nome>
23
  <dataNascimento>11/11/2014</dataNascimento>
24
  <organizacao>seplag</organizacao>
25
  <usuario>500</usuario>
26
  <senha>senha1</senha>
27
  </ConsultaCadastro>
28
  </s:Body>
29
  </s:Envelope>
30
 */
31
32
class NfgWsHelper
33
{
34
    protected $url;
35
    protected $client;
36
    protected $accessToken;
37
    protected $tituloEleitoral;
38
    protected $organizacao;
39
    protected $usuario;
40
    protected $senha;
41
    protected $error;
42
43
    /** @var MeuRSHelper */
44
    protected $meuRSHelper;
45
46
    public function setUrl($var)
47
    {
48
        $this->url = $var;
49
        return $this;
50
    }
51
52
    public function setAccessToken($var)
53
    {
54
        $this->accessToken = $var;
55
        return $this;
56
    }
57
58
    public function setTituloEleitoral($var)
59
    {
60
        $this->tituloEleitoral = $var;
61
        return $this;
62
    }
63
64
    public function setOrganizacao($var)
65
    {
66
        $this->organizacao = $var;
67
        return $this;
68
    }
69
70
    public function setUsuario($var)
71
    {
72
        $this->usuario = $var;
73
        return $this;
74
    }
75
76
    public function setSenha($var)
77
    {
78
        $this->senha = $var;
79
        return $this;
80
    }
81
82
    public function getError()
83
    {
84
        return $this->error;
85
    }
86
87
    public function consultaCadastro()
88
    {
89
        try {
90
            if (!$this->client) {
91
                $this->client = new \SoapClient($this->url,
92
                    array(
93
                    'cache_wsdl' => WSDL_CACHE_NONE,
94
                    'trace' => true,
95
                        'stream_context' => stream_context_create(
96
                            [
97
                                'ssl' => [
98
                                    // disable SSL/TLS security checks
99
                                    'verify_peer' => false,
100
                                    'verify_peer_name' => false,
101
                                    'allow_self_signed' => true,
102
                                ],
103
                            ]
104
                        )
105
                ));
106
            }
107
            $parm = array();
108
            if ($this->accessToken) {
109
                $parm['accessToken'] = $this->accessToken;
110
            }
111
            if ($this->tituloEleitoral) {
112
                $parm['tituloEleitoral'] = $this->tituloEleitoral;
113
            }
114
            $parm += array(
115
                'organizacao' => $this->organizacao ? $this->organizacao : '',
116
                'usuario' => $this->usuario ? $this->usuario : '',
117
                'senha' => $this->senha ? $this->senha : ''
118
            );
119
            $result = $this->client->ConsultaCadastro($parm);
120
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type PROCERGS\LoginCidadao\CoreBundle\Helper\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
121
            $this->error = $e;
122
            return false;
123
        }
124
        $dom = new \DOMDocument();
125
        if (!@$dom->loadXML($a   = $result->ConsultaCadastroResult)) {
126
            return false;
127
        }
128
        foreach (array('CodSitRetorno', 'MsgRetorno', 'CodNivelAcesso') as $val) {
129
            $node = $dom->getElementsByTagName($val);
130
            if (!$node->length) {
131
                return false;
132
            }
133
            $retorno[$val] = $node->item(0)->nodeValue;
134
        }
135
        if ($retorno['CodSitRetorno'] == 1 && $retorno['CodNivelAcesso'] > 1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $retorno seems to be defined by a foreach iteration on line 128. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
136
            foreach (array('NomeConsumidor', 'DtNasc', 'EmailPrinc', 'NroFoneContato',
137
            'CodSitTitulo', 'CodCpf') as $val) {
138
                $node = $dom->getElementsByTagName($val);
139
                if (!$node->length || !strlen($node->item(0)->nodeValue)) {
140
                    continue;
141
                }
142
                if ($val === 'CodCpf') {
143
                    $retorno[$val] = str_pad($node->item(0)->nodeValue, 11, "0",
144
                        STR_PAD_LEFT);
145
                } else {
146
                    $retorno[$val] = $node->item(0)->nodeValue;
147
                }
148
            }
149
        }
150
        return $retorno;
151
    }
152
    /*
153
      POST /LoginCidadaoWS/service.svc HTTP/1.1
154
      Host: m-nfg-des.procergs.reders
155
      Connection: Keep-Alive
156
      User-Agent: PHP-SOAP/5.3.3
157
      Content-Type: text/xml; charset=utf-8
158
      SOAPAction: "https://m-nfg.sefaz.rs.gov.br/LoginCidadaoWs/LoginCidadaoService/ObterAccessID"
159
      Content-Length: 371
160
161
      <?xml version="1.0" encoding="UTF-8"?>
162
      <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://m-nfg.sefaz.rs.gov.br/LoginCidadaoWs/">
163
      <SOAP-ENV:Body>
164
      <ns1:ObterAccessID>
165
      <ns1:organizacao>PROCERGS</ns1:organizacao><
166
      ns1:usuario>4085</ns1:usuario>
167
      <ns1:senha>A93SUDES</ns1:senha>
168
      </ns1:ObterAccessID>
169
      </SOAP-ENV:Body>
170
      </SOAP-ENV:Envelope>
171
     */
172
173
    public function obterAccessID()
174
    {
175
        try {
176
            if (!$this->client) {
177
                $this->client = new \SoapClient($this->url,
178
                    array(
179
                    'cache_wsdl' => WSDL_CACHE_NONE,
180
                    'trace' => true,
181
                        'stream_context' => stream_context_create(
182
                            [
183
                                'ssl' => [
184
                                    // disable SSL/TLS security checks
185
                                    'verify_peer' => false,
186
                                    'verify_peer_name' => false,
187
                                    'allow_self_signed' => true,
188
                                ],
189
                            ]
190
                        )
191
                ));
192
            }
193
            $result = $this->client->ObterAccessID(array(
194
                'organizacao' => $this->organizacao ? $this->organizacao : '',
195
                'usuario' => $this->usuario ? $this->usuario : '',
196
                'senha' => $this->senha ? $this->senha : ''
197
            ));
198
        } catch (Exception $e) {
199
            $this->error = $e;
200
            return false;
201
        }
202
        if (strpos($result->ObterAccessIDResult, ' ')) {
203
            throw new \Exception($result->ObterAccessIDResult);
204
        }
205
        return $result->ObterAccessIDResult;
206
    }
207
208
    public function isVoterRegistrationValid(Person $person,
209
                                             $voterRegistration = null)
210
    {
211
        if (is_null($voterRegistration)) {
212
            $voterRegistration = $this->meuRSHelper->getVoterRegistration($person);
213
        }
214
215
        if ($this->meuRSHelper->getNfgAccessToken()) {
0 ignored issues
show
Bug introduced by
The call to PROCERGS\LoginCidadao\Co...er::getNfgAccessToken() has too few arguments starting with person. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

215
        if ($this->meuRSHelper->/** @scrutinizer ignore-call */ getNfgAccessToken()) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
216
            $this->setAccessToken($this->meuRSHelper->getNfgAccessToken($person));
217
            $this->setTituloEleitoral($voterRegistration);
218
            $nfgData = $this->consultaCadastro();
219
220
            // TODO: this shuldn't be here! It's NOT this method's job to validate
221
            // if the WebService's request was successful!
222
            if ($nfgData['CodSitRetorno'] != 1) {
223
                throw new NfgException($nfgData['MsgRetorno']);
0 ignored issues
show
Bug introduced by
The type PROCERGS\LoginCidadao\Co...dle\Helper\NfgException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
224
            }
225
226
            // TODO: this shuldn't be here! It's NOT this method's job to validate
227
            // if the WebService's request was successful!
228
            if (!isset($nfgData['CodCpf'], $nfgData['NomeConsumidor'],
229
                    $nfgData['EmailPrinc'])) {
230
                throw new NfgException('nfg.missing.required.fields');
231
            }
232
233
            if (array_key_exists('CodSitTitulo', $nfgData) && $nfgData['CodSitTitulo']
234
                == 1) {
235
                return true;
236
            }
237
        } else {
238
            throw new MissingNfgAccessTokenException();
239
        }
240
241
        return false;
242
    }
243
244
    public function setMeuRSHelper(MeuRSHelper $meuRSHelper)
245
    {
246
        $this->meuRSHelper = $meuRSHelper;
247
248
        return $this;
249
    }
250
}
251