Completed
Push — 1.0 ( b072ff...1e92f5 )
by Raphaël
06:12
created

ZohoClient::call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 1 Features 0
Metric Value
c 9
b 1
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 10
nc 2
nop 4
1
<?php
2
3
namespace Wabel\Zoho\CRM;
4
5
use GuzzleHttp\Client;
6
use Wabel\Zoho\CRM\Exception\ZohoCRMResponseException;
7
use Wabel\Zoho\CRM\Request\Response;
8
use GuzzleHttp\Psr7\Request;
9
10
/**
11
 * Client for provide interface with Zoho CRM.
12
 *
13
 * TODO : Add comments (a lot)
14
 */
15
class ZohoClient
16
{
17
    /**
18
     * URL for call request.
19
     *
20
     * @var string
21
     */
22
    const BASE_URI = 'https://crm.zoho.com/crm/private';
23
24
    /**
25
     * Token used for session of request.
26
     *
27
     * @var string
28
     */
29
    protected $authtoken;
30
31
    /**
32
     * Instance of the client.
33
     *
34
     * @var Client
35
     */
36
    protected $zohoRestClient;
37
38
    /**
39
     * Format selected for get request.
40
     *
41
     * @var string
42
     */
43
    protected $format;
44
45
    /**
46
     * Module selected for get request.
47
     *
48
     * @var string
49
     */
50
    protected $module;
51
52
    /**
53
     * Construct.
54
     *
55
     * @param string $authtoken      Token for connection
56
     * @param Client $zohoRestClient Guzzl Client for connection [optional]
57
     */
58
    public function __construct($authtoken, Client $zohoRestClient = null)
59
    {
60
        $this->authtoken = $authtoken;
61
        // Only XML format is supported for the time being
62
        $this->format = 'xml';
63
        $this->zohoRestClient = $zohoRestClient ?: new Client();
64
    }
65
66
    /**
67
     * Implements convertLead API method.
68
     *
69
     * @param $leadId
70
     * @param $data
71
     * @param array $params
72
     *
73
     * @return Response The Response object
74
     *
75
     * @throws ZohoCRMResponseException
76
     */
77
    public function convertLead($leadId, $data, $params = array())
78
    {
79
        $module = 'Leads';
80
        $params['leadId'] = $leadId;
81
        $params['newFormat'] = 1;
82
83
        return $this->call($module, 'convertLead', $params, $data);
84
    }
85
86
    /**
87
     * Implements getFields API method.
88
     *
89
     * @return Response The Response object
90
     */
91
    public function getFields($module)
92
    {
93
        $params['newFormat'] = 1;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
94
95
        return $this->call($module, 'getFields', array());
96
    }
97
98
    /**
99
     * Implements deleteRecords API method.
100
     *
101
     * @param string $module
102
     * @param string $id     Id of the record
103
     *
104
     * @return Response The Response object
105
     *
106
     * @throws ZohoCRMResponseException
107
     */
108
    public function deleteRecords($module, $id)
109
    {
110
        $params = array(
111
            'id' => $id,
112
            'newFormat' => 1,
113
        );
114
115
        return $this->call($module, 'deleteRecords', $params);
116
    }
117
118
    /**
119
     * Implements getRecordById API method.
120
     *
121
     * @param string $module The module to use
122
     * @param string $id     Id of the record or a list of IDs separated by a semicolon
123
     *
124
     * @return Response The Response object
125
     *
126
     * @throws ZohoCRMResponseException
127
     */
128
    public function getRecordById($module, $id)
129
    {
130
        if (strpos($id, ';') === false) {
131
            $params['id'] = $id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
132
        } else {
133
            $params['idlist'] = $id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
134
        }
135
        $params['newFormat'] = 1;
136
137
        return $this->call($module, 'getRecordById', $params);
138
    }
139
140
    /**
141
     * Implements getRecords API method.
142
     *
143
     * @param $module
144
     * @param $sortColumnString
145
     * @param $sortOrderString
146
     * @param \DateTime $lastModifiedTime
147
     * @param $selectColumns
148
     * @param $fromIndex
149
     * @param $toIndex
150
     *
151
     * @return Response The Response object
152
     *
153
     * @throws ZohoCRMResponseException
154
     */
155
    public function getRecords($module, $sortColumnString = null, $sortOrderString = null, \DateTimeInterface $lastModifiedTime = null, $selectColumns = null, $fromIndex = null, $toIndex = null)
156
    {
157
        $params['newFormat'] = 1;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
158
        $params['version'] = 1;
159
        if ($selectColumns) {
160
            $params['selectColumns'] = $selectColumns;
161
        }
162
        if ($fromIndex) {
163
            $params['fromIndex'] = $fromIndex;
164
        }
165
        if ($toIndex) {
166
            $params['toIndex'] = $toIndex;
167
        }
168
        if ($sortColumnString) {
169
            $params['sortColumnString'] = $sortColumnString;
170
        }
171
        if ($sortOrderString) {
172
            $params['sortOrderString'] = $sortOrderString;
173
        }
174
        if ($lastModifiedTime) {
175
            $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s');
176
        }
177
178
        return $this->call($module, 'getRecords', $params);
179
    }
180
181
    /**
182
     * Implements getDeletedRecordIds API method.
183
184
     *
185
     * @param string             $module
186
     * @param \DateTimeInterface $lastModifiedTime
187
     * @param int                $fromIndex
188
     * @param int                $toIndex
189
     *
190
     * @return Response
191
     *
192
     * @throws ZohoCRMResponseException
193
     */
194
    public function getDeletedRecordIds($module, \DateTimeInterface $lastModifiedTime = null, $fromIndex = null, $toIndex = null)
195
    {
196
        $params = [];
197
        if ($fromIndex) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fromIndex of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
198
            $params['fromIndex'] = $fromIndex;
199
        }
200
        if ($toIndex) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toIndex of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
201
            $params['toIndex'] = $toIndex;
202
        }
