Passed
Pull Request — master (#219)
by
unknown
01:52
created

SoapFake::send()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 0
cts 27
cp 0
rs 8.7579
c 0
b 0
f 0
cc 5
nc 6
nop 8
crap 30

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
3
namespace NFePHP\Common\Soap;
4
5
/**
6
 * Soap fake class used for development only
7
 *
8
 * @category  NFePHP
9
 * @package   NFePHP\Common\Soap\SoapFake
10
 * @copyright NFePHP Copyright (c) 2017
11
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
12
 * @license   https://opensource.org/licenses/MIT MIT
13
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
14
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
15
 * @link      http://github.com/nfephp-org/sped-common for the canonical source repository
16
 */
17
18
use NFePHP\Common\Soap\SoapBase;
19
use NFePHP\Common\Soap\SoapInterface;
20
use NFePHP\Common\Exception\SoapException;
21
use NFePHP\Common\Certificate;
22
use Psr\Log\LoggerInterface;
23
24
class SoapFake extends SoapBase implements SoapInterface
25
{
26
    /**
27
     * Constructor
28
     * @param Certificate $certificate
29
     * @param LoggerInterface $logger
30
     */
31 4
    public function __construct(Certificate $certificate = null, LoggerInterface $logger = null)
32
    {
33 4
        parent::__construct($certificate, $logger);
34 3
    }
35
36
    public function send(
37
        $url,
38
        $operation = '',
39
        $action = '',
40
        $soapver = SOAP_1_2,
41
        $parameters = [],
42
        $namespaces = [],
43
        $request = '',
44
        $soapheader = null
45
    ) {
46
        $envelope = $this->makeEnvelopeSoap(
47
            $request,
48
            $namespaces,
49
            $soapver,
50
            $soapheader
51
        );
52
        $msgSize = strlen($envelope);
53
54
        switch ($soapver) {
55
            case SOAP_1_1:
56
                $parameters[] = "Content-Type: text/xml;charset=UTF-8;";
57
                if (!empty($action)) {
58
                    $parameters[] = "SOAPAction: \"$action\"";
59
                }
60
                $parameters[] = "Content-length: $msgSize";
61
                break;
62
            case SOAP_1_2:
63
            default:
64
                $parameters = [
65
                    "Content-Type: application/soap+xml;charset=utf-8;",
66
                    "Content-length: $msgSize"
67
                ];
68
                if (!empty($action)) {
69
                    $parameters[0] .= "action=$action";
70
                }
71
                break;
72
        }
73
        $requestHead = implode("\n", $parameters);
74
        $requestBody = $envelope;
75
76
        return json_encode([
77
            'url' => $url,
78
            'operation' => $operation,
79
            'action' => $action,
80
            'soapver' => $soapver,
81
            'parameters' => $parameters,
82
            'header' => $requestHead,
83
            'namespaces' => $namespaces,
84
            'body' => $requestBody
85
        ], JSON_PRETTY_PRINT);
86
    }
87
}
88