Completed
Pull Request — master (#3)
by
unknown
01:37
created

User::exists()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 5
eloc 12
nc 5
nop 2
1
<?php
2
3
namespace Surge\LaravelSalesforce\Objects;
4
5
class User extends AbstractObject
6
{
7
    /**
8
     * Check if user already exists on SF.
9
     * $params = [
10
     *  'Email' => '[email protected]'
11
     * ]
12
     *
13
     * @param $params
14
     * @param string $condition
15
     * @return bool|mixed
16
     */
17
    public function exists($params, $condition = 'AND')
18
    {
19
        if (empty($params)) {
20
            return false;
21
        }
22
23
        $query = 'SELECT Id, Name, Email, UserType, UserRoleId FROM ' . $this->getType() . ' WHERE ';
24
25
        $paramsWithKeys = [];
26
        foreach ($params as $fieldName => $fieldValue) {
27
            $paramsWithKeys[] = $fieldName . ' = \'' . addslashes(trim($fieldValue)) . '\'';
28
        }
29
30
        $query .= '(' . implode(' ' . $condition . ' ', $paramsWithKeys) . ')';
31
32
        $response = $this->query($query);
33
34
        if ($response && $response->totalSize > 0) {
35
            return array_shift($response->records);
36
        }
37
38
        return false;
39
    }
40
41
}
42