Passed
Push — master ( c40630...f0da52 )
by Nicolaas
08:09 queued 05:18
created

MySalesForcePartnerAPIConnectionOnly::debug()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
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,
1 ignored issue
show
Bug introduced by
It seems like $username can also be of type array; however, parameter $username of SForce\Client\Base::login() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
                /** @scrutinizer ignore-type */ $username,
Loading history...
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) {
0 ignored issues
show
introduced by
$sessionData is a non-empty array, thus ! $sessionData is always false.
Loading history...
Bug Best Practice introduced by
The expression $sessionData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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) {
0 ignored issues
show
introduced by
$sessionHeader is of type SForce\Wsdl\SessionHeader, thus it always evaluated to true.
Loading history...
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