Rastreio::rastreamento()   B
last analyzed

Complexity

Conditions 6
Paths 20

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
c 2
b 0
f 0
nc 20
nop 1
dl 0
loc 29
ccs 0
cts 14
cp 0
crap 42
rs 8.9617
1
<?php
2
namespace Eduardokum\CorreiosPhp;
3
4
use Eduardokum\CorreiosPhp\Contracts\Config\Config as ConfigContract;
5
6
class Rastreio extends Correios
7
{
8
    public function __construct(ConfigContract $config = null, $type = 'curl')
9
    {
10
        parent::__construct($config, $type);
11
        $this->setWs($this->getWs('rastreamento'));
12
13
        if ($this->getConfig()->getEnvironment() == 'testing') {
14
            $this->getConfig()->setUserRastro('ECT');
15
            $this->getConfig()->setPasswordRastro('SRO');
16
        }
17
    }
18
19
    /**
20
     * @param array $codes
21
     *
22
     * @return \stdClass
23
     */
24
    public function rastreamento(array $codes)
25
    {
26
        $request = '<res:buscaEventosLista>';
27
        $request .= sprintf('<usuario>%s</usuario>', $this->getConfig()->getUserRastro());
28
        $request .= sprintf('<senha>%s</senha>', $this->getConfig()->getPasswordRastro());
29
        $request .= sprintf('<tipo>%s</tipo>', 'L');
30
        $request .= sprintf('<resultado>%s</resultado>', 'T');
31
        $request .= sprintf('<lingua>%s</lingua>', '101');
32
        foreach ($codes as $c) {
33
            $request .= sprintf('<objetos>%s</objetos>', $c);
34
        }
35
        $request .= '</res:buscaEventosLista>';
36
        $namespaces = [
37
            'xmlns:res' => 'http://resource.webservice.correios.com.br/',
38
        ];
39
        $actions = [
40
            'curl' => 'buscaEventosLista',
41
            'native' => 'buscaEventosLista',
42
        ];
43
44
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
45
        $result = $result->return;
46
        $result->objeto = is_array($result->objeto) ? $result->objeto : [$result->objeto];
47
        foreach($result->objeto as $objeto) {
48
            $objeto->evento = isset($objeto->evento)
49
                ? is_array($objeto->evento) ? $objeto->evento : [$objeto->evento]
50
                : [];
51
        }
52
        return $result;
53
    }
54
}
55