Completed
Pull Request — 1.1 (#29)
by
unknown
02:37
created

ZohoClient::downloadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
9
/**
10
 * Client for provide interface with Zoho CRM.
11
 *
12
 * TODO : Add comments (a lot)
13
 */
14
class ZohoClient
15
{
16
    /**
17
     * URL for call request.
18
     *
19
     * @var string
20
     */
21
    protected $BASE_URI = 'https://crm.zoho.com/crm/private';
22
23
    /**
24
     * Token used for session of request.
25
     *
26
     * @var string
27
     */
28
    protected $authtoken;
29
30
    /**
31
     * Instance of the client.
32
     *
33
     * @var Client
34
     */
35
    protected $zohoRestClient;
36
37
    /**
38
     * Format selected for get request.
39
     *
40
     * @var string
41
     */
42
    protected $format;
43
44
    /**
45
     * Module selected for get request.
46
     *
47
     * @var string
48
     */
49
    protected $module;
50
51
    /**
52
     * Construct.
53
     *
54
     * @param string $authtoken      Token for connection
55
     * @param Client $zohoRestClient Guzzl Client for connection [optional]
56
     */
57
    public function __construct($authtoken, Client $zohoRestClient = null)
58
    {
59
        $this->authtoken = $authtoken;
60
        // Only XML format is supported for the time being
61
        $this->format = 'xml';
62
        $this->zohoRestClient = $zohoRestClient ?: new Client();
63
    }
64
65
    public function setEuDomain() {
66
    	$this->BASE_URI = 'https://crm.zoho.eu/crm/private';
67
    }
68
69
    /**
70
     * Implements convertLead API method.
71
     *
72
     * @param $leadId
73
     * @param $data
74
     * @param array $params
75
     *
76
     * @return Response The Response object
77
     *
78
     * @throws ZohoCRMResponseException
79
     */
80
    public function convertLead($leadId, $data, $params = array())
81
    {
82
        $module = 'Leads';
83
        $params['leadId'] = $leadId;
84
        $params['newFormat'] = 1;
85
86
        return $this->call($module, 'convertLead', $params, $data);
87
    }
88
89
    /**
90
     * Implements getFields API method.
91
     *
92
     * @return Response The Response object
93
     */
94
    public function getFields($module)
95
    {
96
        $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...
97
98
        return $this->call($module, 'getFields', array());
99
    }
100
101
    /**
102
     * Implements deleteRecords API method.
103
     *
104
     * @param string $module
105
     * @param string $id     Id of the record
106
     *
107
     * @return Response The Response object
108
     *
109
     * @throws ZohoCRMResponseException
110
     */
111
    public function deleteRecords($module, $id)
112
    {
113
        $params = array(
114
            'id' => $id,
115
            'newFormat' => 1,
116
        );
117
118
        return $this->call($module, 'deleteRecords', $params);
119
    }
120
121
    /**
122
     * Implements getRecordById API method.
123
     *
124
     * @param string $module The module to use
125
     * @param string $id     Id of the record or a list of IDs separated by a semicolon
126
     *
127
     * @return Response The Response object
128
     *
129
     * @throws ZohoCRMResponseException
130
     */
131
    public function getRecordById($module, $id)
132
    {
133
        if (strpos($id, ';') === false) {
134
            $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...
135
        } else {
136
            $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...
137
        }
138
        $params['newFormat'] = 1;
139
140
        return $this->call($module, 'getRecordById', $params);
141
    }
142
143
    /**
144
     * Implements getRecords API method.
145
     *
146
     * @param $module
147
     * @param $sortColumnString
148
     * @param $sortOrderString
149
     * @param \DateTime $lastModifiedTime
150
     * @param $selectColumns
151
     * @param $fromIndex
152
     * @param $toIndex
153
     *
154
     * @return Response The Response object
155
     *
156
     * @throws ZohoCRMResponseException
157
     */
158
    public function getRecords($module, $sortColumnString = null, $sortOrderString = null, \DateTimeInterface $lastModifiedTime = null, $selectColumns = null, $fromIndex = null, $toIndex = null)
159
    {
160
        $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...
161
        $params['version'] = 1;
162
        if ($selectColumns) {
163
            $params['selectColumns'] = $selectColumns;
164
        }
165
        if ($fromIndex) {
166
            $params['fromIndex'] = $fromIndex;
167
        }
168
        if ($toIndex) {
169
            $params['toIndex'] = $toIndex;
170
        }
171
        if ($sortColumnString) {
172
            $params['sortColumnString'] = $sortColumnString;
173
        }
174
        if ($sortOrderString) {
175
            $params['sortOrderString'] = $sortOrderString;
176
        }
177
        if ($lastModifiedTime) {
178
            $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s');
179
        }
180
181
        return $this->call($module, 'getRecords', $params);
182
    }
183
184
    /**
185
     * Implements getDeletedRecordIds API method.
186
187
     *
188
     * @param string             $module
189
     * @param \DateTimeInterface $lastModifiedTime
190
     * @param int                $fromIndex
191
     * @param int                $toIndex
192
     *
193
     * @return Response
194
     *
195
     * @throws ZohoCRMResponseException
196
     */
197
    public function getDeletedRecordIds($module, \DateTimeInterface $lastModifiedTime = null, $fromIndex = null, $toIndex = null)
198
    {
199
        $params = [];
200
        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...
201
            $params['fromIndex'] = $fromIndex;
202
        }
203
        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...
204
            $params['toIndex'] = $toIndex;
205
        }
206
        if ($lastModifiedTime) {
207
            $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s');
208
        }
209
210
        return $this->call($module, 'getDeletedRecordIds', $params);
211
    }
