Completed
Push — develop ( e7b8b1...0940ec )
by Mikaël
94:26 queued 93:14
created

WsSecurity::initSecurity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\WsSecurity;
4
5
class WsSecurity
6
{
7
    /**
8
     * @var Security
9
     */
10
    protected $security;
11
    /**
12
     * @param string $username
13
     * @param string $password
14
     * @param bool $passwordDigest
15
     * @param int $addCreated
16
     * @param int $addExpires
17
     * @param bool $mustunderstand
18
     * @param string $actor
19
     * @param string $usernameId
20
     * @param bool $addNonce
21
     */
22 48
    protected function __construct($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $mustunderstand = false, $actor = null, $usernameId = null, $addNonce = true)
23
    {
24 24
        $this
25 48
            ->initSecurity($mustunderstand, $actor)
26 48
            ->setUsernameToken($username, $usernameId)
27 48
            ->setPassword($password, $passwordDigest, $addCreated)
28 48
            ->setNonce($addNonce)
29 48
            ->setCreated($addCreated)
30 48
            ->setTimestamp($addCreated, $addExpires);
31 48
    }
32
    /**
33
     * @param bool $mustunderstand
34
     * @param string $actor
35
     * @return WsSecurity
36
     */
37 48
    protected function initSecurity($mustunderstand = false, $actor = null)
38
    {
39 48
        $this->security = new Security($mustunderstand, $actor);
40 48
        return $this;
41
    }
42
    /**
43
     * @return Security
44
     */
45 48
    public function getSecurity()
46
    {
47 48
        return $this->security;
48
    }
49
    /**
50
     * Create the SoapHeader object to send as SoapHeader in the SOAP request.
51
     * @param string $username
52
     * @param string $password
53
     * @param bool $passwordDigest
54
     * @param int $addCreated
55
     * @param int $addExpires
56
     * @param bool $mustunderstand
57
     * @param string $actor
58
     * @param string $usernameId
59
     * @param bool $addNonce
60
     * @return \SoapHeader|\SoapVar
61
     */
62 48
    public static function createWsSecuritySoapHeader($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $returnSoapHeader = true, $mustunderstand = false, $actor = null, $usernameId = null, $addNonce = true)
63
    {
64 48
        $self = new WsSecurity($username, $password, $passwordDigest, $addCreated, $addExpires, $mustunderstand, $actor, $usernameId, $addNonce);
65 48
        if ($returnSoapHeader) {
66 36
            if (!empty($actor)) {
67 18
                return new \SoapHeader(Element::NS_WSSE, 'Security', new \SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustunderstand, $actor);
68
            } else {
69 18
                return new \SoapHeader(Element::NS_WSSE, 'Security', new \SoapVar($self->getSecurity()->toSend(), XSD_ANYXML), $mustunderstand);
70
            }
71
        } else {
72 12
            return new \SoapVar($self->getSecurity()->toSend(), XSD_ANYXML);
73
        }
74
    }
75
    /**
76
     * @param string $username
77
     * @param string $usernameId
78
     * @return WsSecurity
79
     */
80 48
    protected function setUsernameToken($username, $usernameId = null)
81
    {
82 48
        $usernameToken = new UsernameToken($usernameId);
83 48
        $usernameToken->setUsername(new Username($username));
84 48
        $this->security->setUsernameToken($usernameToken);
85 48
        return $this;
86
    }
87
    /**
88
     * @param string $password
89
     * @param bool $passwordDigest
90
     * @param int $addCreated
91
     * @return WsSecurity
92
     */
93 48
    protected function setPassword($password, $passwordDigest = false, $addCreated = 0)
94
    {
95 48
        $this->getUsernameToken()->setPassword(new Password($password, $passwordDigest ? Password::TYPE_PASSWORD_DIGEST : Password::TYPE_PASSWORD_TEXT, is_bool($addCreated) ? 0 : ($addCreated > 0 ? $addCreated : 0)));
96 48
        return $this;
97
    }
98
    /**
99
     * @param  bool $addNonce
100
     * @return WsSecurity
101
     */
102 48
    protected function setNonce($addNonce)
103
    {
104 48
        if ($addNonce) {
105 42
            $nonceValue = $this->getPassword()->getNonceValue();
106 42
            if (!empty($nonceValue)) {
107 42
                $this->getUsernameToken()->setNonce(new Nonce($nonceValue));
108 21
            }
109 21
        }
110 48
        return $this;
111
    }
112
    /**
113
     * @param int $addCreated
114
     * @return WsSecurity
115
     */
116 48
    protected function setCreated($addCreated)
117
    {
118 48
        $passwordDigest = $this->getPassword()->getTypeValue();
119 48
        $timestampValue = $this->getPassword()->getTimestampValue();
120 48
        if (($addCreated || $passwordDigest === Password::TYPE_PASSWORD_DIGEST) && $timestampValue > 0) {
121 48
            $this->getUsernameToken()->setCreated(new Created($timestampValue));
122 24
        }
123 48
        return $this;
124
    }
125
    /**
126
     * @param int $addCreated
127
     * @param int $addExpires
128
     * @return WsSecurity
129
     */
130 48
    protected function setTimestamp($addCreated = 0, $addExpires = 0)
131
    {
132 48
        $timestampValue = $this->getPassword()->getTimestampValue();
133 48
        if ($addCreated && $addExpires && $timestampValue) {
134 6
            $timestamp = new Timestamp();
135 6
            $timestamp->setCreated(new Created($timestampValue));
136 6
            $timestamp->setExpires(new Expires($timestampValue, $addExpires));
137 6
            $this->security->setTimestamp($timestamp);
138 3
        }
139 48
        return $this;
140
    }
141
    /**
142
     * @return UsernameToken
143
     */
144 48
    protected function getUsernameToken()
145
    {
146 48
        return $this->security->getUsernameToken();
147
    }
148
    /**
149
     * @return Password
150
     */
151 48
    protected function getPassword()
152
    {
153 48
        return $this->getUsernameToken()->getPassword();
154
    }
155
}
156