Test Setup Failed
Push — master ( 4e700f...c7183e )
by Julito
63:12
created

WSASoap   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 109
Duplicated Lines 11.93 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 13
loc 109
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php 
2
/** 
3
 * soap-wsa.php 
4
 * 
5
 * Copyright (c) 2007, Robert Richards <[email protected]>. 
6
 * All rights reserved. 
7
 * 
8
 * Redistribution and use in source and binary forms, with or without 
9
 * modification, are permitted provided that the following conditions 
10
 * are met: 
11
 * 
12
 *   * Redistributions of source code must retain the above copyright 
13
 *     notice, this list of conditions and the following disclaimer. 
14
 * 
15
 *   * Redistributions in binary form must reproduce the above copyright 
16
 *     notice, this list of conditions and the following disclaimer in 
17
 *     the documentation and/or other materials provided with the 
18
 *     distribution. 
19
 * 
20
 *   * Neither the name of Robert Richards nor the names of his 
21
 *     contributors may be used to endorse or promote products derived 
22
 *     from this software without specific prior written permission. 
23
 * 
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
27
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
28
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
31
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
32
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
34
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
35
 * POSSIBILITY OF SUCH DAMAGE. 
36
 * 
37
 * @author     Robert Richards <[email protected]> 
38
 * @copyright  2007 Robert Richards <[email protected]> 
39
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License 
40
 * @version    1.0.0 
41
 */ 
42
43
class WSASoap { 
44
    const WSANS = 'http://schemas.xmlsoap.org/ws/2004/08/addressing'; 
45
    const WSAPFX = 'wsa'; 
46
    private $soapNS, $soapPFX; 
47
    private $soapDoc = NULL; 
48
    private $envelope = NULL; 
49
    private $SOAPXPath = NULL; 
50
    private $header = NULL; 
51
    private $messageID = NULL; 
52
     
53
    private function locateHeader() { 
54
        if ($this->header == NULL) { 
55
            $headers = $this->SOAPXPath->query('//wssoap:Envelope/wssoap:Header'); 
56
            $header = $headers->item(0); 
57
            if (! $header) { 
58
                $header = $this->soapDoc->createElementNS($this->soapNS, $this->soapPFX.':Header'); 
59
                $this->envelope->insertBefore($header, $this->envelope->firstChild); 
60
            } 
61
            $this->header = $header; 
62
        } 
63
        return $this->header; 
64
    } 
65
66
    public function __construct($doc) { 
67
        $this->soapDoc = $doc; 
68
        $this->envelope = $doc->documentElement; 
69
        $this->soapNS = $this->envelope->namespaceURI; 
70
        $this->soapPFX = $this->envelope->prefix; 
71
        $this->SOAPXPath = new DOMXPath($doc); 
72
        $this->SOAPXPath->registerNamespace('wssoap', $this->soapNS); 
73
        $this->SOAPXPath->registerNamespace('wswsa', self::WSANS);
74
         
75
        $this->envelope->setAttributeNS("http://www.w3.org/2000/xmlns/", 'xmlns:'.self::WSAPFX, self::WSANS);
76
        $this->locateHeader(); 
77
    } 
78
79
    public function addAction($action) { 
80
        /* Add the WSA Action */ 
81
        $header = $this->locateHeader(); 
82
83
        $nodeAction = $this->soapDoc->createElementNS(self::WSANS, self::SAPFX.':Action', $action);
84
        $header->appendChild($nodeAction); 
85
    } 
86
87
    public function addTo($location) { 
88
        /* Add the WSA To */ 
89
        $header = $this->locateHeader(); 
90
91
        $nodeTo = $this->soapDoc->createElementNS(WSASoap::WSANS, WSASoap::WSAPFX.':To', $location); 
92
        $header->appendChild($nodeTo); 
93
    } 
94
95
    private function createID() { 
96
        $uuid = md5(uniqid(rand(), true)); 
97
        $guid =  'uudi:'.substr($uuid,0,8)."-". 
98
                substr($uuid,8,4)."-". 
99
                substr($uuid,12,4)."-". 
100
                substr($uuid,16,4)."-". 
101
                substr($uuid,20,12); 
102
        return $guid; 
103
    } 
104
105
    public function addMessageID($id=NULL) { 
106
        /* Add the WSA MessageID or return existing ID */ 
107
        if (! is_null($this->messageID)) { 
108
            return $this->messageID; 
109
        } 
110
111
        if (empty($id)) { 
112
            $id = $this->createID(); 
113
        } 
114
115
        $header = $this->locateHeader(); 
116
117
        $nodeID = $this->soapDoc->createElementNS(self::WSANS, self::WSAPFX.':MessageID', $id);
118
        $header->appendChild($nodeID); 
119
        $this->messageID = $id; 
120
    } 
121
122
    public function addReplyTo($address = NULL) { 
123
            /* Create Message ID is not already added - required for ReplyTo */ 
124
            if (is_null($this->messageID)) { 
125
                $this->addMessageID(); 
126
            } 
127
            /* Add the WSA ReplyTo */ 
128
            $header = $this->locateHeader(); 
129
     
130
            $nodeReply = $this->soapDoc->createElementNS(self::WSANS, self::WSAPFX.':ReplyTo');
131
            $header->appendChild($nodeReply); 
132
             
133
            if (empty($address)) { 
134
                $address = 'http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous'; 
135
            } 
136
            $nodeAddress = $this->soapDoc->createElementNS(self::WSANS, self::WSAPFX.':Address', $address);
137
            $nodeReply->appendChild($nodeAddress); 
138
    } 
139
140
    public function getDoc() { 
141
        return $this->soapDoc; 
142
    } 
143
     
144
    public function saveXML() { 
145
        return $this->soapDoc->saveXML(); 
146
    } 
147
148
    public function save($file) { 
149
        return $this->soapDoc->save($file); 
150
    } 
151
} 
152
153