212
213
    /**
214
     * Implements getRecords API method.
215
     *
216
     * @param $module
217
     * @param $id
218
     * @param $parentModule
219
     * @param null $fromIndex
220
     * @param null $toIndex
221
     *
222
     * @return Response
223
     *
224
     * @throws ZohoCRMResponseException
225
     */
226
    public function getRelatedRecords($module, $id, $parentModule, $fromIndex = null, $toIndex = null)
227
    {
228
        $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...
229
        $params['parentModule'] = $parentModule;
230
        $params['newFormat'] = 1;
231
        if ($fromIndex) {
232
            $params['fromIndex'] = $fromIndex;
233
        }
234
        if ($toIndex) {
235
            $params['toIndex'] = $toIndex;
236
        }
237
238
        return $this->call($module, 'getRelatedRecords', $params);
239
    }
240
241
    /**
242
     * Implements searchRecords API method.
243
     *
244
     * @param string    $module
245
     * @param string    $searchCondition
246
     * @param int       $fromIndex
247
     * @param int       $toIndex
248
     * @param \DateTime $lastModifiedTime
249
     * @param null      $selectColumns
250
     *
251
     * @return Response
252
     *
253
     * @throws ZohoCRMResponseException
254
     */
255
    public function searchRecords($module, $searchCondition = null, $fromIndex = null, $toIndex = null, $lastModifiedTime = null, $selectColumns = null)
256
    {
257
        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...
258
            $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...
259
        } else {
260
            $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...
261
        }
262
        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...
263
            $params['fromIndex'] = $fromIndex;
264
        }
265
        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...
266
            $params['toIndex'] = $toIndex;
267
        }
268
        if ($lastModifiedTime) {
269
            $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s');
270
        }
271
        if ($selectColumns) {
272
            $params['selectColumns'] = $selectColumns;
273
        }
274
275
        $params['newFormat'] = 1;
276
277
        return $this->call($module, 'searchRecords', $params);
278
    }
279
280
    /**
281
     * Implements getUsers API method.
282
     *
283
     * @param string $type The type of users you want retrieve (among AllUsers, ActiveUsers, DeactiveUsers, AdminUsers and ActiveConfirmedAdmins)
284
     *
285
     * @return Response The array of Zoho Beans parsed from the response
286
     *
287
     * @throws ZohoCRMResponseException
288
     */
289
    public function getUsers($type = 'AllUsers')
290
    {
291
        switch ($type) {
292
            case 'AllUsers':
293
            case 'ActiveUsers':
294
            case 'DeactiveUsers':
295
            case 'AdminUsers':
296
            case 'ActiveConfirmedAdmins':
297
                $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...
298
                break;
299
            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...
300
                $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...
301
                break;
302
        }
303
        $params['newFormat'] = 1;
304
305
        return $this->call('Users', 'getUsers', $params);
306
    }
307
308
    /**
309
     * Implements insertRecords API method.
310
     *
311
     * @param $module
312
     * @param \SimpleXMLElement$xmlData
313
     * @param bool $wfTrigger
314
     * @param int  $duplicateCheck
315
     * @param bool $isApproval
316
     *
317
     * @return Response
318
     *
319
     * @throws ZohoCRMResponseException
320
     */
321
    public function insertRecords($module, $xmlData, $wfTrigger = null, $duplicateCheck = null, $isApproval = null, $version = 4, $newFormat = 2)
322
    {
323
        if ($wfTrigger) {
324
            $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...
325
        }
326
        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...
327
            $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...
328
        }
329
        if ($isApproval) {
330
            $params['isApproval'] = 'true';
331
        }
