Passed
Pull Request — master (#195)
by Jonathan
03:35 queued 24s
created

SforceMetadataClient   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 65
c 1
b 0
f 0
dl 0
loc 140
rs 10
1
<?php
2
/*
3
 * Copyright (c) 2007, salesforce.com, inc.
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
7
 * that the following conditions are met:
8
 *
9
 *    Redistributions of source code must retain the above copyright notice, this list of conditions and the
10
 *    following disclaimer.
11
 *
12
 *    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13
 *    the following disclaimer in the documentation and/or other materials provided with the distribution.
14
 *
15
 *    Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or
16
 *    promote products derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
19
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25
 * POSSIBILITY OF SUCH DAMAGE.
26
 */
27
require_once ('SforceMetaObject.php');
28
29
class SforceMetadataClient {
30
  public $sforce;
31
  protected $sessionId;
32
  protected $location;
33
  protected $version = '27.0';
34
35
  protected $namespace = 'http://soap.sforce.com/2006/04/metadata';
36
37
  public function __construct($wsdl, $loginResult, $sforceConn) {
38
39
    $soapClientArray = null;
40
    
41
	  $phpversion = substr(phpversion(), 0, strpos(phpversion(), '-'));
42
//		if (phpversion() > '5.1.2') {
43
	  if ($phpversion > '5.1.2') {
44
      $soapClientArray = array (
45
      'user_agent' => 'salesforce-toolkit-php/'.$this->version,
46
      'encoding' => 'utf-8',
47
      'trace' => 1,
48
      'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
49
      'sessionId' => $loginResult->sessionId
50
      );
51
    } else {
52
      $soapClientArray = array (
53
	  'user_agent' => 'salesforce-toolkit-php/'.$this->version,
54
      'encoding' => 'utf-8',
55
      'trace' => 1,
56
      'sessionId' => $loginResult->sessionId
57
      );
58
    }
59
    $this->sforce = new SoapClient($wsdl,$soapClientArray);
60
    //$this->sforce->__setSoapHeaders($header_array);
61
62
63
    $sessionVar = array(
64
      'sessionId' => new SoapVar($loginResult->sessionId, XSD_STRING)
65
    );
66
67
    $headerBody = new SoapVar($sessionVar, SOAP_ENC_OBJECT);
68
69
    $session_header = new SoapHeader($this->namespace, 'SessionHeader', $headerBody, false);
70
71
    $header_array = array (
72
    $session_header
73
    );
74
75
    $this->sforce->__setSoapHeaders($header_array);
76
77
    $this->sforce->__setLocation($loginResult->metadataServerUrl);
78
    return $this->sforce;
79
  }
80
81
  /**
82
   * Specifies the session ID returned from the login server after a successful
83
   * login.
84
   */
85
  protected function _setLoginHeader($loginResult) {
86
    $this->sessionId = $loginResult->sessionId;
87
    $this->setSessionHeader($this->sessionId);
88
    $serverURL = $loginResult->serverUrl;
89
    $this->setEndPoint($serverURL);
90
  }
91
92
  /**
93
   * Set the endpoint.
94
   *
95
   * @param string $location   Location
96
   */
97
  public function setEndpoint($location) {
98
    $this->location = $location;
99
    $this->sforce->__setLocation($location);
100
  }
101
102
  /**
103
   * Set the Session ID
104
   *
105
   * @param string $sessionId   Session ID
106
   */
107
  public function setSessionHeader($sessionId) {
108
    $this->sforce->__setSoapHeaders(NULL);
109
    $session_header = new SoapHeader($this->namespace, 'SessionHeader', array (
110
    'sessionId' => $sessionId
111
    ));
112
    $this->sessionId = $sessionId;
113
    $header_array = array (
114
    $session_header
115
    );
116
    $this->_setClientId($header_array);
117
    $this->sforce->__setSoapHeaders($header_array);
118
  }
119
  
120
  private function getObjtype($obj) {
121
    $classArray = explode('\\', get_class($obj));
122
    $objtype = array_pop($classArray);
123
    if (strpos($objtype, 'Sforce', 0) === 0) {
124
      $objtype = substr($objtype, 6);
125
    }
126
    return $objtype;
127
  }
128
129
  public function create($obj) {
130
    $encodedObj = new stdClass();
131
    $encodedObj->metadata = new SoapVar($obj, SOAP_ENC_OBJECT, $this->getObjtype($obj), $this->namespace);
132
     
133
    return $this->sforce->create($encodedObj);
134
  }
135
  
136
  public function update($obj) {    
137
    $encodedObj = new stdClass();
138
    $encodedObj->UpdateMetadata = $obj;
139
    $encodedObj->UpdateMetadata->metadata = new SoapVar($obj->metadata, SOAP_ENC_OBJECT, $this->getObjtype($obj->metadata), $this->namespace);
140
    
141
    return $this->sforce->update($encodedObj);
142
  }
143
  
144
  public function delete($obj) {
145
    $encodedObj = new stdClass();
146
    $encodedObj->metadata = new SoapVar($obj, SOAP_ENC_OBJECT, $this->getObjtype($obj), $this->namespace);
147
     
148
    return $this->sforce->delete($encodedObj);
149
  }  
150
  
151
  public function checkStatus($ids) {
152
    return $this->sforce->checkStatus($ids);
153
  }  
154
155
  public function getLastRequest() {
156
    return $this->sforce->__getLastRequest();
157
  }
158
159
  public function getLastRequestHeaders() {
160
    return $this->sforce->__getLastRequestHeaders();
161
  }
162
163
  public function getLastResponse() {
164
    return $this->sforce->__getLastResponse();
165
  }
166
167
  public function getLastResponseHeaders() {
168
    return $this->sforce->__getLastResponseHeaders();
169
  }
170
171
172
}
173
174
175
?>
176