Calculo   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 91
ccs 0
cts 34
cp 0
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B calcularPrecoPrazo() 0 54 9
1
<?php
2
namespace Eduardokum\CorreiosPhp;
3
4
use Eduardokum\CorreiosPhp\Contracts\Config\Config as ConfigContract;
5
use Eduardokum\CorreiosPhp\Exception\InvalidArgumentException;
6
7
class Calculo extends Correios
8
{
9
    const SERVICE_SEDEX = '04014';
10
    const SERVICE_SEDEX_COBRAR = '04065';
11
    const SERVICE_PAC = '04510';
12
    const SERVICE_PAC_COBRAR = '04707';
13
    const SERVICE_SEDEX_12 = '40169';
14
    const SERVICE_SEDEX_10 = '40215';
15
    const SERVICE_SEDEX_HOJE = '40290';
16
17
    const FORMAT_PACOTE  = 1;
18
    const FORMAT_ROLO  = 1;
19
    const FORMAT_ENVELOPE  = 3;
20
21
    public function __construct(ConfigContract $config = null, $type = 'curl')
22
    {
23
        parent::__construct($config, $type);
24
        $this->setWs($this->getWs('calculo'));
25
    }
26
27
    /**
28
     * @param         $service
29
     * @param         $cepFrom
30
     * @param         $cepTo
31
     * @param int     $weight
32
     * @param int     $format
33
     * @param int     $length
34
     * @param int     $height
35
     * @param int     $width
36
     * @param int     $diameter
37
     * @param boolean $maoPropria
38
     * @param int     $price
39
     * @param boolean $ar
40
     *
41
     * @return \stdClass
42
     * @throws InvalidArgumentException
43
     */
44
    public function calcularPrecoPrazo(
45
        $service,
46
        $cepTo,
47
        $cepFrom = null,
48
        $weight = 1,
49
        $format = 1,
50
        $length = 16,
51
        $height = 2,
52
        $width = 11,
53
        $diameter = 1,
54
        $price = 0,
55
        $maoPropria = false,
56
        $ar = false
57
    ) {
58
        if ($format == self::FORMAT_ENVELOPE && $weight > 1) {
59
            throw new InvalidArgumentException('The weight value can not be greater than 1kg when the format is letter');
60
        }
61
        if ($length < 16) {
62
            throw new InvalidArgumentException('Length less than 16cm is not accepted');
63
        }
64
        if ($height < 2) {
65
            throw new InvalidArgumentException('Height less than 2cm is not accepted');
66
        }
67
        if ($width < 11) {
68
            throw new InvalidArgumentException('Width less than 11cm is not accepted');
69
        }
70
71
        $cepFrom = $cepFrom ?: $this->getConfig()->getSender()->getCep();
72
73
        $request = '<CalcPrecoPrazo xmlns="http://tempuri.org/">';
74
        $request .= sprintf('<nCdEmpresa>%s</nCdEmpresa>', $this->getConfig()->getAdministrativeCode());
75
        $request .= sprintf('<sDsSenha>%s</sDsSenha>', $this->getConfig()->getPassword());
76
        $request .= sprintf('<nCdServico>%s</nCdServico>', $service);
77
        $request .= sprintf('<sCepOrigem>%08s</sCepOrigem>', preg_replace('/[^0-9]/', '', $cepFrom));
78
        $request .= sprintf('<sCepDestino>%08s</sCepDestino>', preg_replace('/[^0-9]/', '', $cepTo));
79
        $request .= sprintf('<nVlPeso>%d</nVlPeso>', $weight);
80
        $request .= sprintf('<nCdFormato>%d</nCdFormato>', $format);
81
        $request .= sprintf('<nVlComprimento>%d</nVlComprimento>', $length);
82
        $request .= sprintf('<nVlAltura>%d</nVlAltura>', $height);
83
        $request .= sprintf('<nVlLargura>%d</nVlLargura>', $width);
84
        $request .= sprintf('<nVlDiametro>%d</nVlDiametro>', $diameter);
85
        $request .= sprintf('<sCdMaoPropria>%s</sCdMaoPropria>', $maoPropria ? 'S' : 'N');
86
        $request .= sprintf('<nVlValorDeclarado>%s</nVlValorDeclarado>', $price);
87
        $request .= sprintf('<sCdAvisoRecebimento>%s</sCdAvisoRecebimento>', $ar ? 'S' : 'N');
88
        $request .= '</CalcPrecoPrazo>';
89
        $namespaces = [];
90
        $actions = [
91
            'curl' => 'http://tempuri.org/CalcPrecoPrazo',
92
            'native' => 'http://tempuri.org/CalcPrecoPrazo',
93
        ];
94
95
        $result = $this->getSoap()->send($this->url(), $actions, $request, $namespaces);
96
97
        return $result->CalcPrecoPrazoResult;
98
    }
99
}
100