Wsse::generateHeader()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 56
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 45
cts 45
cp 1
rs 8.7592
c 0
b 0
f 0
cc 5
eloc 45
nc 2
nop 0
crap 5

How to fix   Long Method   

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:

1
<?php
2
3
/*
4
 * This file is part of the AMFWebServicesClientBundle package.
5
 *
6
 * (c) Amine Fattouch <http://github.com/fattouchsquall>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AMF\WebServicesClientBundle\Soap\Security;
13
14
/**
15
 * Add security wsse layer to soap webservices.
16
 *
17
 * @author Mohamed Amine Fattouch <[email protected]>
18
 */
19
class Wsse
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $username;
25
26
    /**
27
     * @var string
28
     */
29
    protected $password;
30
31
    /**
32
     * The constructor class.
33
     *
34
     * @param string $username The username of wsse security.
35
     * @param string $password The password of wsse security.
36
     */
37 6
    public function __construct($username, $password)
38
    {
39 6
        $this->username = $username;
40 6
        $this->password = $password;
41 6
    }
42
43
    /**
44
     * Generates WSSE header for SOAP.
45
     *
46
     * @return \SoapHeader
47
     */
48 6
    public function generateHeader()
49
    {
50 6
        if (isset($this->username)
51 6
                && strlen($this->username) > 0
52 6
                && isset($this->password)
53 6
                && strlen($this->password) > 0) {
54 3
            $xsdNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
55
56 3
            $usernameSoapVar  = new \SoapVar(
57 3
                $this->username,
58 3
                XSD_STRING,
59 3
                null,
60 3
                $xsdNamespace,
61 3
                null,
62
                $xsdNamespace
63 3
            );
64 3
            $passwordSoapVar  = new \SoapVar(
65 3
                $this->password,
66 3
                XSD_STRING,
67 3
                null,
68 3
                $xsdNamespace,
69 3
                null,
70
                $xsdNamespace
71 3
            );
72 3
            $wsseAuth         = new WsseAuth($usernameSoapVar, $passwordSoapVar);
0 ignored issues
show
Documentation introduced by
$usernameSoapVar is of type object<SoapVar>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$passwordSoapVar is of type object<SoapVar>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73 3
            $wsseAuthSoapVar  = new \SoapVar(
74 3
                $wsseAuth,
75 3
                SOAP_ENC_OBJECT,
76 3
                null,
77 3
                $xsdNamespace,
78 3
                'UsernameToken',
79
                $xsdNamespace
80 3
            );
81 3
            $wsseToken        = new WsseToken($wsseAuthSoapVar);
0 ignored issues
show
Documentation introduced by
$wsseAuthSoapVar is of type object<SoapVar>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82 3
            $wsseTokenSoapVar = new \SoapVar(
83 3
                $wsseToken,
84 3
                SOAP_ENC_OBJECT,
85 3
                null,
86 3
                $xsdNamespace,
87 3
                'UsernameToken',
88
                $xsdNamespace
89 3
            );
90 3
            $secHeaderValue   = new \SoapVar(
91 3
                $wsseTokenSoapVar,
92 3
                SOAP_ENC_OBJECT,
93 3
                null,
94 3
                $xsdNamespace,
95 3
                'Security',
96
                $xsdNamespace
97 3
            );
98
99 3
            return new \SoapHeader($xsdNamespace, 'Security', $secHeaderValue);
100
        }
101
102 3
        throw new \Exception('Username and password cannot be empty');
103
    }
104
}
105