Completed
Pull Request — 1.1 (#32)
by David
06:44
created

ZohoClient::convertLead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Wabel\Zoho\CRM;
4
5
use Guzzle\Common\Exception\InvalidArgumentException;
6
use GuzzleHttp\Client;
7
use Psr\Http\Message\UriInterface;
8
use Wabel\Zoho\CRM\Exception\ZohoCRMResponseException;
9
use Wabel\Zoho\CRM\Request\Response;
10
11
/**
12
 * Client for provide interface with Zoho CRM.
13
 *
14
 * TODO : Add comments (a lot)
15
 */
16
class ZohoClient
17
{
18
    /**
19
     * .com URL for call request.
20
     *
21
     * @var string
22
     */
23
    const COM_BASE_URI = 'https://crm.zoho.com/crm/private';
24
25
    /**
26
     * .eu URL for call request.
27
     *
28
     * @var string
29
     */
30
    const EU_BASE_URI = 'https://crm.zoho.eu/crm/private';
31
32
    /**
33
     * Configurable URL for call request.
34
     *
35
     * @var string
36
     */
37
    protected $baseUri;
38
39
    /**
40
     * Token used for session of request.
41
     *
42
     * @var string
43
     */
44
    protected $authtoken;
45
46
    /**
47
     * Instance of the client.
48
     *
49
     * @var Client
50
     */
51
    protected $zohoRestClient;
52
53
    /**
54
     * Format selected for get request.
55
     *
56
     * @var string
57
     */
58
    protected $format;
59
60
    /**
61
     * Module selected for get request.
62
     *
63
     * @var string
64
     */
65
    protected $module;
66
67
    /**
68
     * Construct.
69
     *
70
     * @param string $authtoken      Token for connection
71
     * @param Client $zohoRestClient Guzzl Client for connection [optional]
72
     * @param string $baseUri        Configurable URL for call request
73
     */
74
    public function __construct(string $authtoken, Client $zohoRestClient = null, string $baseUri = self::COM_BASE_URI)
75
    {
76
        $this->baseUri = $baseUri;
77
        $this->authtoken = $authtoken;
78
        // Only XML format is supported for the time being
79
        $this->format = 'xml';
80
        $this->zohoRestClient = $zohoRestClient ?: new Client();
81
    }
82
83
    /**
84
     * Implements convertLead API method.
85
     *
86
     * @param $leadId
87
     * @param $data
88
     * @param array $params
89
     *
90
     * @return Response The Response object
91
     *
92
     * @throws ZohoCRMResponseException
93
     */
94
    public function convertLead($leadId, $data, $params = array())
95
    {
96
        $module = 'Leads';
97
        $params['leadId'] = $leadId;
98
        $params['newFormat'] = 1;
99
100
        return $this->call($module, 'convertLead', $params, $data);
101
    }
102
103
    /**
104
     * Implements getFields API method.
105
     *
106
     * @return Response The Response object
107
     */
108
    public function getFields($module)
109
    {
110
        $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...
111
112
        return $this->call($module, 'getFields', array());
113
    }
114
115
    /**
116
     * Implements deleteRecords API method.
117
     *
118
     * @param string $module
119
     * @param string $id     Id of the record
120
     *
121
     * @return Response The Response object
122
     *
123
     * @throws ZohoCRMResponseException
124
     */
125
    public function deleteRecords($module, $id)
126
    {
127
        $params = array(
128
            'id' => $id,
129
            'newFormat' => 1,
130
        );
131
132
        return $this->call($module, 'deleteRecords', $params);
133
    }
134
135
    /**
136
     * Implements getRecordById API method.
137
     *
138
     * @param string $module The module to use
139
     * @param string $id     Id of the record or a list of IDs separated by a semicolon
140
     *
141
     * @return Response The Response object
142
     *
143
     * @throws ZohoCRMResponseException
144
     */
145
    public function getRecordById($module, $id)
146
    {
147
        if (strpos($id, ';') === false) {
148
            $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...
149
        } else {
150
            $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...
151
        }
152
        $params['newFormat'] = 1;
153
154
        return $this->call($module, 'getRecordById', $params);
155
    }
156
157
    /**
158
     * Implements getRecords API method.
159
     *
160
     * @param $module
161
     * @param $sortColumnString
162
     * @param $sortOrderString
163
     * @param \DateTime $lastModifiedTime
164
     * @param $selectColumns
165
     * @param $fromIndex
166
     * @param $toIndex
167
     *
168
     * @return Response The Response object
169
     *
170
     * @throws ZohoCRMResponseException
171
     */
172
    public function getRecords($module, $sortColumnString = null, $sortOrderString = null, \DateTimeInterface $lastModifiedTime = null, $selectColumns = null, $fromIndex = null, $toIndex = null)
173
    {
174
        $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...
175
        $params['version'] = 1;
176
        if ($selectColumns) {
177
            $params['selectColumns'] = $selectColumns;
178
        }
179
        if ($fromIndex) {
180
            $params['fromIndex'] = $fromIndex;
181
        }
182
        if ($toIndex) {
183
            $params['toIndex'] = $toIndex;
184
        }
185
        if ($sortColumnString) {
186
            $params['sortColumnString'] = $sortColumnString;
187
        }
188
        if ($sortOrderString) {
189
            $params['sortOrderString'] = $sortOrderString;
190
        }
191
        if ($lastModifiedTime) {
192
            $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s');
193
        }
194
195
        return $this->call($module, 'getRecords', $params);
196
    }
197
198
    /**
199
     * Implements getDeletedRecordIds API method.
200
201
     *
202
     * @param string             $module
203
     * @param \DateTimeInterface $lastModifiedTime
204
     * @param int                $fromIndex
205
     * @param int                $toIndex
206
     *
207
     * @return Response
208
     *
209
     * @throws ZohoCRMResponseException
210
     */
211
    public function getDeletedRecordIds($module, \DateTimeInterface $lastModifiedTime = null, $fromIndex = null, $toIndex = null)
212
    {
213
        $params = [];
214
        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...
215
            $params['fromIndex'] = $fromIndex;
216
        }
217
        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...
218
            $params['toIndex'] = $toIndex;
219
        }
220
        if ($lastModifiedTime) {
221
            $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s');
222
        }
223
224
        return $this->call($module, 'getDeletedRecordIds', $params);
225
    }
