Completed
Push — master ( cc0ba8...2e633f )
by Zhmayev
01:28
created

WSClient.php ➔ is_assoc_array()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 1
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Vtiger Web Services PHP Client Library
5
 *
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2015, Zhmayev Yaroslav <[email protected]>
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 *
28
 * @author    Zhmayev Yaroslav <[email protected]>
29
 * @copyright 2015-2016 Zhmayev Yaroslav
30
 * @license   The MIT License (MIT)
31
 */
32
33
namespace Salaros\Vtiger\VTWSCLib;
34
35
use Salaros\Vtiger\VTWSCLib\Entities;
36
use Salaros\Vtiger\VTWSCLib\Modules;
37
use Salaros\Vtiger\VTWSCLib\Session;
38
use Salaros\Vtiger\VTWSCLib\WSException;
39
40
/**
41
 * Vtiger Web Services PHP Client
42
 *
43
 * Class WSClient
44
 * @package Salaros\Vtiger\VTWSCLib
45
 */
46
class WSClient
47
{
48
    private $session = null;
49
    
50
    public $modules = null;
51
    public $entities = null;
52
53
    const USE_ACCESSKEY = 1;
54
    const USE_PASSWORD = 2;
55
    
56
    /**
57
     * Class constructor
58
     * @param string $vtigerUrl  The URL of the remote WebServices server
59
     * @param  string $username  User name
60
     * @param  string $secret  Access key token (shown on user's profile page) or password, depends on $loginMode
61
     * @param  integer [$loginMode = self::USE_ACCESSKEY|USE_PASSWORD]  Login mode, defaults to username + accessKey
62
     * @param string [$wsBaseURL = 'webservice.php']  WebServices base URL appended to vTiger root URL
63
     */
64
    public function __construct($vtigerUrl, $username, $secret, $loginMode = self::USE_ACCESSKEY, $wsBaseURL = 'webservice.php')
65
    {
66
        $this->modules = new Modules($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Salaros\Vtiger\VTWSCLib\WSClient>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
        $this->entities = new Entities($this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Salaros\Vtiger\VTWSCLib\WSClient>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
        $this->session = new Session($vtigerUrl, $wsBaseURL);
69
70
        $loginOK = false;
71
        switch ($loginMode) {
72
            case self::USE_ACCESSKEY:
73
                $loginOK = $this->session->login($username, $secret);
74
                break;
75
76
            case self::USE_PASSWORD:
77
                $loginOK = $this->session->loginPassword($username, $secret);
78
                break;
79
            
80
            default:
81
                throw new WSException(sprinf('Unknown login mode: %s', $loginMode));
82
        }
83
84
        if (!$loginOK) {
85
            throw new WSException(sprintf(
86
                'Failed to log into vTiger CRM (User: %s, URL: %s)',
87
                $username,
88
                $vtigerUrl
89
            ));
90
        }
91
    }
92
93
    /**
94
     * Invokes custom operation (defined in vtiger_ws_operation table)
95
     * @access public
96
     * @param  string  $operation  Name of the webservice to invoke
97
     * @param  array   [$params = null] Parameter values to operation
98
     * @param  string  [$method = 'POST'] HTTP request method (GET, POST etc)
99
     * @return array Result object
100
     */
101
    public function invokeOperation($operation, array $params = null, $method = 'POST')
102
    {
103
        if (!is_assoc_array($params)) {
104
            throw new WSException(
105
                "You have to specified a list of operation parameters, but apparently 
106
                it's not an associative array ('prop' => value)!"
107
            );
108
        }
109
110
        $params[ 'operation' ] = $operation;
111
        return $this->session->sendHttpRequest($params, $method);
112
    }
113
114
    /**
115
     * VTiger provides a simple query language for fetching data.
116
     * This language is quite similar to select queries in SQL.
117
     * There are limitations, the queries work on a single Module,
118
     * embedded queries are not supported, and does not support joins.
119
     * But this is still a powerful way of getting data from Vtiger.
120
     * Query always limits its output to 100 records,
121
     * Client application can use limit operator to get different records.
122
     * @access public
123
     * @param  string $query SQL-like expression
124
     * @return array  Query results
125
     */
126
    public function runQuery($query)
127
    {
128
        // Make sure the query ends with ;
129
        $query = (strripos($query, ';') != strlen($query) - 1)
130
            ? trim($query .= ';')
131
            : trim($query);
132
133
        return $this->invokeOperation('query', [ 'query' => $query ], 'GET');
134
    }
135
136
    /**
137
     * Gets an array containing the basic information about current API user
138
     * @access public
139
     * @return array Basic information about current API user
140
     */
141
    public function getCurrentUser()
142
    {
143
        return $this->session->getUserInfo();
144
    }
145
146
    /**
147
     * Gets an array containing the basic information about the connected vTiger instance
148
     * @access public
149
     * @return array Basic information about the connected vTiger instance
150
     */
151
    public function getVtigerInfo()
152
    {
153
        return [
154
            'vtiger' => $this->session->getVtigerVersion(),
155
            'api' => $this->session->getVtigerApiVersion(),
156
        ];
157
    }
158
}
159
160
if (!function_exists('is_assoc_array')) {
161
162
    /**
163
     * Checks if an array is associative or not
164
     * @param  string  Array to test
165
     * @return boolean Returns true in a given array is associative and false if it's not
166
     */
167
    function is_assoc_array(array $array)
168
    {
169
        if (empty($params) || !is_array($params)) {
0 ignored issues
show
Bug introduced by
The variable $params seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
170
            return false;
171
        }
172
173
        foreach (array_keys($array) as $key) {
174
            if (!is_int($key)) {
175
                return true;
176
            }
177
        }
178
        return false;
179
    }
180
}
181