Test Setup Failed
Push — master ( aa40a2...69297c )
by Eduardo
06:24
created

Sigep::consultaSRO()   B

Complexity

Conditions 7
Paths 40

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 22
c 3
b 0
f 0
nc 40
nop 1
dl 0
loc 33
ccs 0
cts 0
cp 0
crap 56
rs 8.6346
1
<?php
2
namespace Eduardokum\CorreiosPhp;
3
4
use Eduardokum\CorreiosPhp\Contracts\Config\Config as ConfigContract;
5
use Eduardokum\CorreiosPhp\Entities\PostalObject;
6
use Eduardokum\CorreiosPhp\Exception\InvalidArgumentException;
7
use Eduardokum\CorreiosPhp\Entities\MailingList;
8
9
class Sigep extends Correios
10
{
11
    public function __construct(ConfigContract $config = null, $type = 'curl')
12
    {
13
        parent::__construct($config, $type);
14
        $this->setWs($this->getWs('sigep'));
15
16
        if ($this->getConfig()->getEnvironment() == 'testing') {
17
            $this->getConfig()->setUser('sigep');
18
            $this->getConfig()->setPassword('n5f9t8');
19
            $this->getConfig()->setUser('sigep');
20
            $this->getConfig()->setPassword('n5f9t8');
21
        }
22
    }
23
24
    /**
25
     * @param $service
26
     * @param $cepFrom
27
     * @param $cepTo
28
     *
29
     * @return boolean
30
     */
31
    public function statusServico($service, $cepFrom, $cepTo)
32
    {
33
        $request = '<cli:verificaDisponibilidadeServico>';
34
        $request .= sprintf('<codAdministrativo>%s</codAdministrativo>', $this->getConfig()->getAdministrativeCode());
35
        $request .= sprintf('<numeroServico>%s</numeroServico>', $service);
36
        $request .= sprintf('<cepOrigem>%08s</cepOrigem>', preg_replace('/[^0-9]/', '', $cepFrom));
37
        $request .= sprintf('<cepDestino>%08s</cepDestino>', preg_replace('/[^0-9]/', '', $cepTo));
38
        $request .= sprintf('<usuario>%s</usuario>', $this->getConfig()->getUser());
39
        $request .= sprintf('<senha>%s</senha>', $this->getConfig()->getPassword());
40
        $request .= '</cli:verificaDisponibilidadeServico>';
41
        $namespaces = [
42
            'xmlns:cli' => 'http://cliente.bean.master.sigep.bsb.correios.com.br/',
43
        ];
44
        $actions = [
45
            'curl' => null,
46
            'native' => 'verificaDisponibilidadeServico',
47
        ];
48
49
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
50
        return $result->return;
51
    }
52
53
    /**
54
     * @return \stdClass
55
     */
56
    public function buscaCliente()
57
    {
58
        $request = '<cli:buscaCliente>';
59
        $request .= sprintf('<idContrato>%s</idContrato>', $this->getConfig()->getContract());
60
        $request .= sprintf('<idCartaoPostagem>%s</idCartaoPostagem>', $this->getConfig()->getPostCard());
61
        $request .= sprintf('<usuario>%s</usuario>', $this->getConfig()->getUser());
62
        $request .= sprintf('<senha>%s</senha>', $this->getConfig()->getPassword());
63
        $request .= '</cli:buscaCliente>';
64
        $namespaces = [
65
            'xmlns:cli' => 'http://cliente.bean.master.sigep.bsb.correios.com.br/',
66
        ];
67
        $actions = [
68
            'curl' => null,
69
            'native' => 'buscaCliente',
70
        ];
71
72
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
73
        return $result->return;
74
    }
75
76
    /**
77
     * @param $cep
78
     *
79
     * @return \stdClass
80
     */
81
    public function consultaCEP($cep)
82
    {
83
        $request = '<cli:consultaCEP>';
84
        $request .= sprintf('<cep>%08s</cep>', preg_replace('/[^0-9]/', '', $cep));
85
        $request .= '</cli:consultaCEP>';
86
        $namespaces = [
87
            'xmlns:cli' => 'http://cliente.bean.master.sigep.bsb.correios.com.br/',
88
        ];
89
        $actions = [
90
            'curl' => null,
91
            'native' => 'consultaCEP',
92
        ];
93
94
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
95
        return $result->return;
96
    }
97
98
    /**
99
     * @return \stdClass
100
     */
101
    public function getStatusCartaoPostagem()
102
    {
103
        $request = '<cli:getStatusCartaoPostagem>';
104
        $request .= sprintf('<numeroCartaoPostagem>%s</numeroCartaoPostagem>', $this->getConfig()->getPostCard());
105
        $request .= sprintf('<usuario>%s</usuario>', $this->getConfig()->getUser());
106
        $request .= sprintf('<senha>%s</senha>', $this->getConfig()->getPassword());
107
        $request .= '</cli:getStatusCartaoPostagem>';
108
        $namespaces = [
109
            'xmlns:cli' => 'http://cliente.bean.master.sigep.bsb.correios.com.br/',
110
        ];
111
        $actions = [
112
            'curl' => null,
113
            'native' => 'getStatusCartaoPostagem',
114
        ];
115
116
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
117
        return $result->return;
118
    }
119
120
    /**
121
     * @param $service
122
     * @param $amount
123
     *
124
     * @return array
125
     * @throws InvalidArgumentException
126
     */
127
    public function solicitaEtiquetas($service, $amount)
128
    {
129
        $request = '<cli:solicitaEtiquetas>';
130
        $request .= sprintf('<tipoDestinatario>%s</tipoDestinatario>', 'C');
131
        $request .= sprintf('<identificador>%s</identificador>', $this->getConfig()->getCNPJ());
132
        $request .= sprintf('<idServico>%s</idServico>', $service);
133
        $request .= sprintf('<qtdEtiquetas>%d</qtdEtiquetas>', $amount);
134
        $request .= sprintf('<usuario>%s</usuario>', $this->getConfig()->getUser());
135
        $request .= sprintf('<senha>%s</senha>', $this->getConfig()->getPassword());
136
        $request .= '</cli:solicitaEtiquetas>';
137
        $namespaces = [
138
            'xmlns:cli' => 'http://cliente.bean.master.sigep.bsb.correios.com.br/',
139
        ];
140
        $actions = [
141
            'curl' => null,
142
            'native' => 'solicitaEtiquetas',
143
        ];
144
145
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
146
        $tags = explode(',', $result->return);
147
        $result = [];
148
        foreach ($tags as $i => $tag) {
149
            $result[$tag] = PostalObject::calculateDv($tag);
150
        }
151
152
        return $result;
153
    }
154
155
    /**
156
     * @param MailingList $mailingList
157
     *
158
     * @return integer
159
     * @throws \Exception
160
     */
161
    public function fechaPlpVariosServicos(MailingList $mailingList)
162
    {
163
        $request = '<cli:fechaPlpVariosServicos>';
164
        $request .= sprintf('<xml>%s</xml>', $mailingList->save($this->getConfig()));
165
        $request .= sprintf('<idPlpCliente>%s</idPlpCliente>', $mailingList->getId());
166
        $request .= sprintf('<cartaoPostagem>%s</cartaoPostagem>', $this->getConfig()->getPostCard());
167
        foreach ($mailingList->getTags() as $tag) {
168
            $request .= sprintf('<listaEtiquetas>%s</listaEtiquetas>', $tag);
169
        }
170
        $request .= sprintf('<usuario>%s</usuario>', $this->getConfig()->getUser());
171
        $request .= sprintf('<senha>%s</senha>', $this->getConfig()->getPassword());
172
        $request .= '</cli:fechaPlpVariosServicos>';
173
        $namespaces = [
174
            'xmlns:cli' => 'http://cliente.bean.master.sigep.bsb.correios.com.br/',
175
        ];
176
        $actions = [
177
            'curl' => null,
178
            'native' => 'fechaPlpVariosServicos',
179
        ];
180
181
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
182
        return $result->return;
183
    }
184
185
    /**
186
     * @param $plpId
187
     *
188
     * @return \stdClass
189
     */
190
    public function solicitaXmlPlp($plpId)
191
    {
192
        $request = '<cli:solicitaXmlPlp>';
193
        $request .= sprintf('<idPlpMaster>%s</idPlpMaster>', $plpId);
194
        $request .= sprintf('<usuario>%s</usuario>', $this->getConfig()->getUser());
195
        $request .= sprintf('<senha>%s</senha>', $this->getConfig()->getPassword());
196
        $request .= '</cli:solicitaXmlPlp>';
197
        $namespaces = [
198
            'xmlns:cli' => 'http://cliente.bean.master.sigep.bsb.correios.com.br/',
199
        ];
200
        $actions = [
201
            'curl' => null,
202
            'native' => 'solicitaXmlPlp',
203
        ];
204
205
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
206
207
        $result = json_decode(json_encode(simplexml_load_string($result->return, \SimpleXMLElement::class, LIBXML_NOCDATA)));
208
        $result->objeto_postal = is_array($result->objeto_postal) ? $result->objeto_postal : [$result->objeto_postal];
209
        return $result;
210
    }
211
212
    /**
213
     * @param array $codes
214
     *
215
     * @return \stdClass
216
     */
217
    public function consultaSRO(array $codes)
218
    {
219
        if ($this->getConfig()->getEnvironment() == 'testing') {
220
            $this->getConfig()->setUser('ECT');
221
            $this->getConfig()->setPassword('SRO');
222
        }
223
224
        $request = '<cli:consultaSRO_NEW>';
225
        $request .= sprintf('<usuarioSro>%s</usuarioSro>', $this->getConfig()->getUserRastro());
226
        $request .= sprintf('<senhaSro>%s</senhaSro>', $this->getConfig()->getPasswordRastro());
227
        $request .= sprintf('<tipoResultado>%s</tipoResultado>', 'T');
228
        foreach ($codes as $c) {
229
            $request .= sprintf('<listaObjetos>%s</listaObjetos>', $c);
230
        }
231
        $request .= '</cli:consultaSRO_NEW>';
232
        $namespaces = [
233
            'xmlns:cli' => 'http://cliente.bean.master.sigep.bsb.correios.com.br/',
234
        ];
235
        $actions = [
236
            'curl' => null,
237
            'native' => 'buscaEventosLista',
238
        ];
239
240
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
241
242
        $result = json_decode(json_encode(simplexml_load_string($result->return, \SimpleXMLElement::class, LIBXML_NOCDATA)));
243
        $result->objeto = is_array($result->objeto) ? $result->objeto : [$result->objeto];
244
        foreach($result->objeto as $objeto) {
245
            $objeto->evento = isset($objeto->evento)
246
                ? is_array($objeto->evento) ? $objeto->evento : [$objeto->evento]
247
                : [];
248
        }
249
        return $result;
250
    }
251
}
252