Passed
Push — develop ( e10991...f36a80 )
by Mikaël
04:03
created

WsSecurity::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 10
dl 0
loc 19
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

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 WsdlToPhp\WsSecurity;
4
5
class WsSecurity
6
{
7
    /**
8
     * @var Security
9
     */
10
    protected $security;
11
12
    /**
13
     * @param string $username
14
     * @param string $password
15
     * @param bool   $passwordDigest
16
     * @param int    $addCreated
17
     * @param int    $addExpires
18
     * @param bool   $mustunderstand
19
     * @param string $actor
20
     * @param string $usernameId
21
     * @param bool   $addNonce
22
     * @param string $envelopeNamespace
23
     */
24 54
    protected function __construct(
25
        $username,
26
        $password,
27
        $passwordDigest = false,
28
        $addCreated = 0,
29
        $addExpires = 0,
30
        $mustunderstand = false,
31
        $actor = null,
32
        $usernameId = null,
33
        $addNonce = true,
34
        $envelopeNamespace = Security::ENV_NAMESPACE
35
    ) {
36 27
        $this
37 54
            ->initSecurity($mustunderstand, $actor, $envelopeNamespace)
38 54
            ->setUsernameToken($username, $usernameId)
39 54
            ->setPassword($password, $passwordDigest, $addCreated)
40 54
            ->setNonce($addNonce)
41 54
            ->setCreated($addCreated)
42 54
            ->setTimestamp($addCreated, $addExpires);
43 54
    }
44
45
    /**
46
     * @param bool   $mustunderstand
47
     * @param string $actor
48
     * @param string $envelopeNamespace
49
     *
50
     * @return WsSecurity
51
     */
52 54
    protected function initSecurity($mustunderstand = false, $actor = null, $envelopeNamespace = Security::ENV_NAMESPACE)
53
    {
54 54
        $this->security = new Security($mustunderstand, $actor, Security::NS_WSSE, $envelopeNamespace);
55 54
        return $this;
56
    }
57
    /**
58
     * @return Security
59
     */
60 54
    public function getSecurity()
61
    {
62 54
        return $this->security;
63
    }
64
65
    /**
66
     * Create the SoapHeader object to send as SoapHeader in the SOAP request.
67
     *
68
     * @param string $username
69
     * @param string $password
70
     * @param bool   $passwordDigest
71
     * @param int    $addCreated
72
     * @param int    $addExpires
73
     * @param bool   $returnSoapHeader
74
     * @param bool   $mustunderstand
75
     * @param string $actor
76
     * @param string $usernameId
77
     * @param bool   $addNonce
78
     * @param string $envelopeNamespace
79
     *
80
     * @return \SoapHeader|\SoapVar
81
     */
82 54
    public static function createWsSecuritySoapHeader(
83
        $username,
84
        $password,
85
        $passwordDigest = false,
86
        $addCreated = 0,
87
        $addExpires = 0,
88
        $returnSoapHeader = true,
89
        $mustunderstand = false,
90
        $actor = null,
91
        $usernameId = null,
92
        $addNonce = true,
93
        $envelopeNamespace = Security::ENV_NAMESPACE
94
    ) {
95 54
        $self = new WsSecurity($username, $password, $passwordDigest, $addCreated, $addExpires, $mustunderstand, $actor, $usernameId, $addNonce, $envelopeNamespace);
96 54
        if ($returnSoapHeader) {
97 42
            if (!empty($actor)) {
98 24
                return new \SoapHeader(Element::NS_WSSE, 'Security', new \SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustunderstand, $actor);
99
            } else {
100 18
                return new \SoapHeader(Element::NS_WSSE, 'Security', new \SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustunderstand);
101
            }
102
        } else {
103 12
            return new \SoapVar($self->getSecurity()->toSend(), XSD_ANYXML);
104
        }
105
    }
106
    /**
107
     * @param string $username
108
     * @param string $usernameId
109
     * @return WsSecurity
110
     */
111 54
    protected function setUsernameToken($username, $usernameId = null)
112
    {
113 54
        $usernameToken = new UsernameToken($usernameId);
114 54
        $usernameToken->setUsername(new Username($username));
115 54
        $this->security->setUsernameToken($usernameToken);
116 54
        return $this;
117
    }
118
    /**
119
     * @param string $password
120
     * @param bool $passwordDigest
121
     * @param int $addCreated
122
     * @return WsSecurity
123
     */
124 54
    protected function setPassword($password, $passwordDigest = false, $addCreated = 0)
125
    {
126 54
        $this->getUsernameToken()->setPassword(new Password($password, $passwordDigest ? Password::TYPE_PASSWORD_DIGEST : Password::TYPE_PASSWORD_TEXT, is_bool($addCreated) ? 0 : ($addCreated > 0 ? $addCreated : 0)));
0 ignored issues
show
introduced by
The condition is_bool($addCreated) is always false.
Loading history...
127 54
        return $this;
128
    }
129
    /**
130
     * @param  bool $addNonce
131
     * @return WsSecurity
132
     */
133 54
    protected function setNonce($addNonce)
134
    {
135 54
        if ($addNonce) {
136 48
            $nonceValue = $this->getPassword()->getNonceValue();
137 48
            if (!empty($nonceValue)) {
138 48
                $this->getUsernameToken()->setNonce(new Nonce($nonceValue));
139 24
            }
140 24
        }
141 54
        return $this;
142
    }
143
    /**
144
     * @param int $addCreated
145
     * @return WsSecurity
146
     */
147 54
    protected function setCreated($addCreated)
148
    {
149 54
        $passwordDigest = $this->getPassword()->getTypeValue();
150 54
        $timestampValue = $this->getPassword()->getTimestampValue();
151 54
        if (($addCreated || $passwordDigest === Password::TYPE_PASSWORD_DIGEST) && $timestampValue > 0) {
152 54
            $this->getUsernameToken()->setCreated(new Created($timestampValue));
153 27
        }
154 54
        return $this;
155
    }
156
    /**
157
     * @param int $addCreated
158
     * @param int $addExpires
159
     * @return WsSecurity
160
     */
161 54
    protected function setTimestamp($addCreated = 0, $addExpires = 0)
162
    {
163 54
        $timestampValue = $this->getPassword()->getTimestampValue();
164 54
        if ($addCreated && $addExpires && $timestampValue) {
165 6
            $timestamp = new Timestamp();
166 6
            $timestamp->setCreated(new Created($timestampValue));
167 6
            $timestamp->setExpires(new Expires($timestampValue, $addExpires));
168 6
            $this->security->setTimestamp($timestamp);
169 3
        }
170 54
        return $this;
171
    }
172
    /**
173
     * @return UsernameToken
174
     */
175 54
    protected function getUsernameToken()
176
    {
177 54
        return $this->security->getUsernameToken();
178
    }
179
    /**
180
     * @return Password
181
     */
182 54
    protected function getPassword()
183
    {
184 54
        return $this->getUsernameToken()->getPassword();
185
    }
186
}
187