Passed
Pull Request — master (#219)
by
unknown
03:00
created

SoapFake::send()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 0
cts 29
cp 0
rs 8.3814
c 0
b 0
f 0
cc 6
nc 6
nop 8
crap 42

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
use NFePHP\Common\Soap\SoapBase;
18
use NFePHP\Common\Soap\SoapInterface;
19
use NFePHP\Common\Exception\SoapException;
20
use NFePHP\Common\Certificate;
21
use Psr\Log\LoggerInterface;
22
23
class SoapFake extends SoapBase implements SoapInterface
24
{
25
    /**
26
     * Constructor
27
     * @param Certificate $certificate
28
     * @param LoggerInterface $logger
29
     */
30 4
    public function __construct(Certificate $certificate = null, LoggerInterface $logger = null)
31
    {
32 4
        parent::__construct($certificate, $logger);
33 3
    }
34
    
35
    public function send(
36
        $url,
37
        $operation = '',
38
        $action = '',
39
        $soapver = SOAP_1_2,
40
        $parameters = [],
41
        $namespaces = [],
42
        $request = '',
43
        $soapheader = null
44
    ) {
45
        $envelope = $this->makeEnvelopeSoap(
46
            $request,
47
            $namespaces,
48
            $soapver,
49
            $soapheader
50
        );
51
        $msgSize = strlen($envelope);
52
53
        switch ($soapver) {
54
            case SOAP_1_1:
55
                $parameters[] = "Content-Type: text/xml;charset=UTF-8;";
56
                if(!empty($action)) $parameters[] = "SOAPAction: \"$action\"";
57
                $parameters[] = "Content-length: $msgSize";
58
                break;
59
            case SOAP_1_2:
60
                $parameters = [
61
                    "Content-Type: application/soap+xml;charset=utf-8;",
62
                    "Content-length: $msgSize"
63
                ];
64
                if(!empty($action)) $parameter[0] .= "action=$action";
0 ignored issues
show
Bug introduced by
The variable $parameter does not exist. Did you mean $parameters?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
65
                break;
66
            default:
67
                $parameters = [
68
                    "Content-Type: application/soap+xml;charset=utf-8;",
69
                    "Content-length: $msgSize"
70
                ];
71
                if(!empty($action)) $parameter[0] .= "action=$action";
0 ignored issues
show
Bug introduced by
The variable $parameter does not exist. Did you mean $parameters?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
72
                break;
73
        }
74
        
75
        $requestHead = implode("\n", $parameters);
76
        $requestBody = $envelope;
77
        
78
        return json_encode([
79
            'url' => $url,
80
            'operation' => $operation,
81
            'action' => $action,
82
            'soapver' => $soapver,
83
            'parameters' => $parameters,
84
            'header' => $requestHead,
85
            'namespaces' => $namespaces,
86
            'body' => $requestBody
87
        ], JSON_PRETTY_PRINT);
88
    }
89
}
90