226
227
    /**
228
     * Implements getRecords API method.
229
     *
230
     * @param $module
231
     * @param $id
232
     * @param $parentModule
233
     * @param null $fromIndex
234
     * @param null $toIndex
235
     *
236
     * @return Response
237
     *
238
     * @throws ZohoCRMResponseException
239
     */
240
    public function getRelatedRecords($module, $id, $parentModule, $fromIndex = null, $toIndex = null)
241
    {
242
        $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...
243
        $params['parentModule'] = $parentModule;
244
        $params['newFormat'] = 1;
245
        if ($fromIndex) {
246
            $params['fromIndex'] = $fromIndex;
247
        }
248
        if ($toIndex) {
249
            $params['toIndex'] = $toIndex;
250
        }
251
252
        return $this->call($module, 'getRelatedRecords', $params);
253
    }
254
255
    /**
256
     * Implements searchRecords API method.
257
     *
258
     * @param string    $module
259
     * @param string    $searchCondition
260
     * @param int       $fromIndex
261
     * @param int       $toIndex
262
     * @param \DateTime $lastModifiedTime
263
     * @param null      $selectColumns
264
     *
265
     * @return Response
266
     *
267
     * @throws ZohoCRMResponseException
268
     */
269
    public function searchRecords($module, $searchCondition = null, $fromIndex = null, $toIndex = null, $lastModifiedTime = null, $selectColumns = null)
