WsSecurity::createWsSecuritySoapHeader()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 23
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 11
crap 3

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
declare(strict_types=1);
4
5
namespace WsdlToPhp\WsSecurity;
6
7
use SoapHeader;
8
use SoapVar;
9
10
class WsSecurity
11
{
12
    protected Security $security;
13
14 18
    protected function __construct(
15
        string $username,
16
        string $password,
17
        bool $passwordDigest = false,
18
        int $addCreated = 0,
19
        int $addExpires = 0,
20
        bool $mustUnderstand = false,
21
        ?string $actor = null,
22
        ?string $usernameId = null,
23
        bool $addNonce = true,
24
        string $envelopeNamespace = Security::ENV_NAMESPACE
25
    ) {
26
        $this
27 18
            ->initSecurity($mustUnderstand, $actor, $envelopeNamespace)
28 18
            ->setUsernameToken($username, $usernameId)
29 18
            ->setPassword($password, $passwordDigest, $addCreated)
30 18
            ->setNonce($addNonce)
31 18
            ->setCreated($addCreated)
32 18
            ->setTimestamp($addCreated, $addExpires)
33
        ;
34
    }
35
36 18
    public function getSecurity(): ?Security
37
    {
38 18
        return $this->security;
39
    }
40
41
    /**
42
     * @return SoapHeader|SoapVar
43
     */
44 18
    public static function createWsSecuritySoapHeader(
45
        string $username,
46
        string $password,
47
        bool $passwordDigest = false,
48
        int $addCreated = 0,
49
        int $addExpires = 0,
50
        bool $returnSoapHeader = true,
51
        bool $mustUnderstand = false,
52
        ?string $actor = null,
53
        ?string $usernameId = null,
54
        bool $addNonce = true,
55
        string $envelopeNamespace = Security::ENV_NAMESPACE
56
    ) {
57 18
        $self = new WsSecurity($username, $password, $passwordDigest, $addCreated, $addExpires, $mustUnderstand, $actor, $usernameId, $addNonce, $envelopeNamespace);
58 18
        if ($returnSoapHeader) {
59 14
            if (!empty($actor)) {
60 8
                return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand, $actor);
61
            }
62
63 6
            return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand);
64
        }
65
66 4
        return new SoapVar($self->getSecurity()->toSend(), XSD_ANYXML);
67
    }
68
69 18
    protected function initSecurity(bool $mustUnderstand = false, ?string $actor = null, string $envelopeNamespace = Security::ENV_NAMESPACE): self
70
    {
71 18
        $this->security = new Security($mustUnderstand, $actor, Security::NS_WSSE, $envelopeNamespace);
72
73 18
        return $this;
74
    }
75
76 18
    protected function setUsernameToken(string $username, ?string $usernameId = null): self
77
    {
78 18
        $usernameToken = new UsernameToken($usernameId);
79 18
        $usernameToken->setUsername(new Username($username));
80 18
        $this->security->setUsernameToken($usernameToken);
81
82 18
        return $this;
83
    }
84
85 18
    protected function setPassword(string $password, bool $passwordDigest = false, int $addCreated = 0): self
86
    {
87 18
        $this->getUsernameToken()->setPassword(new Password($password, $passwordDigest ? Password::TYPE_PASSWORD_DIGEST : Password::TYPE_PASSWORD_TEXT, $addCreated));
88
89 18
        return $this;
90
    }
91
92 18
    protected function setNonce(bool $addNonce): self
93
    {
94 18
        if ($addNonce) {
95 16
            $nonceValue = $this->getPassword()->getNonceValue();
96 16
            if (!empty($nonceValue)) {
97 16
                $this->getUsernameToken()->setNonce(new Nonce($nonceValue));
98
            }
99
        }
100
101 18
        return $this;
102
    }
103
104 18
    protected function setCreated(int $addCreated): self
105
    {
106 18
        $passwordDigest = $this->getPassword()->getTypeValue();
107 18
        $timestampValue = $this->getPassword()->getTimestampValue();
108 18
        if (($addCreated || Password::TYPE_PASSWORD_DIGEST === $passwordDigest) && 0 < $timestampValue) {
109 18
            $this->getUsernameToken()->setCreated(new Created($timestampValue));
110
        }
111
112 18
        return $this;
113
    }
114
115 18
    protected function setTimestamp(int $addCreated = 0, int $addExpires = 0): self
116
    {
117 18
        $timestampValue = $this->getPassword()->getTimestampValue();
118 18
        if ($addCreated && $addExpires && $timestampValue) {
119 2
            $timestamp = new Timestamp();
120 2
            $timestamp->setCreated(new Created($timestampValue));
121 2
            $timestamp->setExpires(new Expires($timestampValue, $addExpires));
122 2
            $this->security->setTimestamp($timestamp);
123
        }
124
125 18
        return $this;
126
    }
127
128 18
    protected function getUsernameToken(): ?UsernameToken
129
    {
130 18
        return $this->security->getUsernameToken();
131
    }
132
133 18
    protected function getPassword(): ?Password
134
    {
135 18
        return $this->getUsernameToken()->getPassword();
136
    }
137
}
138