332
        $params['newFormat'] = $newFormat;
333
        $params['version'] = $version;
334
335
        return $this->call($module, 'insertRecords', $params, ['xmlData' => $xmlData->asXML()]);
336
    }
337
338
    /**
339
     * Implements updateRecords API method.
340
     *
341
     * @param $module
342
     * @param \SimpleXMLElement $xmlData
343
     * @param string            $id
344
     * @param bool              $wfTrigger
345
     *
346
     * @return Response
347
     *
348
     * @throws ZohoCRMResponseException
349
     */
350 View Code Duplication
    public function updateRecords($module, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
351
    {
352
        $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...
353
        $params['version'] = $version;
354
        if ($wfTrigger) {
355
            $params['wfTrigger'] = 'true';
356
        }
357
        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...
358
            $params['id'] = $id;
359
        }
360
361
        return $this->call($module, 'updateRecords', $params, ['xmlData' => $xmlData->asXML()]);
362
    }
363
364
    /**
365
     * Implements updateRelatedRecords API method.
366
     *
367
     * @param $module
368
     * @param $relatedModule
369
     * @param \SimpleXMLElement $xmlData
370
     * @param string            $id
371
     * @param bool              $wfTrigger
372
     *
373
     * @return Response
374
     *
375
     * @throws ZohoCRMResponseException
376
     */
377 View Code Duplication
    public function updateRelatedRecords($module, $relatedModule, $xmlData, $id = null, $wfTrigger = null, $version = 4, $newFormat = 2)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
378
    {
379
        $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...
380
        $params['version'] = $version;
381
        $params['relatedModule'] = $relatedModule;
382
        if ($wfTrigger) {
383
            $params['wfTrigger'] = 'true';
384
        }
385
        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...
386
            $params['id'] = $id;
387
        }
388
389
        return $this->call($module, 'updateRelatedRecords', $params, ['xmlData' => $xmlData->asXML()]);
390
    }
391
392
    /**
393
     * Implements uploadFile API method.
394
     *
395
     * @param $module
396
     * @param $id
397
     * @param $content
398
     *
399
     * @return Response
400
     *
401
     * @throws ZohoCRMResponseException
402
     */
403
    public function uploadFile($module, $id, $content)
404
    {
405
        $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...
406
        $params['content'] = $content;
407
408
        return $this->call($module, 'uploadFile', $params);
409
    }
410
411
    /**
412
     * Implements downloadFile API method.
413
     *
414
     * @param $module
415
     * @param $id
416
     *
417
     * @return Response
418
     *
419
     * @throws ZohoCRMResponseException
420
     */
421
    public function downloadFile($module, $id)
422
    {
423
        $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...
424
425
        return $this->call($module, 'downloadFile', $params);
426
    }
427
428
    /**
429
     * Returns a list of modules from Zoho.
430
     */
431
    public function getModules()
432
    {
433
        return $this->call('Info', 'getModules', ['type' => 'api']);
434
    }
435
    
436
    /**
437
     * Make the call using the client.
438
     *
439
     * @param string                   $module  The module to use
440
     * @param string                   $command Command to call
441
     * @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...
442
     * @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...
443
     * @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...
444
     *
445
     * @return Response
446
     */
447
    public function call($module, $command, $getParams = array(), $postParams = array())
448
    {
449
        $getParams['authtoken'] = $this->authtoken;
450
        $getParams['scope'] = 'crmapi';
451
452
        $uri = $this->getRequestURI($module, $command);
453
        if(isset($postParams['xmlData'])){
454
            $postParams[]=[
455
                'name' => 'xmlData',
456
                'contents' => $postParams['xmlData']
457
            ];
458
            unset($postParams['xmlData']);
459
        }
460
461
	$response = $this->zohoRestClient->request('POST', $uri, ['query'=>$getParams,'multipart'=> $postParams]);
462
        $zohoResponse = new Response((string)$response->getBody(), $module, $command);
463
        if ($zohoResponse->ifSuccess()) {
464
            return $zohoResponse;
465
        } else {
466
            throw new ZohoCRMResponseException($zohoResponse);
467
        }
468
    }
469
470
    /**
471
     * Get the current request uri.
472
     *
473
     * @param  $module The module to use
474
     * @param string $command Command for get uri
475
     *
476
     * @return string
477
     */
478
    protected function getRequestURI($module, $command)
479
    {
480
        if (empty($module)) {
481
            throw new \RuntimeException('Zoho CRM module is not set.');
482
        }
483
        $parts = array($this->BASE_URI, $this->format, $module, $command);
484
485
        return implode('/', $parts);
486
    }
487
}
488