270
    {
271
        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...
272
            $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...
273
        } else {
274
            $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...
275
        }
276
        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...
277
            $params['fromIndex'] = $fromIndex;
278
        }
279
        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...
280
            $params['toIndex'] = $toIndex;
281
        }
282
        if ($lastModifiedTime) {
283
            $params['lastModifiedTime'] = $lastModifiedTime->format('Y-m-d H:i:s');
284
        }
285
        if ($selectColumns) {
286
            $params['selectColumns'] = $selectColumns;
287
        }
288
289
        $params['newFormat'] = 1;
290
291
        return $this->call($module, 'searchRecords', $params);
292
    }
293
294
    /**
295
     * Implements getUsers API method.
296
     *
297
     * @param string $type The type of users you want retrieve (among AllUsers, ActiveUsers, DeactiveUsers, AdminUsers and ActiveConfirmedAdmins)
298
     *
299
     * @return Response The array of Zoho Beans parsed from the response
300
     *
301
     * @throws ZohoCRMResponseException
302
     */
303
    public function getUsers($type = 'AllUsers')
304
    {
305
        switch ($type) {
306
            case 'AllUsers':
307
            case 'ActiveUsers':
308
            case 'DeactiveUsers':
309
            case 'AdminUsers':
310
            case 'ActiveConfirmedAdmins':
311
                $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...
312
                break;
313
            default :
314
                $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...
315
                break;
316
        }
317
        $params['newFormat'] = 1;
318
319
        return $this->call('Users', 'getUsers', $params);
320
    }
321
322
    /**
323
     * Implements insertRecords API method.
324
     *
325
     * @param $module
326
     * @param \SimpleXMLElement$xmlData
327
     * @param bool $wfTrigger
328
     * @param int  $duplicateCheck
329
     * @param bool $isApproval
330
     *
331
     * @return Response
332
     *
333
     * @throws ZohoCRMResponseException
334
     */
335
    public function insertRecords($module, $xmlData, $wfTrigger = null, $duplicateCheck = null, $isApproval = null, $version = 4, $newFormat = 2)
336
    {
337
        if ($wfTrigger) {
338
            $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...
339
        }
340
        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...
341
            $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...
342
        }
343
        if ($isApproval) {
344
            $params['isApproval'] = 'true';
345
        }
346
        $params['newFormat'] = $newFormat;
347
        $params['version'] = $version;
348
349
        return $this->call($module, 'insertRecords', $params, ['xmlData' => $xmlData->asXML()]);
350
    }
351
352
    /**
353
     * Implements updateRecords API method.
354
     *
355
     * @param $module
356
     * @param \SimpleXMLElement $xmlData
357
     * @param string            $id
358
     * @param bool              $wfTrigger
359
     *
360
     * @return Response
361
     *
362
     * @throws ZohoCRMResponseException
363
     */
364 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...
365
    {
366
        $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...
367
        $params['version'] = $version;
368
        if ($wfTrigger) {
369
            $params['wfTrigger'] = 'true';
370
        }
371
        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...
372
            $params['id'] = $id;
373
        }
374
375
        return $this->call($module, 'updateRecords', $params, ['xmlData' => $xmlData->asXML()]);
376
    }
377
378
    /**
379
     * Implements updateRelatedRecords API method.
380
     *
381
     * @param $module
382
     * @param $relatedModule
383
     * @param \SimpleXMLElement $xmlData
384
     * @param string            $id
385
     * @param bool              $wfTrigger
386
     *
387
     * @return Response
388
     *
389
     * @throws ZohoCRMResponseException
390
     */
391 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...
392
    {
393
        $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...
394
        $params['version'] = $version;
395
        $params['relatedModule'] = $relatedModule;
396
        if ($wfTrigger) {
397
            $params['wfTrigger'] = 'true';
398
        }
399
        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...
400
            $params['id'] = $id;
401
        }