203
        if ($lastModifiedTime) {
204
            $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s');
205
        }
206
207
        return $this->call($module, 'getDeletedRecordIds', $params);
208
    }
209
210
    /**
211
     * Implements getRecords API method.
212
     *
213
     * @param $module
214
     * @param $id
215
     * @param $parentModule
216
     * @param null $fromIndex
217
     * @param null $toIndex
218
     *
219
     * @return Response
220
     *
221
     * @throws ZohoCRMResponseException
222
     */
223
    public function getRelatedRecords($module, $id, $parentModule, $fromIndex = null, $toIndex = null)
224
    {
225
        $params['id'] = $id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
226
        $params['parentModule'] = $parentModule;
227
        $params['newFormat'] = 1;
228
        if ($fromIndex) {
229
            $params['fromIndex'] = $fromIndex;
230
        }
231
        if ($toIndex) {
232
            $params['toIndex'] = $toIndex;
233
        }
234
235
        return $this->call($module, 'getRelatedRecords', $params);
236
    }
237
238
    /**
239
     * Implements searchRecords API method.
240
     *
241
     * @param string    $module
242
     * @param string    $searchCondition
243
     * @param int       $fromIndex
244
     * @param int       $toIndex
245
     * @param \DateTime $lastModifiedTime
246
     * @param null      $selectColumns
247
     *
248
     * @return Response
249
     *
250
     * @throws ZohoCRMResponseException
251
     */
252
    public function searchRecords($module, $searchCondition = null, $fromIndex = null, $toIndex = null, $lastModifiedTime = null, $selectColumns = null)
253
    {
254
        if ($searchCondition) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $searchCondition of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
255
            $params['criteria'] = $searchCondition;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
256
        } else {
257
            $params['criteria'] = '()';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
258
        }
259
        if ($fromIndex) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fromIndex of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
260
            $params['fromIndex'] = $fromIndex;
261
        }
262
        if ($toIndex) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toIndex of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
263
            $params['toIndex'] = $toIndex;
264
        }
265
        if ($lastModifiedTime) {
266
            $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s');
267
        }
268
        if ($selectColumns) {
269
            $params['selectColumns'] = $selectColumns;
270
        }
271
272
        $params['newFormat'] = 1;
273
274
        return $this->call($module, 'searchRecords', $params);
275
    }
276
277
    /**
278
     * Implements getUsers API method.
279
     *
280
     * @param string $type The type of users you want retrieve (among AllUsers, ActiveUsers, DeactiveUsers, AdminUsers and ActiveConfirmedAdmins)
281
     *
282
     * @return Response The array of Zoho Beans parsed from the response
283
     *
284
     * @throws ZohoCRMResponseException
285
     */
286
    public function getUsers($type = 'AllUsers')
287
    {
288
        switch ($type) {
289
            case 'AllUsers':
290
            case 'ActiveUsers':
291
            case 'DeactiveUsers':
292
            case 'AdminUsers':
293
            case 'ActiveConfirmedAdmins':
294
                $params['type'] = $type;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
295
                break;
296
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
297
                $params['type'] = 'AllUsers';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
298
                break;
299
        }
300
        $params['newFormat'] = 1;
301
302
        return $this->call('Users', 'getUsers', $params);
303
    }
304
305
    /**
306
     * Implements insertRecords API method.
307
     *
308
     * @param $module
309
     * @param \SimpleXMLElement$xmlData
310
     * @param bool $wfTrigger
311
     * @param int  $duplicateCheck
312
     * @param bool $isApproval
313
     *
314
     * @return Response
315
     *
316
     * @throws ZohoCRMResponseException
317
     */
