Passed
Push — feature/issue-19 ( 616ee6 )
by Mikaël
46:16
created

WsSecurity::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 7
c 2
b 0
f 1
dl 0
loc 19
rs 10
cc 1
nc 1
nop 10

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
    public 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
            ->initSecurity($mustUnderstand, $actor, $envelopeNamespace)
28
            ->setUsernameToken($username, $usernameId)
29
            ->setPassword($password, $passwordDigest, $addCreated)
30
            ->setNonce($addNonce)
31
            ->setCreated($addCreated)
32
            ->setTimestamp($addCreated, $addExpires)
33
        ;
34
    }
35
36
    public function getSecurity(): ?Security
37
    {
38
        return $this->security;
39
    }
40
41
    /**
42
     * @return SoapHeader|SoapVar
43
     */
44
    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
        $self = new WsSecurity($username, $password, $passwordDigest, $addCreated, $addExpires, $mustUnderstand, $actor, $usernameId, $addNonce, $envelopeNamespace);
58
59
        return $self->getSoapHeader($returnSoapHeader, $mustUnderstand, $actor);
60
    }
61
62
    public function getSoapHeader(bool $returnSoapHeader = true, bool $mustUnderstand = false, ?string $actor = null): object
63
    {
64
        if ($returnSoapHeader) {
65
            if (!empty($actor)) {
66
                return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($this->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand, $actor);
67
            }
68
69
            return new SoapHeader(Element::NS_WSSE, Security::NAME, new SoapVar($this->getSecurity()->toSend(), XSD_ANYXML), $mustUnderstand);
70
        }
71
72
        return new SoapVar($this->getSecurity()->toSend(), XSD_ANYXML);
73
    }
74
75
    protected function initSecurity(bool $mustUnderstand = false, ?string $actor = null, string $envelopeNamespace = Security::ENV_NAMESPACE): self
76
    {
77
        $this->security = new Security($mustUnderstand, $actor, Security::NS_WSSE, $envelopeNamespace);
78
79
        return $this;
80
    }
81
82
    protected function setUsernameToken(string $username, ?string $usernameId = null): self
83
    {
84
        $usernameToken = new UsernameToken($usernameId);
85
        $usernameToken->setUsername(new Username($username));
86
        $this->security->setUsernameToken($usernameToken);
87
88
        return $this;
89
    }
90
91
    protected function setPassword(string $password, bool $passwordDigest = false, int $addCreated = 0): self
92
    {
93
        $this->getUsernameToken()->setPassword(new Password($password, $passwordDigest ? Password::TYPE_PASSWORD_DIGEST : Password::TYPE_PASSWORD_TEXT, $addCreated));
94
95
        return $this;
96
    }
97
98
    protected function setNonce(bool $addNonce): self
99
    {
100
        if ($addNonce) {
101
            $nonceValue = $this->getPassword()->getNonceValue();
102
            if (!empty($nonceValue)) {
103
                $this->getUsernameToken()->setNonce(new Nonce($nonceValue));
104
            }
105
        }
106
107
        return $this;
108
    }
109
110
    protected function setCreated(int $addCreated): self
111
    {
112
        $passwordDigest = $this->getPassword()->getTypeValue();
113
        $timestampValue = $this->getPassword()->getTimestampValue();
114
        if (($addCreated || Password::TYPE_PASSWORD_DIGEST === $passwordDigest) && 0 < $timestampValue) {
115
            $this->getUsernameToken()->setCreated(new Created($timestampValue));
116
        }
117
118
        return $this;
119
    }
120
121
    protected function setTimestamp(int $addCreated = 0, int $addExpires = 0): self
122
    {
123
        $timestampValue = $this->getPassword()->getTimestampValue();
124
        if (!$timestampValue || (0 === $addCreated && 0 === $addExpires)) {
125
            return $this;
126
        }
127
128
        $timestamp = new Timestamp();
129
        if (0 < $addCreated) {
130
            $timestamp->setCreated(new Created($timestampValue));
131
        }
132
        if (0 < $addExpires) {
133
            $timestamp->setExpires(new Expires($timestampValue, $addExpires));
134
        }
135
        $this->security->setTimestamp($timestamp);
136
137
        return $this;
138
    }
139
140
    protected function getUsernameToken(): ?UsernameToken
141
    {
142
        return $this->security->getUsernameToken();
143
    }
144
145
    protected function getPassword(): ?Password
146
    {
147
        return $this->getUsernameToken()->getPassword();
148
    }
149
}
150