402
403
        return $this->call($module, 'updateRelatedRecords', $params, ['xmlData' => $xmlData->asXML()]);
404
    }
405
406
    /**
407
     * Implements uploadFile API method.
408
     *
409
     * @param $module
410
     * @param $id
411
     * @param $content
412
     * @param $filename
413
     *
414
     * @return Response
415
     *
416
     * @throws ZohoCRMResponseException
417
     */
418
    public function uploadFile($module, $id, $content, $filename = null)
419
    {
420
        $getParams['id'] = $id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$getParams was never initialized. Although not strictly required by PHP, it is generally a good practice to add $getParams = 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...
421
        $postParams = [];
422
423
        if ($content instanceof UriInterface) {
424
            $getParams['attachmentUrl'] = $content->__toString();
425
        } else {
426
            $innerPostParams = [];
427
            $innerPostParams['name'] = 'content';
428
            if ($content instanceof \SplFileInfo) {
429
                $innerPostParams['filename'] = $filename ?? $content->getBasename();
430
                $innerPostParams['contents'] = $content->openFile();
431
            } else if (is_resource($content)) {
432
                $innerPostParams['filename'] = $filename ?? basename(stream_get_meta_data($content)["uri"]);
433
                $innerPostParams['contents'] = $content;
434
            } else {
435
                if (!$filename) {
436
                    throw new InvalidArgumentException('filename cannot be empty');
437
                }
438
                $innerPostParams['filename'] = $filename;
439
                $innerPostParams['contents'] = $content;
440
            }
441
            $postParams[] = $innerPostParams;
442
        }
443
444
        return $this->call($module, 'uploadFile', ['id' => $id], $postParams);
445
    }
446
447
    /**
448
     * Implements downloadFile API method.
449
     *
450
     * @param $module
451
     * @param $id
452
     *
453
     * @return Response
454
     *
455
     * @throws ZohoCRMResponseException
456
     */
457
    public function downloadFile($module, $id)
458
    {
459
        $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...
460
461
        return $this->call($module, 'downloadFile', $params);
462
    }
463
464
    /**
465
     * Returns a list of modules from Zoho.
466
     */
467
    public function getModules()
468
    {
469
        return $this->call('Info', 'getModules', ['type' => 'api']);
470
    }
471
    
472
    /**
473
     * Make the call using the client.
474
     *
475
     * @param string                   $module  The module to use
476
     * @param string                   $command Command to call
477
     * @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...
478
     * @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...
479
     * @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...
480
     *
481
     * @return Response
482
     */
483
    public function call($module, $command, $getParams = array(), $postParams = array())
484
    {
485
        $getParams['authtoken'] = $this->authtoken;
486
        $getParams['scope'] = 'crmapi';
487
488
        $uri = $this->getRequestURI($module, $command);
489
        if(isset($postParams['xmlData'])){
490
            $postParams[]=[
491
                'name' => 'xmlData',
492
                'contents' => $postParams['xmlData']
493
            ];
494
            unset($postParams['xmlData']);
495
        }
496
497
        $response = $this->zohoRestClient->request('POST', $uri, ['query'=>$getParams,'multipart'=> $postParams]);
498
        $zohoResponse = new Response((string)$response->getBody(), $module, $command);
499
        if ($zohoResponse->ifSuccess()) {
500
            return $zohoResponse;
501
        } else {
502
            throw new ZohoCRMResponseException($zohoResponse);
503
        }
504
    }
505
506
    /**
507
     * Get the current request uri.
508
     *
509
     * @param  $module The module to use
510
     * @param string $command Command for get uri
511
     *
512
     * @return string
513
     */
514
    protected function getRequestURI($module, $command)
515
    {
516
        if (empty($module)) {
517
            throw new \RuntimeException('Zoho CRM module is not set.');
518
        }
519
        $parts = array($this->baseUri, $this->format, $module, $command);
520
521
        return implode('/', $parts);
522
    }
523
}
524