1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SForce\Client\Partner; |
4
|
|
|
|
5
|
|
|
use SForce\Wsdl\create; |
6
|
|
|
|
7
|
|
|
class MySalesforcePartnerApiConnectionOnly extends Object |
8
|
|
|
{ |
9
|
|
|
private static $username = ''; |
10
|
|
|
private static $password = ''; |
11
|
|
|
private static $security_token = ''; |
12
|
|
|
private static $wsdl_location = ''; |
13
|
|
|
|
14
|
|
|
protected static $my_connection = null; |
15
|
|
|
protected static $my_soap_client = null; |
16
|
|
|
|
17
|
|
|
public static function singleton() |
18
|
|
|
{ |
19
|
|
|
self::$my_connection = self::create_from_cache(); |
20
|
|
|
|
21
|
|
|
if (! self::$my_connection) { |
22
|
|
|
|
23
|
|
|
// Get details from yml |
24
|
|
|
$wsdlLocation = Director::baseFolder() . '/' . self::config()->wsdl_location; |
25
|
|
|
$username = self::config()->username; |
26
|
|
|
$password = self::config()->password . self::config()->security_token; |
27
|
|
|
|
28
|
|
|
// Create connection |
29
|
|
|
self::$my_connection = new MySalesforcePartnerApi(); |
30
|
|
|
self::$my_soap_client = self::$my_connection->createConnection($wsdlLocation); |
31
|
|
|
self::$my_connection->login( |
32
|
|
|
$username, |
|
|
|
|
33
|
|
|
$password |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
// Save connection cache |
37
|
|
|
$cache = SS_Cache::factory(self::class); |
38
|
|
|
$cache->save(self::$my_connection->getLocation(), 'location'); |
39
|
|
|
$cache->save(self::$my_connection->getSessionId(), 'sessionId'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return self::$my_connection; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected static function create_from_cache() |
46
|
|
|
{ |
47
|
|
|
$sessionData = self::retrieve_cache_data(); |
48
|
|
|
|
49
|
|
|
if (! $sessionData) { |
|
|
|
|
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$location = $sessionData['location']; |
54
|
|
|
$sessionId = $sessionData['sessionId']; |
55
|
|
|
|
56
|
|
|
if (! $location || ! $sessionId) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$sessionHeader = new SForce\Wsdl\SessionHeader($sessionId); |
61
|
|
|
|
62
|
|
|
if (! $sessionHeader) { |
|
|
|
|
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Use SforceEnterpriseClient or SforcePartnerClient as appropriate |
67
|
|
|
$wsdlLocation = Director::baseFolder() . '/' . self::config()->wsdl_location; |
68
|
|
|
|
69
|
|
|
// Get from cache |
70
|
|
|
self::$my_connection = new MySalesforcePartnerApi(); |
71
|
|
|
self::$my_connection->createConnection($wsdlLocation); |
72
|
|
|
self::$my_connection->setEndpoint($location); |
73
|
|
|
self::$my_connection->setSessionHeader($sessionHeader); |
74
|
|
|
|
75
|
|
|
return self::$my_connection; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected static function retrieve_cache_data() |
79
|
|
|
{ |
80
|
|
|
$cache = SS_Cache::factory(self::class); |
81
|
|
|
$location = $cache->load('location'); |
82
|
|
|
$sessionId = $cache->load('sessionId'); |
83
|
|
|
|
84
|
|
|
return [ |
85
|
|
|
'location' => $location, |
86
|
|
|
'sessionId' => $sessionId, |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
public static function debug($showSessionID = false) |
92
|
|
|
{ |
93
|
|
|
$xml = self::$my_soap_client->__getLastRequest(); |
94
|
|
|
$domxml = new \DOMDocument('1.0'); |
95
|
|
|
$domxml->preserveWhiteSpace = false; |
96
|
|
|
$domxml->formatOutput = true; |
97
|
|
|
/* @var $xml SimpleXMLElement */ |
98
|
|
|
$domxml->loadXML($xml); |
99
|
|
|
if ($showSessionID === false) { |
100
|
|
|
$list = $domxml->getElementsByTagName('sessionId'); |
101
|
|
|
foreach ($list as $domElement) { |
102
|
|
|
$domElement->nodeValue = '--- hidden for security reasons ---'; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $domxml->saveXML(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|