318
    public function insertRecords($module, $xmlData, $wfTrigger = null, $duplicateCheck = null, $isApproval = null, $version = 4, $newFormat = 2)
319
    {
320
        if ($wfTrigger) {
321
            $params['wfTrigger'] = 'true';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
322
        }
323
        if ($duplicateCheck) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $duplicateCheck of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
324
            $params['duplicateCheck'] = $duplicateCheck;
0 ignored issues
show
Bug introduced by
The variable $params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
325
        }
326
        if ($isApproval) {
327
            $params['isApproval'] = 'true';
328
        }
329
        $params['newFormat'] = $newFormat;
330
        $params['version'] = $version;
331
332
        return $this->call($module, 'insertRecords', $params, ['xmlData' => $xmlData->asXML()]);
333
    }
334
335
    /**
336
     * Implements updateRecords API method.
337
     *
338
     * @param $module
339
     * @param \SimpleXMLElement $xmlData
340
     * @param string            $id
341
     * @param bool              $wfTrigger
342
     *
343
     * @return Response
344
     *
345
     * @throws ZohoCRMResponseException
346
     */
347
    public function updateRecords($module, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2)
348
    {
349
        $params['newFormat'] = $newFormat;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
350
        $params['version'] = $version;
351
        if ($wfTrigger) {
352
            $params['wfTrigger'] = 'true';
353
        }
354
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
355
            $params['id'] = $id;
356
        }
357
358
        return $this->call($module, 'updateRecords', $params, ['xmlData' => $xmlData->asXML()]);
359
    }
360
361
    /**
362
     * Implements uploadFile API method.
363
     *
364
     * @param $module
365
     * @param $id
366
     * @param $content
367
     *
368
     * @return Response
369
     *
370
     * @throws ZohoCRMResponseException
371
     */
372
    public function uploadFile($module, $id, $content)
373
    {
374
        $params['id'] = $id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
375
        $params['content'] = $content;
376
377
        return $this->call($module, 'uploadFile', $params);
378
    }
379
380
    /**
381
     * Implements downloadFile API method.
382
     *
383
     * @param $module
384
     * @param $id
385
     *
386
     * @return Response
387
     *
388
     * @throws ZohoCRMResponseException
389
     */
390
    public function downloadFile($module, $id)
391
    {
392
        $params['id'] = $id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
393
394
        return $this->call($module, 'downloadFile', $params);
395
    }
396
397
    /**
398
     * Returns a list of modules from Zoho.
399
     */
400
    public function getModules()
401
    {
402
        return $this->call('Info', 'getModules', ['type' => 'api']);
403
    }
404
405
406
    /**
407
     * Get the body of the request
408
     *
409
     * @param array $params Params
410
     * @param Object $data Data
411
     * @return string
412
     */
413
    protected function getRequestBody($params, $data, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

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

Loading history...
414
    {
415
        if($data){
416
            return http_build_query($data, '', '&');
417
        }
418
        return '';
419
    }
420
421
    
422
    /**
423
     * Make the call using the client.
424
     *
425
     * @param string                   $module  The module to use
426
     * @param string                   $command Command to call
427
     * @param array                    $params  Options
0 ignored issues
show
Documentation introduced by
There is no parameter named $params. Did you maybe mean $getParams?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
428
     * @param \SimpleXMLElement|string $data    Data to send [optional]
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
429
     * @param array                    $options Options to add for configurations [optional]
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
430
     *
431
     * @return Response
432
     */
433
    public function call($module, $command, $getParams = array(), $postParams = array())
434
    {
435
        $getParams['authtoken'] = $this->authtoken;
436
        $getParams['scope'] = 'crmapi';
437
438
        $uri = $this->getRequestURI($module, $command);
439
440
//        $params = $this->getRequestBody([], $postParams, []);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
441
//        $stream = \GuzzleHttp\Psr7\stream_for($params);
442
        $response = $this->zohoRestClient->request('POST', $uri, ['query'=>$getParams+$postParams]);
443
        $zohoResponse = new Response((string)$response->getBody(), $module, $command);
444
        if ($zohoResponse->ifSuccess()) {
445
            return $zohoResponse;
446
        } else {
447
            throw new ZohoCRMResponseException($zohoResponse);
448
        }
449
    }
450
451
    /**
452
     * Get the current request uri.
453
     *
454
     * @param  $module The module to use
455
     * @param string $command Command for get uri
456
     *
457
     * @return string
458
     */
459
    protected function getRequestURI($module, $command)
460
    {
461
        if (empty($module)) {
462
            throw new \RuntimeException('Zoho CRM module is not set.');
463
        }
464
        $parts = array(self::BASE_URI, $this->format, $module, $command);
465
466
        return implode('/', $parts);
467
    }
468
}
469