retrieve_cache_data()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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