Calculo::calcularPrecoPrazo()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 54
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 31
c 2
b 0
f 0
nc 5
nop 12
dl 0
loc 54
ccs 0
cts 30
cp 0
crap 90
rs 8.0555

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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