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

MySalesForcePartnerAPI::create_contact()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 3
b 0
f 0
nc 3
nop 2
dl 0
loc 17
rs 9.9
1
<?php
2
3
use SForce\Client\Partner;
4
5
use SForce\SObject;
6
use SForce\Wsdl\SaveResult;
7
8
class MySalesForcePartnerAPI extends Partner
9
{
10
11
    protected $connection = null;
12
13
    public function __construct()
14
    {
15
        parent::__construct();
16
        $this->connection = MySalesForcePartnerAPIConnectionOnly::singleton();
17
    }
18
19
    /**
20
     * todo: all nullify thingy
21
     * assumes that all fields are correct (e.g. correct email format)
22
     * can use email or phone as identifier
23
     * @param  array $fieldsArray
24
     * @param  array $extraFilterArray -independent additional filters for retrieving contact
25
     *
26
     * @return SForce\Wsdl\SaveResult|null
27
     */
28
    public static function create_contact($fieldsArray, $extraFilterArray = [])
29
    {
30
        $response = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
31
        $existingContact = self::array2sql($fieldsArray, $extraFilterArray);
0 ignored issues
show
Unused Code introduced by
The call to MySalesForcePartnerAPI::array2sql() has too many arguments starting with $extraFilterArray. ( Ignorable by Annotation )

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

31
        /** @scrutinizer ignore-call */ 
32
        $existingContact = self::array2sql($fieldsArray, $extraFilterArray);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
32
        // Contact not found. Create a new Contact and set the details
33
        if ($existingContact) {
34
            return null;
35
        } else {
36
            $contact = new SObject();
37
            $contact->setType('Contact');
38
            foreach ($fieldsArray as $fieldName => $fieldValue) {
39
                $contact->$fieldName = $fieldValue;
40
            }
41
            $response = $this->connection->create([$contact]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
42
        }
43
44
        return $response;
45
    }
46
47
    /**
48
     * todo: all nullify thingy
49
     * assumes that all fields are correct (e.g. correct email format)
50
     * can use email or phone as identifier
51
     * @param  array $fieldsArray
52
     * @param  array $extraFilterArray -independent additional filters for retrieving contact
53
     *
54
     * @return SForce\Wsdl\SaveResult|null
55
     */
56
    public static function update_contact($fieldsArray, $extraFilterArray = [])
57
    {
58
        $response = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
59
        $existingContact = self::array2sql($fieldsArray, $extraFilterArray);
0 ignored issues
show
Unused Code introduced by
The call to MySalesForcePartnerAPI::array2sql() has too many arguments starting with $extraFilterArray. ( Ignorable by Annotation )

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

59
        /** @scrutinizer ignore-call */ 
60
        $existingContact = self::array2sql($fieldsArray, $extraFilterArray);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
60
        // Contact found. Update Contact with details
61
        if ($existingContact) {
62
            $contact = new SObject();
63
            $contact->setType('Contact');
64
            $contact->setId($existingContact->getId());
65
            foreach ($fieldsArray as $fieldName => $fieldValue) {
66
                if ($existingContact->$fieldName != $fieldValue) {
67
                    $contact->$fieldName = $fieldValue;
68
                }
69
            }
70
            $response = $this->connection->update([$contact]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
71
        } else {
72
            return null;
73
        }
74
75
        return $response;
76
    }
77
78
    /**
79
     * Retrive a contact by email address or by phone number
80
     * either the email (preferred) of phone needs to be set in the $fieldsArray
81
     *
82
     * @param array $fieldsArray
83
     * @param array $extraFilterArray additional filter key-value sets
84
     *
85
     * @return SForce\SObject|null
86
     */
87
    public static function retrieve_contact($fieldsArray, $extraFilterArray = [])
88
    {
89
        // Check for existing Contact
90
        $existingContact = null;
91
        $filterArray = [];
92
        $finalFilterArray = [];
93
94
        $email = isset($fieldsArray['Email']) ? trim($fieldsArray['Email']) : null;
95
        if ($email) {
96
            $filterArray['Email'] = $email;
97
        } else {
98
            $phone = isset($fieldsArray['Phone']) ? trim($fieldsArray['Phone']) : null;
99
            if ($phone) {
100
                $filterArray['Phone'] = $phone;
101
            }
102
        }
103
        if(count($filterArray)) {
104
            $finalFilterArray[] = self::array2sql($filterArray);
105
            if(count($extraFilterArray)) {
106
                $finalFilterArray[] = self::array2sql($extraFilterArray);
107
            }
108
            $where = ' ( '.implode(' ) AND ( ', $finalFilterArray).' ) ';
109
            $query = 'SELECT Id, FirstName, LastName, Phone, Email FROM Contact WHERE '.$where.' LIMIT 1';
110
111
            $result = $this->connection->query($query);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
112
            if ($result) {
0 ignored issues
show
introduced by
$result is of type SForce\QueryResult, thus it always evaluated to true.
Loading history...
113
                $contacts = $result->getRecords();
114
                if ($contacts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $contacts of type SForce\Wsdl\sObject[] 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...
115
                    $existingContact = $contacts[0];
116
                }
117
            }
118
        }
119
120
        return $existingContact;
121
    }
122
123
    /**
124
     * turns key value pairs into string
125
     * @param  array $array [description]
126
     * @return string
127
     */
128
    protected static function array2sql($array) : string
129
    {
130
        if(count($array) === 0) {
131
            user_error('must have at least one entry');
132
        }
133
        $inner = [];
134
        foreach($array as $field => $value) {
135
            $inner[$field] =  $field.' = '.Convert::raw2sql($value, true);
1 ignored issue
show
Bug introduced by
Are you sure Convert::raw2sql($value, true) of type array|string can be used in concatenation? ( Ignorable by Annotation )

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

135
            $inner[$field] =  $field.' = './** @scrutinizer ignore-type */ Convert::raw2sql($value, true);
Loading history...
136
        }
137
138
        return implode(' AND ', $inner);
139
    }
140
141
    public function debug()
142
    {
143
        $xml = $this->getLastRequest();
144
145
        if (! $xml) {
146
            return null;
147
        }
148
        $domxml = new \DOMDocument('1.0');
149
        $domxml->preserveWhiteSpace = false;
150
        $domxml->formatOutput = true;
151
        /* @var $xml SimpleXMLElement */
152
        $domxml->loadXML($xml);
153
        echo $domxml->saveXML();
154
    }
155
156
    #####################################
157
    # overriding parent class to fix bugs
158
    # in the parent class.
159
    #####################################
160
    /**
161
     * Adds one or more new individual objects to your organization's data.
162
     *
163
     * @param SObject[] $sObjects Array of one or more sObjects (up to 200) to create.
164
     * @param null|string $type Unused
165
     *
166
     * @return SaveResult
167
     */
168
    public function create($sObjects, $type = null)
169
    {
170
        foreach ($sObjects as $sObject) {
171
            if (property_exists($sObject, 'fields')) {
172
                $sObject->setAny($this->_convertToAny($sObject->getFields()));
1 ignored issue
show
Bug introduced by
$sObject->getFields() of type object is incompatible with the type array expected by parameter $fields of SForce\Client\Base::_convertToAny(). ( Ignorable by Annotation )

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

172
                $sObject->setAny($this->_convertToAny(/** @scrutinizer ignore-type */ $sObject->getFields()));
Loading history...
173
                // print_r($this->_convertToAny($sObject->getFields()));
174
            }
175
        }
176
        $createObject = new SForce\Wsdl\create($sObjects);
0 ignored issues
show
Bug introduced by
$sObjects of type SForce\SObject[] is incompatible with the type SForce\Wsdl\sObject expected by parameter $sObjects of SForce\Wsdl\create::__construct(). ( Ignorable by Annotation )

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

176
        $createObject = new SForce\Wsdl\create(/** @scrutinizer ignore-type */ $sObjects);
Loading history...
177
178
        return parent::_create($createObject);
179
    }
180
181
    #####################################
182
    # overriding parent class to fix bugs
183
    # in the parent class.
184
    #####################################
185
    /**
186
     * Updates one or more new individual objects to your organization's data.
187
     *
188
     * @param SObject[] $sObjects Array of one or more sObjects (up to 200) to update.
189
     * @param null|string $type Unused
190
     *
191
     * @return SaveResult
192
     */
193
    public function update($sObjects, $type = null)
1 ignored issue
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

193
    public function update($sObjects, /** @scrutinizer ignore-unused */ $type = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
194
    {
195
        foreach ($sObjects as $sObject) {
196
            if (property_exists($sObject, 'fields')) {
197
                $sObject->setAny($this->_convertToAny($sObject->getFields()));
1 ignored issue
show
Bug introduced by
$sObject->getFields() of type object is incompatible with the type array expected by parameter $fields of SForce\Client\Base::_convertToAny(). ( Ignorable by Annotation )

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

197
                $sObject->setAny($this->_convertToAny(/** @scrutinizer ignore-type */ $sObject->getFields()));
Loading history...
198
            }
199
        }
200
        $updateObject = new SForce\Wsdl\update($sObjects);
0 ignored issues
show
Bug introduced by
$sObjects of type SForce\SObject[] is incompatible with the type SForce\Wsdl\sObject expected by parameter $sObjects of SForce\Wsdl\update::__construct(). ( Ignorable by Annotation )

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

200
        $updateObject = new SForce\Wsdl\update(/** @scrutinizer ignore-type */ $sObjects);
Loading history...
201
202
        return parent::_update($updateObject);
203
    }
204
205
    /**
206
     * NOTA BENE:
207
     * added here because Describe Layout occurs two times (double) with capital D
208
     * (DescribeLayout) and without capital D (changed to describeLayoutDouble)
209
     *
210
     * Use describeLayoutDouble to retrieve information about the layout (presentation
211
     * of data to users) for a given object type. The describeLayoutDouble call returns
212
     * metadata about a given page layout, including layouts for edit and
213
     * display-only views and record type mappings. Note that field-level security
214
     * and layout editability affects which fields appear in a layout.
215
     *
216
     * @param string $type Object Type
217
     * @param array $recordTypeIds
218
     *
219
     * @return Wsdl\DescribeLayoutResult
1 ignored issue
show
Bug introduced by
The type Wsdl\DescribeLayoutResult was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
220
     */
221
    public function describeLayoutDouble($type, array $recordTypeIds = [])
222
    {
223
        $this->setHeaders(static::CALL_DESCRIBE_LAYOUT);
224
225
        return $this->sforce
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->sforce->de...dTypeIds))->getResult() returns the type SForce\Wsdl\DescribeLayoutResult which is incompatible with the documented return type Wsdl\DescribeLayoutResult.
Loading history...
226
            ->describeLayoutDouble(new Wsdl\describeLayoutDouble($type, $recordTypeIds))
1 ignored issue
show
Bug introduced by
The type Wsdl\describeLayoutDouble was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
227
            ->getResult();
228
    }
229
}
230